eap_sim_db.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /*
  2. * hostapd / EAP-SIM database/authenticator gateway
  3. * Copyright (c) 2005-2010, 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 that is using an external program as an SS7 gateway to
  10. * GSM/UMTS authentication center (HLR/AuC). hlr_auc_gw is an example
  11. * implementation of such a gateway program. This eap_sim_db.c takes care of
  12. * EAP-SIM/AKA pseudonyms and re-auth identities. It can be used with different
  13. * gateway implementations for HLR/AuC access. Alternatively, it can also be
  14. * completely replaced if the in-memory database of pseudonyms/re-auth
  15. * identities is not suitable for some cases.
  16. */
  17. #include "includes.h"
  18. #include <sys/un.h>
  19. #ifdef CONFIG_SQLITE
  20. #include <sqlite3.h>
  21. #endif /* CONFIG_SQLITE */
  22. #include "common.h"
  23. #include "crypto/random.h"
  24. #include "eap_common/eap_sim_common.h"
  25. #include "eap_server/eap_sim_db.h"
  26. #include "eloop.h"
  27. struct eap_sim_pseudonym {
  28. struct eap_sim_pseudonym *next;
  29. char *permanent; /* permanent username */
  30. char *pseudonym; /* pseudonym username */
  31. };
  32. struct eap_sim_db_pending {
  33. struct eap_sim_db_pending *next;
  34. char imsi[20];
  35. enum { PENDING, SUCCESS, FAILURE } state;
  36. void *cb_session_ctx;
  37. struct os_time timestamp;
  38. int aka;
  39. union {
  40. struct {
  41. u8 kc[EAP_SIM_MAX_CHAL][EAP_SIM_KC_LEN];
  42. u8 sres[EAP_SIM_MAX_CHAL][EAP_SIM_SRES_LEN];
  43. u8 rand[EAP_SIM_MAX_CHAL][GSM_RAND_LEN];
  44. int num_chal;
  45. } sim;
  46. struct {
  47. u8 rand[EAP_AKA_RAND_LEN];
  48. u8 autn[EAP_AKA_AUTN_LEN];
  49. u8 ik[EAP_AKA_IK_LEN];
  50. u8 ck[EAP_AKA_CK_LEN];
  51. u8 res[EAP_AKA_RES_MAX_LEN];
  52. size_t res_len;
  53. } aka;
  54. } u;
  55. };
  56. struct eap_sim_db_data {
  57. int sock;
  58. char *fname;
  59. char *local_sock;
  60. void (*get_complete_cb)(void *ctx, void *session_ctx);
  61. void *ctx;
  62. struct eap_sim_pseudonym *pseudonyms;
  63. struct eap_sim_reauth *reauths;
  64. struct eap_sim_db_pending *pending;
  65. #ifdef CONFIG_SQLITE
  66. sqlite3 *sqlite_db;
  67. char db_tmp_identity[100];
  68. char db_tmp_pseudonym_str[100];
  69. struct eap_sim_pseudonym db_tmp_pseudonym;
  70. struct eap_sim_reauth db_tmp_reauth;
  71. #endif /* CONFIG_SQLITE */
  72. };
  73. #ifdef CONFIG_SQLITE
  74. static int db_table_exists(sqlite3 *db, const char *name)
  75. {
  76. char cmd[128];
  77. os_snprintf(cmd, sizeof(cmd), "SELECT 1 FROM %s;", name);
  78. return sqlite3_exec(db, cmd, NULL, NULL, NULL) == SQLITE_OK;
  79. }
  80. static int db_table_create_pseudonym(sqlite3 *db)
  81. {
  82. char *err = NULL;
  83. const char *sql =
  84. "CREATE TABLE pseudonyms("
  85. " permanent CHAR(21) PRIMARY KEY,"
  86. " pseudonym CHAR(21) NOT NULL"
  87. ");";
  88. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Adding database table for "
  89. "pseudonym information");
  90. if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
  91. wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
  92. sqlite3_free(err);
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. static int db_table_create_reauth(sqlite3 *db)
  98. {
  99. char *err = NULL;
  100. const char *sql =
  101. "CREATE TABLE reauth("
  102. " permanent CHAR(21) PRIMARY KEY,"
  103. " reauth_id CHAR(21) NOT NULL,"
  104. " counter INTEGER,"
  105. " mk CHAR(40),"
  106. " k_encr CHAR(32),"
  107. " k_aut CHAR(64),"
  108. " k_re CHAR(64)"
  109. ");";
  110. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Adding database table for "
  111. "reauth information");
  112. if (sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK) {
  113. wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
  114. sqlite3_free(err);
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. static sqlite3 * db_open(const char *db_file)
  120. {
  121. sqlite3 *db;
  122. if (sqlite3_open(db_file, &db)) {
  123. wpa_printf(MSG_ERROR, "EAP-SIM DB: Failed to open database "
  124. "%s: %s", db_file, sqlite3_errmsg(db));
  125. sqlite3_close(db);
  126. return NULL;
  127. }
  128. if (!db_table_exists(db, "pseudonyms") &&
  129. db_table_create_pseudonym(db) < 0) {
  130. sqlite3_close(db);
  131. return NULL;
  132. }
  133. if (!db_table_exists(db, "reauth") &&
  134. db_table_create_reauth(db) < 0) {
  135. sqlite3_close(db);
  136. return NULL;
  137. }
  138. return db;
  139. }
  140. static int valid_db_string(const char *str)
  141. {
  142. const char *pos = str;
  143. while (*pos) {
  144. if ((*pos < '0' || *pos > '9') &&
  145. (*pos < 'a' || *pos > 'f'))
  146. return 0;
  147. pos++;
  148. }
  149. return 1;
  150. }
  151. static int db_add_pseudonym(struct eap_sim_db_data *data,
  152. const char *permanent, char *pseudonym)
  153. {
  154. char cmd[128];
  155. char *err = NULL;
  156. if (!valid_db_string(permanent) || !valid_db_string(pseudonym)) {
  157. os_free(pseudonym);
  158. return -1;
  159. }
  160. os_snprintf(cmd, sizeof(cmd), "INSERT OR REPLACE INTO pseudonyms "
  161. "(permanent, pseudonym) VALUES ('%s', '%s');",
  162. permanent, pseudonym);
  163. os_free(pseudonym);
  164. if (sqlite3_exec(data->sqlite_db, cmd, NULL, NULL, &err) != SQLITE_OK)
  165. {
  166. wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
  167. sqlite3_free(err);
  168. return -1;
  169. }
  170. return 0;
  171. }
  172. static int get_pseudonym_cb(void *ctx, int argc, char *argv[], char *col[])
  173. {
  174. struct eap_sim_db_data *data = ctx;
  175. int i;
  176. for (i = 0; i < argc; i++) {
  177. if (os_strcmp(col[i], "permanent") == 0 && argv[i]) {
  178. os_strlcpy(data->db_tmp_identity, argv[i],
  179. sizeof(data->db_tmp_identity));
  180. }
  181. }
  182. return 0;
  183. }
  184. static char *
  185. db_get_pseudonym(struct eap_sim_db_data *data, const char *pseudonym)
  186. {
  187. char cmd[128];
  188. if (!valid_db_string(pseudonym))
  189. return NULL;
  190. os_memset(&data->db_tmp_identity, 0, sizeof(data->db_tmp_identity));
  191. os_snprintf(cmd, sizeof(cmd),
  192. "SELECT permanent FROM pseudonyms WHERE pseudonym='%s';",
  193. pseudonym);
  194. if (sqlite3_exec(data->sqlite_db, cmd, get_pseudonym_cb, data, NULL) !=
  195. SQLITE_OK)
  196. return NULL;
  197. if (data->db_tmp_identity[0] == '\0')
  198. return NULL;
  199. return data->db_tmp_identity;
  200. }
  201. static int db_add_reauth(struct eap_sim_db_data *data, const char *permanent,
  202. char *reauth_id, u16 counter, const u8 *mk,
  203. const u8 *k_encr, const u8 *k_aut, const u8 *k_re)
  204. {
  205. char cmd[2000], *pos, *end;
  206. char *err = NULL;
  207. if (!valid_db_string(permanent) || !valid_db_string(reauth_id)) {
  208. os_free(reauth_id);
  209. return -1;
  210. }
  211. pos = cmd;
  212. end = pos + sizeof(cmd);
  213. pos += os_snprintf(pos, end - pos, "INSERT OR REPLACE INTO reauth "
  214. "(permanent, reauth_id, counter%s%s%s%s) "
  215. "VALUES ('%s', '%s', %u",
  216. mk ? ", mk" : "",
  217. k_encr ? ", k_encr" : "",
  218. k_aut ? ", k_aut" : "",
  219. k_re ? ", k_re" : "",
  220. permanent, reauth_id, counter);
  221. os_free(reauth_id);
  222. if (mk) {
  223. pos += os_snprintf(pos, end - pos, ", '");
  224. pos += wpa_snprintf_hex(pos, end - pos, mk, EAP_SIM_MK_LEN);
  225. pos += os_snprintf(pos, end - pos, "'");
  226. }
  227. if (k_encr) {
  228. pos += os_snprintf(pos, end - pos, ", '");
  229. pos += wpa_snprintf_hex(pos, end - pos, k_encr,
  230. EAP_SIM_K_ENCR_LEN);
  231. pos += os_snprintf(pos, end - pos, "'");
  232. }
  233. if (k_aut) {
  234. pos += os_snprintf(pos, end - pos, ", '");
  235. pos += wpa_snprintf_hex(pos, end - pos, k_aut,
  236. EAP_AKA_PRIME_K_AUT_LEN);
  237. pos += os_snprintf(pos, end - pos, "'");
  238. }
  239. if (k_re) {
  240. pos += os_snprintf(pos, end - pos, ", '");
  241. pos += wpa_snprintf_hex(pos, end - pos, k_re,
  242. EAP_AKA_PRIME_K_RE_LEN);
  243. pos += os_snprintf(pos, end - pos, "'");
  244. }
  245. os_snprintf(pos, end - pos, ");");
  246. if (sqlite3_exec(data->sqlite_db, cmd, NULL, NULL, &err) != SQLITE_OK)
  247. {
  248. wpa_printf(MSG_ERROR, "EAP-SIM DB: SQLite error: %s", err);
  249. sqlite3_free(err);
  250. return -1;
  251. }
  252. return 0;
  253. }
  254. static int get_reauth_cb(void *ctx, int argc, char *argv[], char *col[])
  255. {
  256. struct eap_sim_db_data *data = ctx;
  257. int i;
  258. struct eap_sim_reauth *reauth = &data->db_tmp_reauth;
  259. for (i = 0; i < argc; i++) {
  260. if (os_strcmp(col[i], "permanent") == 0 && argv[i]) {
  261. os_strlcpy(data->db_tmp_identity, argv[i],
  262. sizeof(data->db_tmp_identity));
  263. reauth->permanent = data->db_tmp_identity;
  264. } else if (os_strcmp(col[i], "counter") == 0 && argv[i]) {
  265. reauth->counter = atoi(argv[i]);
  266. } else if (os_strcmp(col[i], "mk") == 0 && argv[i]) {
  267. hexstr2bin(argv[i], reauth->mk, sizeof(reauth->mk));
  268. } else if (os_strcmp(col[i], "k_encr") == 0 && argv[i]) {
  269. hexstr2bin(argv[i], reauth->k_encr,
  270. sizeof(reauth->k_encr));
  271. } else if (os_strcmp(col[i], "k_aut") == 0 && argv[i]) {
  272. hexstr2bin(argv[i], reauth->k_aut,
  273. sizeof(reauth->k_aut));
  274. } else if (os_strcmp(col[i], "k_re") == 0 && argv[i]) {
  275. hexstr2bin(argv[i], reauth->k_re,
  276. sizeof(reauth->k_re));
  277. }
  278. }
  279. return 0;
  280. }
  281. static struct eap_sim_reauth *
  282. db_get_reauth(struct eap_sim_db_data *data, const char *reauth_id)
  283. {
  284. char cmd[256];
  285. if (!valid_db_string(reauth_id))
  286. return NULL;
  287. os_memset(&data->db_tmp_reauth, 0, sizeof(data->db_tmp_reauth));
  288. os_strlcpy(data->db_tmp_pseudonym_str, reauth_id,
  289. sizeof(data->db_tmp_pseudonym_str));
  290. data->db_tmp_reauth.reauth_id = data->db_tmp_pseudonym_str;
  291. os_snprintf(cmd, sizeof(cmd),
  292. "SELECT * FROM reauth WHERE reauth_id='%s';", reauth_id);
  293. if (sqlite3_exec(data->sqlite_db, cmd, get_reauth_cb, data, NULL) !=
  294. SQLITE_OK)
  295. return NULL;
  296. if (data->db_tmp_reauth.permanent == NULL)
  297. return NULL;
  298. return &data->db_tmp_reauth;
  299. }
  300. static void db_remove_reauth(struct eap_sim_db_data *data,
  301. struct eap_sim_reauth *reauth)
  302. {
  303. char cmd[256];
  304. if (!valid_db_string(reauth->permanent))
  305. return;
  306. os_snprintf(cmd, sizeof(cmd),
  307. "DELETE FROM reauth WHERE permanent='%s';",
  308. reauth->permanent);
  309. sqlite3_exec(data->sqlite_db, cmd, NULL, NULL, NULL);
  310. }
  311. #endif /* CONFIG_SQLITE */
  312. static struct eap_sim_db_pending *
  313. eap_sim_db_get_pending(struct eap_sim_db_data *data, const char *imsi, int aka)
  314. {
  315. struct eap_sim_db_pending *entry, *prev = NULL;
  316. entry = data->pending;
  317. while (entry) {
  318. if (entry->aka == aka && os_strcmp(entry->imsi, imsi) == 0) {
  319. if (prev)
  320. prev->next = entry->next;
  321. else
  322. data->pending = entry->next;
  323. break;
  324. }
  325. prev = entry;
  326. entry = entry->next;
  327. }
  328. return entry;
  329. }
  330. static void eap_sim_db_add_pending(struct eap_sim_db_data *data,
  331. struct eap_sim_db_pending *entry)
  332. {
  333. entry->next = data->pending;
  334. data->pending = entry;
  335. }
  336. static void eap_sim_db_sim_resp_auth(struct eap_sim_db_data *data,
  337. const char *imsi, char *buf)
  338. {
  339. char *start, *end, *pos;
  340. struct eap_sim_db_pending *entry;
  341. int num_chal;
  342. /*
  343. * SIM-RESP-AUTH <IMSI> Kc(i):SRES(i):RAND(i) ...
  344. * SIM-RESP-AUTH <IMSI> FAILURE
  345. * (IMSI = ASCII string, Kc/SRES/RAND = hex string)
  346. */
  347. entry = eap_sim_db_get_pending(data, imsi, 0);
  348. if (entry == NULL) {
  349. wpa_printf(MSG_DEBUG, "EAP-SIM DB: No pending entry for the "
  350. "received message found");
  351. return;
  352. }
  353. start = buf;
  354. if (os_strncmp(start, "FAILURE", 7) == 0) {
  355. wpa_printf(MSG_DEBUG, "EAP-SIM DB: External server reported "
  356. "failure");
  357. entry->state = FAILURE;
  358. eap_sim_db_add_pending(data, entry);
  359. data->get_complete_cb(data->ctx, entry->cb_session_ctx);
  360. return;
  361. }
  362. num_chal = 0;
  363. while (num_chal < EAP_SIM_MAX_CHAL) {
  364. end = os_strchr(start, ' ');
  365. if (end)
  366. *end = '\0';
  367. pos = os_strchr(start, ':');
  368. if (pos == NULL)
  369. goto parse_fail;
  370. *pos = '\0';
  371. if (hexstr2bin(start, entry->u.sim.kc[num_chal],
  372. EAP_SIM_KC_LEN))
  373. goto parse_fail;
  374. start = pos + 1;
  375. pos = os_strchr(start, ':');
  376. if (pos == NULL)
  377. goto parse_fail;
  378. *pos = '\0';
  379. if (hexstr2bin(start, entry->u.sim.sres[num_chal],
  380. EAP_SIM_SRES_LEN))
  381. goto parse_fail;
  382. start = pos + 1;
  383. if (hexstr2bin(start, entry->u.sim.rand[num_chal],
  384. GSM_RAND_LEN))
  385. goto parse_fail;
  386. num_chal++;
  387. if (end == NULL)
  388. break;
  389. else
  390. start = end + 1;
  391. }
  392. entry->u.sim.num_chal = num_chal;
  393. entry->state = SUCCESS;
  394. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Authentication data parsed "
  395. "successfully - callback");
  396. eap_sim_db_add_pending(data, entry);
  397. data->get_complete_cb(data->ctx, entry->cb_session_ctx);
  398. return;
  399. parse_fail:
  400. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
  401. os_free(entry);
  402. }
  403. static void eap_sim_db_aka_resp_auth(struct eap_sim_db_data *data,
  404. const char *imsi, char *buf)
  405. {
  406. char *start, *end;
  407. struct eap_sim_db_pending *entry;
  408. /*
  409. * AKA-RESP-AUTH <IMSI> <RAND> <AUTN> <IK> <CK> <RES>
  410. * AKA-RESP-AUTH <IMSI> FAILURE
  411. * (IMSI = ASCII string, RAND/AUTN/IK/CK/RES = hex string)
  412. */
  413. entry = eap_sim_db_get_pending(data, imsi, 1);
  414. if (entry == NULL) {
  415. wpa_printf(MSG_DEBUG, "EAP-SIM DB: No pending entry for the "
  416. "received message found");
  417. return;
  418. }
  419. start = buf;
  420. if (os_strncmp(start, "FAILURE", 7) == 0) {
  421. wpa_printf(MSG_DEBUG, "EAP-SIM DB: External server reported "
  422. "failure");
  423. entry->state = FAILURE;
  424. eap_sim_db_add_pending(data, entry);
  425. data->get_complete_cb(data->ctx, entry->cb_session_ctx);
  426. return;
  427. }
  428. end = os_strchr(start, ' ');
  429. if (end == NULL)
  430. goto parse_fail;
  431. *end = '\0';
  432. if (hexstr2bin(start, entry->u.aka.rand, EAP_AKA_RAND_LEN))
  433. goto parse_fail;
  434. start = end + 1;
  435. end = os_strchr(start, ' ');
  436. if (end == NULL)
  437. goto parse_fail;
  438. *end = '\0';
  439. if (hexstr2bin(start, entry->u.aka.autn, EAP_AKA_AUTN_LEN))
  440. goto parse_fail;
  441. start = end + 1;
  442. end = os_strchr(start, ' ');
  443. if (end == NULL)
  444. goto parse_fail;
  445. *end = '\0';
  446. if (hexstr2bin(start, entry->u.aka.ik, EAP_AKA_IK_LEN))
  447. goto parse_fail;
  448. start = end + 1;
  449. end = os_strchr(start, ' ');
  450. if (end == NULL)
  451. goto parse_fail;
  452. *end = '\0';
  453. if (hexstr2bin(start, entry->u.aka.ck, EAP_AKA_CK_LEN))
  454. goto parse_fail;
  455. start = end + 1;
  456. end = os_strchr(start, ' ');
  457. if (end)
  458. *end = '\0';
  459. else {
  460. end = start;
  461. while (*end)
  462. end++;
  463. }
  464. entry->u.aka.res_len = (end - start) / 2;
  465. if (entry->u.aka.res_len > EAP_AKA_RES_MAX_LEN) {
  466. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Too long RES");
  467. entry->u.aka.res_len = 0;
  468. goto parse_fail;
  469. }
  470. if (hexstr2bin(start, entry->u.aka.res, entry->u.aka.res_len))
  471. goto parse_fail;
  472. entry->state = SUCCESS;
  473. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Authentication data parsed "
  474. "successfully - callback");
  475. eap_sim_db_add_pending(data, entry);
  476. data->get_complete_cb(data->ctx, entry->cb_session_ctx);
  477. return;
  478. parse_fail:
  479. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
  480. os_free(entry);
  481. }
  482. static void eap_sim_db_receive(int sock, void *eloop_ctx, void *sock_ctx)
  483. {
  484. struct eap_sim_db_data *data = eloop_ctx;
  485. char buf[1000], *pos, *cmd, *imsi;
  486. int res;
  487. res = recv(sock, buf, sizeof(buf), 0);
  488. if (res < 0)
  489. return;
  490. wpa_hexdump_ascii_key(MSG_MSGDUMP, "EAP-SIM DB: Received from an "
  491. "external source", (u8 *) buf, res);
  492. if (res == 0)
  493. return;
  494. if (res >= (int) sizeof(buf))
  495. res = sizeof(buf) - 1;
  496. buf[res] = '\0';
  497. if (data->get_complete_cb == NULL) {
  498. wpa_printf(MSG_DEBUG, "EAP-SIM DB: No get_complete_cb "
  499. "registered");
  500. return;
  501. }
  502. /* <cmd> <IMSI> ... */
  503. cmd = buf;
  504. pos = os_strchr(cmd, ' ');
  505. if (pos == NULL)
  506. goto parse_fail;
  507. *pos = '\0';
  508. imsi = pos + 1;
  509. pos = os_strchr(imsi, ' ');
  510. if (pos == NULL)
  511. goto parse_fail;
  512. *pos = '\0';
  513. wpa_printf(MSG_DEBUG, "EAP-SIM DB: External response=%s for IMSI %s",
  514. cmd, imsi);
  515. if (os_strcmp(cmd, "SIM-RESP-AUTH") == 0)
  516. eap_sim_db_sim_resp_auth(data, imsi, pos + 1);
  517. else if (os_strcmp(cmd, "AKA-RESP-AUTH") == 0)
  518. eap_sim_db_aka_resp_auth(data, imsi, pos + 1);
  519. else
  520. wpa_printf(MSG_INFO, "EAP-SIM DB: Unknown external response "
  521. "'%s'", cmd);
  522. return;
  523. parse_fail:
  524. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
  525. }
  526. static int eap_sim_db_open_socket(struct eap_sim_db_data *data)
  527. {
  528. struct sockaddr_un addr;
  529. static int counter = 0;
  530. if (os_strncmp(data->fname, "unix:", 5) != 0)
  531. return -1;
  532. data->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  533. if (data->sock < 0) {
  534. wpa_printf(MSG_INFO, "socket(eap_sim_db): %s", strerror(errno));
  535. return -1;
  536. }
  537. os_memset(&addr, 0, sizeof(addr));
  538. addr.sun_family = AF_UNIX;
  539. os_snprintf(addr.sun_path, sizeof(addr.sun_path),
  540. "/tmp/eap_sim_db_%d-%d", getpid(), counter++);
  541. os_free(data->local_sock);
  542. data->local_sock = os_strdup(addr.sun_path);
  543. if (bind(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  544. wpa_printf(MSG_INFO, "bind(eap_sim_db): %s", strerror(errno));
  545. close(data->sock);
  546. data->sock = -1;
  547. return -1;
  548. }
  549. os_memset(&addr, 0, sizeof(addr));
  550. addr.sun_family = AF_UNIX;
  551. os_strlcpy(addr.sun_path, data->fname + 5, sizeof(addr.sun_path));
  552. if (connect(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  553. wpa_printf(MSG_INFO, "connect(eap_sim_db): %s",
  554. strerror(errno));
  555. wpa_hexdump_ascii(MSG_INFO, "HLR/AuC GW socket",
  556. (u8 *) addr.sun_path,
  557. os_strlen(addr.sun_path));
  558. close(data->sock);
  559. data->sock = -1;
  560. return -1;
  561. }
  562. eloop_register_read_sock(data->sock, eap_sim_db_receive, data, NULL);
  563. return 0;
  564. }
  565. static void eap_sim_db_close_socket(struct eap_sim_db_data *data)
  566. {
  567. if (data->sock >= 0) {
  568. eloop_unregister_read_sock(data->sock);
  569. close(data->sock);
  570. data->sock = -1;
  571. }
  572. if (data->local_sock) {
  573. unlink(data->local_sock);
  574. os_free(data->local_sock);
  575. data->local_sock = NULL;
  576. }
  577. }
  578. /**
  579. * eap_sim_db_init - Initialize EAP-SIM DB / authentication gateway interface
  580. * @config: Configuration data (e.g., file name)
  581. * @get_complete_cb: Callback function for reporting availability of triplets
  582. * @ctx: Context pointer for get_complete_cb
  583. * Returns: Pointer to a private data structure or %NULL on failure
  584. */
  585. struct eap_sim_db_data *
  586. eap_sim_db_init(const char *config,
  587. void (*get_complete_cb)(void *ctx, void *session_ctx),
  588. void *ctx)
  589. {
  590. struct eap_sim_db_data *data;
  591. char *pos;
  592. data = os_zalloc(sizeof(*data));
  593. if (data == NULL)
  594. return NULL;
  595. data->sock = -1;
  596. data->get_complete_cb = get_complete_cb;
  597. data->ctx = ctx;
  598. data->fname = os_strdup(config);
  599. if (data->fname == NULL)
  600. goto fail;
  601. pos = os_strstr(data->fname, " db=");
  602. if (pos) {
  603. *pos = '\0';
  604. #ifdef CONFIG_SQLITE
  605. pos += 4;
  606. data->sqlite_db = db_open(pos);
  607. if (data->sqlite_db == NULL)
  608. goto fail;
  609. #endif /* CONFIG_SQLITE */
  610. }
  611. if (os_strncmp(data->fname, "unix:", 5) == 0) {
  612. if (eap_sim_db_open_socket(data)) {
  613. wpa_printf(MSG_DEBUG, "EAP-SIM DB: External database "
  614. "connection not available - will retry "
  615. "later");
  616. }
  617. }
  618. return data;
  619. fail:
  620. eap_sim_db_close_socket(data);
  621. os_free(data->fname);
  622. os_free(data);
  623. return NULL;
  624. }
  625. static void eap_sim_db_free_pseudonym(struct eap_sim_pseudonym *p)
  626. {
  627. os_free(p->permanent);
  628. os_free(p->pseudonym);
  629. os_free(p);
  630. }
  631. static void eap_sim_db_free_reauth(struct eap_sim_reauth *r)
  632. {
  633. os_free(r->permanent);
  634. os_free(r->reauth_id);
  635. os_free(r);
  636. }
  637. /**
  638. * eap_sim_db_deinit - Deinitialize EAP-SIM DB/authentication gw interface
  639. * @priv: Private data pointer from eap_sim_db_init()
  640. */
  641. void eap_sim_db_deinit(void *priv)
  642. {
  643. struct eap_sim_db_data *data = priv;
  644. struct eap_sim_pseudonym *p, *prev;
  645. struct eap_sim_reauth *r, *prevr;
  646. struct eap_sim_db_pending *pending, *prev_pending;
  647. #ifdef CONFIG_SQLITE
  648. if (data->sqlite_db) {
  649. sqlite3_close(data->sqlite_db);
  650. data->sqlite_db = NULL;
  651. }
  652. #endif /* CONFIG_SQLITE */
  653. eap_sim_db_close_socket(data);
  654. os_free(data->fname);
  655. p = data->pseudonyms;
  656. while (p) {
  657. prev = p;
  658. p = p->next;
  659. eap_sim_db_free_pseudonym(prev);
  660. }
  661. r = data->reauths;
  662. while (r) {
  663. prevr = r;
  664. r = r->next;
  665. eap_sim_db_free_reauth(prevr);
  666. }
  667. pending = data->pending;
  668. while (pending) {
  669. prev_pending = pending;
  670. pending = pending->next;
  671. os_free(prev_pending);
  672. }
  673. os_free(data);
  674. }
  675. static int eap_sim_db_send(struct eap_sim_db_data *data, const char *msg,
  676. size_t len)
  677. {
  678. int _errno = 0;
  679. if (send(data->sock, msg, len, 0) < 0) {
  680. _errno = errno;
  681. wpa_printf(MSG_INFO, "send[EAP-SIM DB UNIX]: %s",
  682. strerror(errno));
  683. }
  684. if (_errno == ENOTCONN || _errno == EDESTADDRREQ || _errno == EINVAL ||
  685. _errno == ECONNREFUSED) {
  686. /* Try to reconnect */
  687. eap_sim_db_close_socket(data);
  688. if (eap_sim_db_open_socket(data) < 0)
  689. return -1;
  690. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Reconnected to the "
  691. "external server");
  692. if (send(data->sock, msg, len, 0) < 0) {
  693. wpa_printf(MSG_INFO, "send[EAP-SIM DB UNIX]: %s",
  694. strerror(errno));
  695. return -1;
  696. }
  697. }
  698. return 0;
  699. }
  700. static void eap_sim_db_expire_pending(struct eap_sim_db_data *data)
  701. {
  702. /* TODO: add limit for maximum length for pending list; remove latest
  703. * (i.e., last) entry from the list if the limit is reached; could also
  704. * use timeout to expire pending entries */
  705. }
  706. /**
  707. * eap_sim_db_get_gsm_triplets - Get GSM triplets
  708. * @data: Private data pointer from eap_sim_db_init()
  709. * @username: Permanent username (prefix | IMSI)
  710. * @max_chal: Maximum number of triplets
  711. * @_rand: Buffer for RAND values
  712. * @kc: Buffer for Kc values
  713. * @sres: Buffer for SRES values
  714. * @cb_session_ctx: Session callback context for get_complete_cb()
  715. * Returns: Number of triplets received (has to be less than or equal to
  716. * max_chal), -1 (EAP_SIM_DB_FAILURE) on error (e.g., user not found), or
  717. * -2 (EAP_SIM_DB_PENDING) if results are not yet available. In this case, the
  718. * callback function registered with eap_sim_db_init() will be called once the
  719. * results become available.
  720. *
  721. * When using an external server for GSM triplets, this function can always
  722. * start a request and return EAP_SIM_DB_PENDING immediately if authentication
  723. * triplets are not available. Once the triplets are received, callback
  724. * function registered with eap_sim_db_init() is called to notify EAP state
  725. * machine to reprocess the message. This eap_sim_db_get_gsm_triplets()
  726. * function will then be called again and the newly received triplets will then
  727. * be given to the caller.
  728. */
  729. int eap_sim_db_get_gsm_triplets(struct eap_sim_db_data *data,
  730. const char *username, int max_chal,
  731. u8 *_rand, u8 *kc, u8 *sres,
  732. void *cb_session_ctx)
  733. {
  734. struct eap_sim_db_pending *entry;
  735. int len, ret;
  736. char msg[40];
  737. const char *imsi;
  738. size_t imsi_len;
  739. if (username == NULL || username[0] != EAP_SIM_PERMANENT_PREFIX ||
  740. username[1] == '\0' || os_strlen(username) > sizeof(entry->imsi)) {
  741. wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
  742. username);
  743. return EAP_SIM_DB_FAILURE;
  744. }
  745. imsi = username + 1;
  746. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get GSM triplets for IMSI '%s'",
  747. imsi);
  748. entry = eap_sim_db_get_pending(data, imsi, 0);
  749. if (entry) {
  750. int num_chal;
  751. if (entry->state == FAILURE) {
  752. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
  753. "failure");
  754. os_free(entry);
  755. return EAP_SIM_DB_FAILURE;
  756. }
  757. if (entry->state == PENDING) {
  758. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
  759. "still pending");
  760. eap_sim_db_add_pending(data, entry);
  761. return EAP_SIM_DB_PENDING;
  762. }
  763. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
  764. "%d challenges", entry->u.sim.num_chal);
  765. num_chal = entry->u.sim.num_chal;
  766. if (num_chal > max_chal)
  767. num_chal = max_chal;
  768. os_memcpy(_rand, entry->u.sim.rand, num_chal * GSM_RAND_LEN);
  769. os_memcpy(sres, entry->u.sim.sres,
  770. num_chal * EAP_SIM_SRES_LEN);
  771. os_memcpy(kc, entry->u.sim.kc, num_chal * EAP_SIM_KC_LEN);
  772. os_free(entry);
  773. return num_chal;
  774. }
  775. if (data->sock < 0) {
  776. if (eap_sim_db_open_socket(data) < 0)
  777. return EAP_SIM_DB_FAILURE;
  778. }
  779. imsi_len = os_strlen(imsi);
  780. len = os_snprintf(msg, sizeof(msg), "SIM-REQ-AUTH ");
  781. if (len < 0 || len + imsi_len >= sizeof(msg))
  782. return EAP_SIM_DB_FAILURE;
  783. os_memcpy(msg + len, imsi, imsi_len);
  784. len += imsi_len;
  785. ret = os_snprintf(msg + len, sizeof(msg) - len, " %d", max_chal);
  786. if (ret < 0 || (size_t) ret >= sizeof(msg) - len)
  787. return EAP_SIM_DB_FAILURE;
  788. len += ret;
  789. wpa_printf(MSG_DEBUG, "EAP-SIM DB: requesting SIM authentication "
  790. "data for IMSI '%s'", imsi);
  791. if (eap_sim_db_send(data, msg, len) < 0)
  792. return EAP_SIM_DB_FAILURE;
  793. entry = os_zalloc(sizeof(*entry));
  794. if (entry == NULL)
  795. return EAP_SIM_DB_FAILURE;
  796. os_get_time(&entry->timestamp);
  797. os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
  798. entry->cb_session_ctx = cb_session_ctx;
  799. entry->state = PENDING;
  800. eap_sim_db_add_pending(data, entry);
  801. eap_sim_db_expire_pending(data);
  802. return EAP_SIM_DB_PENDING;
  803. }
  804. static char * eap_sim_db_get_next(struct eap_sim_db_data *data, char prefix)
  805. {
  806. char *id, *pos, *end;
  807. u8 buf[10];
  808. if (random_get_bytes(buf, sizeof(buf)))
  809. return NULL;
  810. id = os_malloc(sizeof(buf) * 2 + 2);
  811. if (id == NULL)
  812. return NULL;
  813. pos = id;
  814. end = id + sizeof(buf) * 2 + 2;
  815. *pos++ = prefix;
  816. pos += wpa_snprintf_hex(pos, end - pos, buf, sizeof(buf));
  817. return id;
  818. }
  819. /**
  820. * eap_sim_db_get_next_pseudonym - EAP-SIM DB: Get next pseudonym
  821. * @data: Private data pointer from eap_sim_db_init()
  822. * @method: EAP method (SIM/AKA/AKA')
  823. * Returns: Next pseudonym (allocated string) or %NULL on failure
  824. *
  825. * This function is used to generate a pseudonym for EAP-SIM. The returned
  826. * pseudonym is not added to database at this point; it will need to be added
  827. * with eap_sim_db_add_pseudonym() once the authentication has been completed
  828. * successfully. Caller is responsible for freeing the returned buffer.
  829. */
  830. char * eap_sim_db_get_next_pseudonym(struct eap_sim_db_data *data,
  831. enum eap_sim_db_method method)
  832. {
  833. char prefix = EAP_SIM_REAUTH_ID_PREFIX;
  834. switch (method) {
  835. case EAP_SIM_DB_SIM:
  836. prefix = EAP_SIM_PSEUDONYM_PREFIX;
  837. break;
  838. case EAP_SIM_DB_AKA:
  839. prefix = EAP_AKA_PSEUDONYM_PREFIX;
  840. break;
  841. case EAP_SIM_DB_AKA_PRIME:
  842. prefix = EAP_AKA_PRIME_PSEUDONYM_PREFIX;
  843. break;
  844. }
  845. return eap_sim_db_get_next(data, prefix);
  846. }
  847. /**
  848. * eap_sim_db_get_next_reauth_id - EAP-SIM DB: Get next reauth_id
  849. * @data: Private data pointer from eap_sim_db_init()
  850. * @method: EAP method (SIM/AKA/AKA')
  851. * Returns: Next reauth_id (allocated string) or %NULL on failure
  852. *
  853. * This function is used to generate a fast re-authentication identity for
  854. * EAP-SIM. The returned reauth_id is not added to database at this point; it
  855. * will need to be added with eap_sim_db_add_reauth() once the authentication
  856. * has been completed successfully. Caller is responsible for freeing the
  857. * returned buffer.
  858. */
  859. char * eap_sim_db_get_next_reauth_id(struct eap_sim_db_data *data,
  860. enum eap_sim_db_method method)
  861. {
  862. char prefix = EAP_SIM_REAUTH_ID_PREFIX;
  863. switch (method) {
  864. case EAP_SIM_DB_SIM:
  865. prefix = EAP_SIM_REAUTH_ID_PREFIX;
  866. break;
  867. case EAP_SIM_DB_AKA:
  868. prefix = EAP_AKA_REAUTH_ID_PREFIX;
  869. break;
  870. case EAP_SIM_DB_AKA_PRIME:
  871. prefix = EAP_AKA_PRIME_REAUTH_ID_PREFIX;
  872. break;
  873. }
  874. return eap_sim_db_get_next(data, prefix);
  875. }
  876. /**
  877. * eap_sim_db_add_pseudonym - EAP-SIM DB: Add new pseudonym
  878. * @data: Private data pointer from eap_sim_db_init()
  879. * @permanent: Permanent username
  880. * @pseudonym: Pseudonym for this user. This needs to be an allocated buffer,
  881. * e.g., return value from eap_sim_db_get_next_pseudonym(). Caller must not
  882. * free it.
  883. * Returns: 0 on success, -1 on failure
  884. *
  885. * This function adds a new pseudonym for EAP-SIM user. EAP-SIM DB is
  886. * responsible of freeing pseudonym buffer once it is not needed anymore.
  887. */
  888. int eap_sim_db_add_pseudonym(struct eap_sim_db_data *data,
  889. const char *permanent, char *pseudonym)
  890. {
  891. struct eap_sim_pseudonym *p;
  892. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add pseudonym '%s' for permanent "
  893. "username '%s'", pseudonym, permanent);
  894. /* TODO: could store last two pseudonyms */
  895. #ifdef CONFIG_SQLITE
  896. if (data->sqlite_db)
  897. return db_add_pseudonym(data, permanent, pseudonym);
  898. #endif /* CONFIG_SQLITE */
  899. for (p = data->pseudonyms; p; p = p->next) {
  900. if (os_strcmp(permanent, p->permanent) == 0)
  901. break;
  902. }
  903. if (p) {
  904. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous "
  905. "pseudonym: %s", p->pseudonym);
  906. os_free(p->pseudonym);
  907. p->pseudonym = pseudonym;
  908. return 0;
  909. }
  910. p = os_zalloc(sizeof(*p));
  911. if (p == NULL) {
  912. os_free(pseudonym);
  913. return -1;
  914. }
  915. p->next = data->pseudonyms;
  916. p->permanent = os_strdup(permanent);
  917. if (p->permanent == NULL) {
  918. os_free(p);
  919. os_free(pseudonym);
  920. return -1;
  921. }
  922. p->pseudonym = pseudonym;
  923. data->pseudonyms = p;
  924. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new pseudonym entry");
  925. return 0;
  926. }
  927. static struct eap_sim_reauth *
  928. eap_sim_db_add_reauth_data(struct eap_sim_db_data *data,
  929. const char *permanent,
  930. char *reauth_id, u16 counter)
  931. {
  932. struct eap_sim_reauth *r;
  933. for (r = data->reauths; r; r = r->next) {
  934. if (os_strcmp(r->permanent, permanent) == 0)
  935. break;
  936. }
  937. if (r) {
  938. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous "
  939. "reauth_id: %s", r->reauth_id);
  940. os_free(r->reauth_id);
  941. r->reauth_id = reauth_id;
  942. } else {
  943. r = os_zalloc(sizeof(*r));
  944. if (r == NULL) {
  945. os_free(reauth_id);
  946. return NULL;
  947. }
  948. r->next = data->reauths;
  949. r->permanent = os_strdup(permanent);
  950. if (r->permanent == NULL) {
  951. os_free(r);
  952. os_free(reauth_id);
  953. return NULL;
  954. }
  955. r->reauth_id = reauth_id;
  956. data->reauths = r;
  957. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new reauth entry");
  958. }
  959. r->counter = counter;
  960. return r;
  961. }
  962. /**
  963. * eap_sim_db_add_reauth - EAP-SIM DB: Add new re-authentication entry
  964. * @priv: Private data pointer from eap_sim_db_init()
  965. * @permanent: Permanent username
  966. * @identity_len: Length of identity
  967. * @reauth_id: reauth_id for this user. This needs to be an allocated buffer,
  968. * e.g., return value from eap_sim_db_get_next_reauth_id(). Caller must not
  969. * free it.
  970. * @counter: AT_COUNTER value for fast re-authentication
  971. * @mk: 16-byte MK from the previous full authentication or %NULL
  972. * Returns: 0 on success, -1 on failure
  973. *
  974. * This function adds a new re-authentication entry for an EAP-SIM user.
  975. * EAP-SIM DB is responsible of freeing reauth_id buffer once it is not needed
  976. * anymore.
  977. */
  978. int eap_sim_db_add_reauth(struct eap_sim_db_data *data, const char *permanent,
  979. char *reauth_id, u16 counter, const u8 *mk)
  980. {
  981. struct eap_sim_reauth *r;
  982. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add reauth_id '%s' for permanent "
  983. "identity '%s'", reauth_id, permanent);
  984. #ifdef CONFIG_SQLITE
  985. if (data->sqlite_db)
  986. return db_add_reauth(data, permanent, reauth_id, counter, mk,
  987. NULL, NULL, NULL);
  988. #endif /* CONFIG_SQLITE */
  989. r = eap_sim_db_add_reauth_data(data, permanent, reauth_id, counter);
  990. if (r == NULL)
  991. return -1;
  992. os_memcpy(r->mk, mk, EAP_SIM_MK_LEN);
  993. return 0;
  994. }
  995. #ifdef EAP_SERVER_AKA_PRIME
  996. /**
  997. * eap_sim_db_add_reauth_prime - EAP-AKA' DB: Add new re-authentication entry
  998. * @data: Private data pointer from eap_sim_db_init()
  999. * @permanent: Permanent username
  1000. * @reauth_id: reauth_id for this user. This needs to be an allocated buffer,
  1001. * e.g., return value from eap_sim_db_get_next_reauth_id(). Caller must not
  1002. * free it.
  1003. * @counter: AT_COUNTER value for fast re-authentication
  1004. * @k_encr: K_encr from the previous full authentication
  1005. * @k_aut: K_aut from the previous full authentication
  1006. * @k_re: 32-byte K_re from the previous full authentication
  1007. * Returns: 0 on success, -1 on failure
  1008. *
  1009. * This function adds a new re-authentication entry for an EAP-AKA' user.
  1010. * EAP-SIM DB is responsible of freeing reauth_id buffer once it is not needed
  1011. * anymore.
  1012. */
  1013. int eap_sim_db_add_reauth_prime(struct eap_sim_db_data *data,
  1014. const char *permanent, char *reauth_id,
  1015. u16 counter, const u8 *k_encr,
  1016. const u8 *k_aut, const u8 *k_re)
  1017. {
  1018. struct eap_sim_reauth *r;
  1019. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add reauth_id '%s' for permanent "
  1020. "identity '%s'", reauth_id, permanent);
  1021. #ifdef CONFIG_SQLITE
  1022. if (data->sqlite_db)
  1023. return db_add_reauth(data, permanent, reauth_id, counter, NULL,
  1024. k_encr, k_aut, k_re);
  1025. #endif /* CONFIG_SQLITE */
  1026. r = eap_sim_db_add_reauth_data(data, permanent, reauth_id, counter);
  1027. if (r == NULL)
  1028. return -1;
  1029. os_memcpy(r->k_encr, k_encr, EAP_SIM_K_ENCR_LEN);
  1030. os_memcpy(r->k_aut, k_aut, EAP_AKA_PRIME_K_AUT_LEN);
  1031. os_memcpy(r->k_re, k_re, EAP_AKA_PRIME_K_RE_LEN);
  1032. return 0;
  1033. }
  1034. #endif /* EAP_SERVER_AKA_PRIME */
  1035. /**
  1036. * eap_sim_db_get_permanent - EAP-SIM DB: Get permanent identity
  1037. * @data: Private data pointer from eap_sim_db_init()
  1038. * @pseudonym: Pseudonym username
  1039. * Returns: Pointer to permanent username or %NULL if not found
  1040. */
  1041. const char *
  1042. eap_sim_db_get_permanent(struct eap_sim_db_data *data, const char *pseudonym)
  1043. {
  1044. struct eap_sim_pseudonym *p;
  1045. #ifdef CONFIG_SQLITE
  1046. if (data->sqlite_db)
  1047. return db_get_pseudonym(data, pseudonym);
  1048. #endif /* CONFIG_SQLITE */
  1049. p = data->pseudonyms;
  1050. while (p) {
  1051. if (os_strcmp(p->pseudonym, pseudonym) == 0)
  1052. return p->permanent;
  1053. p = p->next;
  1054. }
  1055. return NULL;
  1056. }
  1057. /**
  1058. * eap_sim_db_get_reauth_entry - EAP-SIM DB: Get re-authentication entry
  1059. * @data: Private data pointer from eap_sim_db_init()
  1060. * @reauth_id: Fast re-authentication username
  1061. * Returns: Pointer to the re-auth entry, or %NULL if not found
  1062. */
  1063. struct eap_sim_reauth *
  1064. eap_sim_db_get_reauth_entry(struct eap_sim_db_data *data,
  1065. const char *reauth_id)
  1066. {
  1067. struct eap_sim_reauth *r;
  1068. #ifdef CONFIG_SQLITE
  1069. if (data->sqlite_db)
  1070. return db_get_reauth(data, reauth_id);
  1071. #endif /* CONFIG_SQLITE */
  1072. r = data->reauths;
  1073. while (r) {
  1074. if (os_strcmp(r->reauth_id, reauth_id) == 0)
  1075. break;
  1076. r = r->next;
  1077. }
  1078. return r;
  1079. }
  1080. /**
  1081. * eap_sim_db_remove_reauth - EAP-SIM DB: Remove re-authentication entry
  1082. * @data: Private data pointer from eap_sim_db_init()
  1083. * @reauth: Pointer to re-authentication entry from
  1084. * eap_sim_db_get_reauth_entry()
  1085. */
  1086. void eap_sim_db_remove_reauth(struct eap_sim_db_data *data,
  1087. struct eap_sim_reauth *reauth)
  1088. {
  1089. struct eap_sim_reauth *r, *prev = NULL;
  1090. #ifdef CONFIG_SQLITE
  1091. if (data->sqlite_db) {
  1092. db_remove_reauth(data, reauth);
  1093. return;
  1094. }
  1095. #endif /* CONFIG_SQLITE */
  1096. r = data->reauths;
  1097. while (r) {
  1098. if (r == reauth) {
  1099. if (prev)
  1100. prev->next = r->next;
  1101. else
  1102. data->reauths = r->next;
  1103. eap_sim_db_free_reauth(r);
  1104. return;
  1105. }
  1106. prev = r;
  1107. r = r->next;
  1108. }
  1109. }
  1110. /**
  1111. * eap_sim_db_get_aka_auth - Get AKA authentication values
  1112. * @data: Private data pointer from eap_sim_db_init()
  1113. * @username: Permanent username (prefix | IMSI)
  1114. * @_rand: Buffer for RAND value
  1115. * @autn: Buffer for AUTN value
  1116. * @ik: Buffer for IK value
  1117. * @ck: Buffer for CK value
  1118. * @res: Buffer for RES value
  1119. * @res_len: Buffer for RES length
  1120. * @cb_session_ctx: Session callback context for get_complete_cb()
  1121. * Returns: 0 on success, -1 (EAP_SIM_DB_FAILURE) on error (e.g., user not
  1122. * found), or -2 (EAP_SIM_DB_PENDING) if results are not yet available. In this
  1123. * case, the callback function registered with eap_sim_db_init() will be
  1124. * called once the results become available.
  1125. *
  1126. * When using an external server for AKA authentication, this function can
  1127. * always start a request and return EAP_SIM_DB_PENDING immediately if
  1128. * authentication triplets are not available. Once the authentication data are
  1129. * received, callback function registered with eap_sim_db_init() is called to
  1130. * notify EAP state machine to reprocess the message. This
  1131. * eap_sim_db_get_aka_auth() function will then be called again and the newly
  1132. * received triplets will then be given to the caller.
  1133. */
  1134. int eap_sim_db_get_aka_auth(struct eap_sim_db_data *data, const char *username,
  1135. u8 *_rand, u8 *autn, u8 *ik, u8 *ck,
  1136. u8 *res, size_t *res_len, void *cb_session_ctx)
  1137. {
  1138. struct eap_sim_db_pending *entry;
  1139. int len;
  1140. char msg[40];
  1141. const char *imsi;
  1142. size_t imsi_len;
  1143. if (username == NULL ||
  1144. (username[0] != EAP_AKA_PERMANENT_PREFIX &&
  1145. username[0] != EAP_AKA_PRIME_PERMANENT_PREFIX) ||
  1146. username[1] == '\0' || os_strlen(username) > sizeof(entry->imsi)) {
  1147. wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
  1148. username);
  1149. return EAP_SIM_DB_FAILURE;
  1150. }
  1151. imsi = username + 1;
  1152. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get AKA auth for IMSI '%s'",
  1153. imsi);
  1154. entry = eap_sim_db_get_pending(data, imsi, 1);
  1155. if (entry) {
  1156. if (entry->state == FAILURE) {
  1157. os_free(entry);
  1158. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failure");
  1159. return EAP_SIM_DB_FAILURE;
  1160. }
  1161. if (entry->state == PENDING) {
  1162. eap_sim_db_add_pending(data, entry);
  1163. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending");
  1164. return EAP_SIM_DB_PENDING;
  1165. }
  1166. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Returning successfully "
  1167. "received authentication data");
  1168. os_memcpy(_rand, entry->u.aka.rand, EAP_AKA_RAND_LEN);
  1169. os_memcpy(autn, entry->u.aka.autn, EAP_AKA_AUTN_LEN);
  1170. os_memcpy(ik, entry->u.aka.ik, EAP_AKA_IK_LEN);
  1171. os_memcpy(ck, entry->u.aka.ck, EAP_AKA_CK_LEN);
  1172. os_memcpy(res, entry->u.aka.res, EAP_AKA_RES_MAX_LEN);
  1173. *res_len = entry->u.aka.res_len;
  1174. os_free(entry);
  1175. return 0;
  1176. }
  1177. if (data->sock < 0) {
  1178. if (eap_sim_db_open_socket(data) < 0)
  1179. return EAP_SIM_DB_FAILURE;
  1180. }
  1181. imsi_len = os_strlen(imsi);
  1182. len = os_snprintf(msg, sizeof(msg), "AKA-REQ-AUTH ");
  1183. if (len < 0 || len + imsi_len >= sizeof(msg))
  1184. return EAP_SIM_DB_FAILURE;
  1185. os_memcpy(msg + len, imsi, imsi_len);
  1186. len += imsi_len;
  1187. wpa_printf(MSG_DEBUG, "EAP-SIM DB: requesting AKA authentication "
  1188. "data for IMSI '%s'", imsi);
  1189. if (eap_sim_db_send(data, msg, len) < 0)
  1190. return EAP_SIM_DB_FAILURE;
  1191. entry = os_zalloc(sizeof(*entry));
  1192. if (entry == NULL)
  1193. return EAP_SIM_DB_FAILURE;
  1194. os_get_time(&entry->timestamp);
  1195. entry->aka = 1;
  1196. os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
  1197. entry->cb_session_ctx = cb_session_ctx;
  1198. entry->state = PENDING;
  1199. eap_sim_db_add_pending(data, entry);
  1200. eap_sim_db_expire_pending(data);
  1201. return EAP_SIM_DB_PENDING;
  1202. }
  1203. /**
  1204. * eap_sim_db_resynchronize - Resynchronize AKA AUTN
  1205. * @data: Private data pointer from eap_sim_db_init()
  1206. * @username: Permanent username
  1207. * @auts: AUTS value from the peer
  1208. * @_rand: RAND value used in the rejected message
  1209. * Returns: 0 on success, -1 on failure
  1210. *
  1211. * This function is called when the peer reports synchronization failure in the
  1212. * AUTN value by sending AUTS. The AUTS and RAND values should be sent to
  1213. * HLR/AuC to allow it to resynchronize with the peer. After this,
  1214. * eap_sim_db_get_aka_auth() will be called again to to fetch updated
  1215. * RAND/AUTN values for the next challenge.
  1216. */
  1217. int eap_sim_db_resynchronize(struct eap_sim_db_data *data,
  1218. const char *username,
  1219. const u8 *auts, const u8 *_rand)
  1220. {
  1221. const char *imsi;
  1222. size_t imsi_len;
  1223. if (username == NULL ||
  1224. (username[0] != EAP_AKA_PERMANENT_PREFIX &&
  1225. username[0] != EAP_AKA_PRIME_PERMANENT_PREFIX) ||
  1226. username[1] == '\0' || os_strlen(username) > 20) {
  1227. wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
  1228. username);
  1229. return -1;
  1230. }
  1231. imsi = username + 1;
  1232. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get AKA auth for IMSI '%s'",
  1233. imsi);
  1234. if (data->sock >= 0) {
  1235. char msg[100];
  1236. int len, ret;
  1237. imsi_len = os_strlen(imsi);
  1238. len = os_snprintf(msg, sizeof(msg), "AKA-AUTS ");
  1239. if (len < 0 || len + imsi_len >= sizeof(msg))
  1240. return -1;
  1241. os_memcpy(msg + len, imsi, imsi_len);
  1242. len += imsi_len;
  1243. ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
  1244. if (ret < 0 || (size_t) ret >= sizeof(msg) - len)
  1245. return -1;
  1246. len += ret;
  1247. len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
  1248. auts, EAP_AKA_AUTS_LEN);
  1249. ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
  1250. if (ret < 0 || (size_t) ret >= sizeof(msg) - len)
  1251. return -1;
  1252. len += ret;
  1253. len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
  1254. _rand, EAP_AKA_RAND_LEN);
  1255. wpa_printf(MSG_DEBUG, "EAP-SIM DB: reporting AKA AUTS for "
  1256. "IMSI '%s'", imsi);
  1257. if (eap_sim_db_send(data, msg, len) < 0)
  1258. return -1;
  1259. }
  1260. return 0;
  1261. }
  1262. /**
  1263. * sim_get_username - Extract username from SIM identity
  1264. * @identity: Identity
  1265. * @identity_len: Identity length
  1266. * Returns: Allocated buffer with the username part of the identity
  1267. *
  1268. * Caller is responsible for freeing the returned buffer with os_free().
  1269. */
  1270. char * sim_get_username(const u8 *identity, size_t identity_len)
  1271. {
  1272. size_t pos;
  1273. if (identity == NULL)
  1274. return NULL;
  1275. for (pos = 0; pos < identity_len; pos++) {
  1276. if (identity[pos] == '@' || identity[pos] == '\0')
  1277. break;
  1278. }
  1279. return dup_binstr(identity, pos);
  1280. }