eap_sim_db.c 39 KB

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