eap_sim_db.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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) - 1, 0);
  487. if (res < 0)
  488. return;
  489. buf[res] = '\0';
  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 (data->get_complete_cb == NULL) {
  495. wpa_printf(MSG_DEBUG, "EAP-SIM DB: No get_complete_cb "
  496. "registered");
  497. return;
  498. }
  499. /* <cmd> <IMSI> ... */
  500. cmd = buf;
  501. pos = os_strchr(cmd, ' ');
  502. if (pos == NULL)
  503. goto parse_fail;
  504. *pos = '\0';
  505. imsi = pos + 1;
  506. pos = os_strchr(imsi, ' ');
  507. if (pos == NULL)
  508. goto parse_fail;
  509. *pos = '\0';
  510. wpa_printf(MSG_DEBUG, "EAP-SIM DB: External response=%s for IMSI %s",
  511. cmd, imsi);
  512. if (os_strcmp(cmd, "SIM-RESP-AUTH") == 0)
  513. eap_sim_db_sim_resp_auth(data, imsi, pos + 1);
  514. else if (os_strcmp(cmd, "AKA-RESP-AUTH") == 0)
  515. eap_sim_db_aka_resp_auth(data, imsi, pos + 1);
  516. else
  517. wpa_printf(MSG_INFO, "EAP-SIM DB: Unknown external response "
  518. "'%s'", cmd);
  519. return;
  520. parse_fail:
  521. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failed to parse response string");
  522. }
  523. static int eap_sim_db_open_socket(struct eap_sim_db_data *data)
  524. {
  525. struct sockaddr_un addr;
  526. static int counter = 0;
  527. if (os_strncmp(data->fname, "unix:", 5) != 0)
  528. return -1;
  529. data->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  530. if (data->sock < 0) {
  531. wpa_printf(MSG_INFO, "socket(eap_sim_db): %s", strerror(errno));
  532. return -1;
  533. }
  534. os_memset(&addr, 0, sizeof(addr));
  535. addr.sun_family = AF_UNIX;
  536. os_snprintf(addr.sun_path, sizeof(addr.sun_path),
  537. "/tmp/eap_sim_db_%d-%d", getpid(), counter++);
  538. os_free(data->local_sock);
  539. data->local_sock = os_strdup(addr.sun_path);
  540. if (data->local_sock == NULL) {
  541. close(data->sock);
  542. data->sock = -1;
  543. return -1;
  544. }
  545. if (bind(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  546. wpa_printf(MSG_INFO, "bind(eap_sim_db): %s", strerror(errno));
  547. close(data->sock);
  548. data->sock = -1;
  549. return -1;
  550. }
  551. os_memset(&addr, 0, sizeof(addr));
  552. addr.sun_family = AF_UNIX;
  553. os_strlcpy(addr.sun_path, data->fname + 5, sizeof(addr.sun_path));
  554. if (connect(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  555. wpa_printf(MSG_INFO, "connect(eap_sim_db): %s",
  556. strerror(errno));
  557. wpa_hexdump_ascii(MSG_INFO, "HLR/AuC GW socket",
  558. (u8 *) addr.sun_path,
  559. os_strlen(addr.sun_path));
  560. close(data->sock);
  561. data->sock = -1;
  562. unlink(data->local_sock);
  563. os_free(data->local_sock);
  564. data->local_sock = NULL;
  565. return -1;
  566. }
  567. eloop_register_read_sock(data->sock, eap_sim_db_receive, data, NULL);
  568. return 0;
  569. }
  570. static void eap_sim_db_close_socket(struct eap_sim_db_data *data)
  571. {
  572. if (data->sock >= 0) {
  573. eloop_unregister_read_sock(data->sock);
  574. close(data->sock);
  575. data->sock = -1;
  576. }
  577. if (data->local_sock) {
  578. unlink(data->local_sock);
  579. os_free(data->local_sock);
  580. data->local_sock = NULL;
  581. }
  582. }
  583. /**
  584. * eap_sim_db_init - Initialize EAP-SIM DB / authentication gateway interface
  585. * @config: Configuration data (e.g., file name)
  586. * @get_complete_cb: Callback function for reporting availability of triplets
  587. * @ctx: Context pointer for get_complete_cb
  588. * Returns: Pointer to a private data structure or %NULL on failure
  589. */
  590. struct eap_sim_db_data *
  591. eap_sim_db_init(const char *config,
  592. void (*get_complete_cb)(void *ctx, void *session_ctx),
  593. void *ctx)
  594. {
  595. struct eap_sim_db_data *data;
  596. char *pos;
  597. data = os_zalloc(sizeof(*data));
  598. if (data == NULL)
  599. return NULL;
  600. data->sock = -1;
  601. data->get_complete_cb = get_complete_cb;
  602. data->ctx = ctx;
  603. data->fname = os_strdup(config);
  604. if (data->fname == NULL)
  605. goto fail;
  606. pos = os_strstr(data->fname, " db=");
  607. if (pos) {
  608. *pos = '\0';
  609. #ifdef CONFIG_SQLITE
  610. pos += 4;
  611. data->sqlite_db = db_open(pos);
  612. if (data->sqlite_db == NULL)
  613. goto fail;
  614. #endif /* CONFIG_SQLITE */
  615. }
  616. if (os_strncmp(data->fname, "unix:", 5) == 0) {
  617. if (eap_sim_db_open_socket(data)) {
  618. wpa_printf(MSG_DEBUG, "EAP-SIM DB: External database "
  619. "connection not available - will retry "
  620. "later");
  621. }
  622. }
  623. return data;
  624. fail:
  625. eap_sim_db_close_socket(data);
  626. os_free(data->fname);
  627. os_free(data);
  628. return NULL;
  629. }
  630. static void eap_sim_db_free_pseudonym(struct eap_sim_pseudonym *p)
  631. {
  632. os_free(p->permanent);
  633. os_free(p->pseudonym);
  634. os_free(p);
  635. }
  636. static void eap_sim_db_free_reauth(struct eap_sim_reauth *r)
  637. {
  638. os_free(r->permanent);
  639. os_free(r->reauth_id);
  640. os_free(r);
  641. }
  642. /**
  643. * eap_sim_db_deinit - Deinitialize EAP-SIM DB/authentication gw interface
  644. * @priv: Private data pointer from eap_sim_db_init()
  645. */
  646. void eap_sim_db_deinit(void *priv)
  647. {
  648. struct eap_sim_db_data *data = priv;
  649. struct eap_sim_pseudonym *p, *prev;
  650. struct eap_sim_reauth *r, *prevr;
  651. struct eap_sim_db_pending *pending, *prev_pending;
  652. #ifdef CONFIG_SQLITE
  653. if (data->sqlite_db) {
  654. sqlite3_close(data->sqlite_db);
  655. data->sqlite_db = NULL;
  656. }
  657. #endif /* CONFIG_SQLITE */
  658. eap_sim_db_close_socket(data);
  659. os_free(data->fname);
  660. p = data->pseudonyms;
  661. while (p) {
  662. prev = p;
  663. p = p->next;
  664. eap_sim_db_free_pseudonym(prev);
  665. }
  666. r = data->reauths;
  667. while (r) {
  668. prevr = r;
  669. r = r->next;
  670. eap_sim_db_free_reauth(prevr);
  671. }
  672. pending = data->pending;
  673. while (pending) {
  674. prev_pending = pending;
  675. pending = pending->next;
  676. os_free(prev_pending);
  677. }
  678. os_free(data);
  679. }
  680. static int eap_sim_db_send(struct eap_sim_db_data *data, const char *msg,
  681. size_t len)
  682. {
  683. int _errno = 0;
  684. if (send(data->sock, msg, len, 0) < 0) {
  685. _errno = errno;
  686. wpa_printf(MSG_INFO, "send[EAP-SIM DB UNIX]: %s",
  687. strerror(errno));
  688. }
  689. if (_errno == ENOTCONN || _errno == EDESTADDRREQ || _errno == EINVAL ||
  690. _errno == ECONNREFUSED) {
  691. /* Try to reconnect */
  692. eap_sim_db_close_socket(data);
  693. if (eap_sim_db_open_socket(data) < 0)
  694. return -1;
  695. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Reconnected to the "
  696. "external server");
  697. if (send(data->sock, msg, len, 0) < 0) {
  698. wpa_printf(MSG_INFO, "send[EAP-SIM DB UNIX]: %s",
  699. strerror(errno));
  700. return -1;
  701. }
  702. }
  703. return 0;
  704. }
  705. static void eap_sim_db_expire_pending(struct eap_sim_db_data *data)
  706. {
  707. /* TODO: add limit for maximum length for pending list; remove latest
  708. * (i.e., last) entry from the list if the limit is reached; could also
  709. * use timeout to expire pending entries */
  710. }
  711. /**
  712. * eap_sim_db_get_gsm_triplets - Get GSM triplets
  713. * @data: Private data pointer from eap_sim_db_init()
  714. * @username: Permanent username (prefix | IMSI)
  715. * @max_chal: Maximum number of triplets
  716. * @_rand: Buffer for RAND values
  717. * @kc: Buffer for Kc values
  718. * @sres: Buffer for SRES values
  719. * @cb_session_ctx: Session callback context for get_complete_cb()
  720. * Returns: Number of triplets received (has to be less than or equal to
  721. * max_chal), -1 (EAP_SIM_DB_FAILURE) on error (e.g., user not found), or
  722. * -2 (EAP_SIM_DB_PENDING) if results are not yet available. In this case, the
  723. * callback function registered with eap_sim_db_init() will be called once the
  724. * results become available.
  725. *
  726. * When using an external server for GSM triplets, this function can always
  727. * start a request and return EAP_SIM_DB_PENDING immediately if authentication
  728. * triplets are not available. Once the triplets are received, callback
  729. * function registered with eap_sim_db_init() is called to notify EAP state
  730. * machine to reprocess the message. This eap_sim_db_get_gsm_triplets()
  731. * function will then be called again and the newly received triplets will then
  732. * be given to the caller.
  733. */
  734. int eap_sim_db_get_gsm_triplets(struct eap_sim_db_data *data,
  735. const char *username, int max_chal,
  736. u8 *_rand, u8 *kc, u8 *sres,
  737. void *cb_session_ctx)
  738. {
  739. struct eap_sim_db_pending *entry;
  740. int len, ret;
  741. char msg[40];
  742. const char *imsi;
  743. size_t imsi_len;
  744. if (username == NULL || username[0] != EAP_SIM_PERMANENT_PREFIX ||
  745. username[1] == '\0' || os_strlen(username) > sizeof(entry->imsi)) {
  746. wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
  747. username);
  748. return EAP_SIM_DB_FAILURE;
  749. }
  750. imsi = username + 1;
  751. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get GSM triplets for IMSI '%s'",
  752. imsi);
  753. entry = eap_sim_db_get_pending(data, imsi, 0);
  754. if (entry) {
  755. int num_chal;
  756. if (entry->state == FAILURE) {
  757. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
  758. "failure");
  759. os_free(entry);
  760. return EAP_SIM_DB_FAILURE;
  761. }
  762. if (entry->state == PENDING) {
  763. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
  764. "still pending");
  765. eap_sim_db_add_pending(data, entry);
  766. return EAP_SIM_DB_PENDING;
  767. }
  768. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending entry -> "
  769. "%d challenges", entry->u.sim.num_chal);
  770. num_chal = entry->u.sim.num_chal;
  771. if (num_chal > max_chal)
  772. num_chal = max_chal;
  773. os_memcpy(_rand, entry->u.sim.rand, num_chal * GSM_RAND_LEN);
  774. os_memcpy(sres, entry->u.sim.sres,
  775. num_chal * EAP_SIM_SRES_LEN);
  776. os_memcpy(kc, entry->u.sim.kc, num_chal * EAP_SIM_KC_LEN);
  777. os_free(entry);
  778. return num_chal;
  779. }
  780. if (data->sock < 0) {
  781. if (eap_sim_db_open_socket(data) < 0)
  782. return EAP_SIM_DB_FAILURE;
  783. }
  784. imsi_len = os_strlen(imsi);
  785. len = os_snprintf(msg, sizeof(msg), "SIM-REQ-AUTH ");
  786. if (os_snprintf_error(sizeof(msg), len) ||
  787. len + imsi_len >= sizeof(msg))
  788. return EAP_SIM_DB_FAILURE;
  789. os_memcpy(msg + len, imsi, imsi_len);
  790. len += imsi_len;
  791. ret = os_snprintf(msg + len, sizeof(msg) - len, " %d", max_chal);
  792. if (os_snprintf_error(sizeof(msg) - len, ret))
  793. return EAP_SIM_DB_FAILURE;
  794. len += ret;
  795. wpa_printf(MSG_DEBUG, "EAP-SIM DB: requesting SIM authentication "
  796. "data for IMSI '%s'", imsi);
  797. if (eap_sim_db_send(data, msg, len) < 0)
  798. return EAP_SIM_DB_FAILURE;
  799. entry = os_zalloc(sizeof(*entry));
  800. if (entry == NULL)
  801. return EAP_SIM_DB_FAILURE;
  802. os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
  803. entry->cb_session_ctx = cb_session_ctx;
  804. entry->state = PENDING;
  805. eap_sim_db_add_pending(data, entry);
  806. eap_sim_db_expire_pending(data);
  807. return EAP_SIM_DB_PENDING;
  808. }
  809. static char * eap_sim_db_get_next(struct eap_sim_db_data *data, char prefix)
  810. {
  811. char *id, *pos, *end;
  812. u8 buf[10];
  813. if (random_get_bytes(buf, sizeof(buf)))
  814. return NULL;
  815. id = os_malloc(sizeof(buf) * 2 + 2);
  816. if (id == NULL)
  817. return NULL;
  818. pos = id;
  819. end = id + sizeof(buf) * 2 + 2;
  820. *pos++ = prefix;
  821. wpa_snprintf_hex(pos, end - pos, buf, sizeof(buf));
  822. return id;
  823. }
  824. /**
  825. * eap_sim_db_get_next_pseudonym - EAP-SIM DB: Get next pseudonym
  826. * @data: Private data pointer from eap_sim_db_init()
  827. * @method: EAP method (SIM/AKA/AKA')
  828. * Returns: Next pseudonym (allocated string) or %NULL on failure
  829. *
  830. * This function is used to generate a pseudonym for EAP-SIM. The returned
  831. * pseudonym is not added to database at this point; it will need to be added
  832. * with eap_sim_db_add_pseudonym() once the authentication has been completed
  833. * successfully. Caller is responsible for freeing the returned buffer.
  834. */
  835. char * eap_sim_db_get_next_pseudonym(struct eap_sim_db_data *data,
  836. enum eap_sim_db_method method)
  837. {
  838. char prefix = EAP_SIM_REAUTH_ID_PREFIX;
  839. switch (method) {
  840. case EAP_SIM_DB_SIM:
  841. prefix = EAP_SIM_PSEUDONYM_PREFIX;
  842. break;
  843. case EAP_SIM_DB_AKA:
  844. prefix = EAP_AKA_PSEUDONYM_PREFIX;
  845. break;
  846. case EAP_SIM_DB_AKA_PRIME:
  847. prefix = EAP_AKA_PRIME_PSEUDONYM_PREFIX;
  848. break;
  849. }
  850. return eap_sim_db_get_next(data, prefix);
  851. }
  852. /**
  853. * eap_sim_db_get_next_reauth_id - EAP-SIM DB: Get next reauth_id
  854. * @data: Private data pointer from eap_sim_db_init()
  855. * @method: EAP method (SIM/AKA/AKA')
  856. * Returns: Next reauth_id (allocated string) or %NULL on failure
  857. *
  858. * This function is used to generate a fast re-authentication identity for
  859. * EAP-SIM. The returned reauth_id is not added to database at this point; it
  860. * will need to be added with eap_sim_db_add_reauth() once the authentication
  861. * has been completed successfully. Caller is responsible for freeing the
  862. * returned buffer.
  863. */
  864. char * eap_sim_db_get_next_reauth_id(struct eap_sim_db_data *data,
  865. enum eap_sim_db_method method)
  866. {
  867. char prefix = EAP_SIM_REAUTH_ID_PREFIX;
  868. switch (method) {
  869. case EAP_SIM_DB_SIM:
  870. prefix = EAP_SIM_REAUTH_ID_PREFIX;
  871. break;
  872. case EAP_SIM_DB_AKA:
  873. prefix = EAP_AKA_REAUTH_ID_PREFIX;
  874. break;
  875. case EAP_SIM_DB_AKA_PRIME:
  876. prefix = EAP_AKA_PRIME_REAUTH_ID_PREFIX;
  877. break;
  878. }
  879. return eap_sim_db_get_next(data, prefix);
  880. }
  881. /**
  882. * eap_sim_db_add_pseudonym - EAP-SIM DB: Add new pseudonym
  883. * @data: Private data pointer from eap_sim_db_init()
  884. * @permanent: Permanent username
  885. * @pseudonym: Pseudonym for this user. This needs to be an allocated buffer,
  886. * e.g., return value from eap_sim_db_get_next_pseudonym(). Caller must not
  887. * free it.
  888. * Returns: 0 on success, -1 on failure
  889. *
  890. * This function adds a new pseudonym for EAP-SIM user. EAP-SIM DB is
  891. * responsible of freeing pseudonym buffer once it is not needed anymore.
  892. */
  893. int eap_sim_db_add_pseudonym(struct eap_sim_db_data *data,
  894. const char *permanent, char *pseudonym)
  895. {
  896. struct eap_sim_pseudonym *p;
  897. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add pseudonym '%s' for permanent "
  898. "username '%s'", pseudonym, permanent);
  899. /* TODO: could store last two pseudonyms */
  900. #ifdef CONFIG_SQLITE
  901. if (data->sqlite_db)
  902. return db_add_pseudonym(data, permanent, pseudonym);
  903. #endif /* CONFIG_SQLITE */
  904. for (p = data->pseudonyms; p; p = p->next) {
  905. if (os_strcmp(permanent, p->permanent) == 0)
  906. break;
  907. }
  908. if (p) {
  909. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous "
  910. "pseudonym: %s", p->pseudonym);
  911. os_free(p->pseudonym);
  912. p->pseudonym = pseudonym;
  913. return 0;
  914. }
  915. p = os_zalloc(sizeof(*p));
  916. if (p == NULL) {
  917. os_free(pseudonym);
  918. return -1;
  919. }
  920. p->next = data->pseudonyms;
  921. p->permanent = os_strdup(permanent);
  922. if (p->permanent == NULL) {
  923. os_free(p);
  924. os_free(pseudonym);
  925. return -1;
  926. }
  927. p->pseudonym = pseudonym;
  928. data->pseudonyms = p;
  929. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new pseudonym entry");
  930. return 0;
  931. }
  932. static struct eap_sim_reauth *
  933. eap_sim_db_add_reauth_data(struct eap_sim_db_data *data,
  934. const char *permanent,
  935. char *reauth_id, u16 counter)
  936. {
  937. struct eap_sim_reauth *r;
  938. for (r = data->reauths; r; r = r->next) {
  939. if (os_strcmp(r->permanent, permanent) == 0)
  940. break;
  941. }
  942. if (r) {
  943. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Replacing previous "
  944. "reauth_id: %s", r->reauth_id);
  945. os_free(r->reauth_id);
  946. r->reauth_id = reauth_id;
  947. } else {
  948. r = os_zalloc(sizeof(*r));
  949. if (r == NULL) {
  950. os_free(reauth_id);
  951. return NULL;
  952. }
  953. r->next = data->reauths;
  954. r->permanent = os_strdup(permanent);
  955. if (r->permanent == NULL) {
  956. os_free(r);
  957. os_free(reauth_id);
  958. return NULL;
  959. }
  960. r->reauth_id = reauth_id;
  961. data->reauths = r;
  962. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Added new reauth entry");
  963. }
  964. r->counter = counter;
  965. return r;
  966. }
  967. /**
  968. * eap_sim_db_add_reauth - EAP-SIM DB: Add new re-authentication entry
  969. * @priv: Private data pointer from eap_sim_db_init()
  970. * @permanent: Permanent username
  971. * @identity_len: Length of identity
  972. * @reauth_id: reauth_id for this user. This needs to be an allocated buffer,
  973. * e.g., return value from eap_sim_db_get_next_reauth_id(). Caller must not
  974. * free it.
  975. * @counter: AT_COUNTER value for fast re-authentication
  976. * @mk: 16-byte MK from the previous full authentication or %NULL
  977. * Returns: 0 on success, -1 on failure
  978. *
  979. * This function adds a new re-authentication entry for an EAP-SIM user.
  980. * EAP-SIM DB is responsible of freeing reauth_id buffer once it is not needed
  981. * anymore.
  982. */
  983. int eap_sim_db_add_reauth(struct eap_sim_db_data *data, const char *permanent,
  984. char *reauth_id, u16 counter, const u8 *mk)
  985. {
  986. struct eap_sim_reauth *r;
  987. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add reauth_id '%s' for permanent "
  988. "identity '%s'", reauth_id, permanent);
  989. #ifdef CONFIG_SQLITE
  990. if (data->sqlite_db)
  991. return db_add_reauth(data, permanent, reauth_id, counter, mk,
  992. NULL, NULL, NULL);
  993. #endif /* CONFIG_SQLITE */
  994. r = eap_sim_db_add_reauth_data(data, permanent, reauth_id, counter);
  995. if (r == NULL)
  996. return -1;
  997. os_memcpy(r->mk, mk, EAP_SIM_MK_LEN);
  998. return 0;
  999. }
  1000. #ifdef EAP_SERVER_AKA_PRIME
  1001. /**
  1002. * eap_sim_db_add_reauth_prime - EAP-AKA' DB: Add new re-authentication entry
  1003. * @data: Private data pointer from eap_sim_db_init()
  1004. * @permanent: Permanent username
  1005. * @reauth_id: reauth_id for this user. This needs to be an allocated buffer,
  1006. * e.g., return value from eap_sim_db_get_next_reauth_id(). Caller must not
  1007. * free it.
  1008. * @counter: AT_COUNTER value for fast re-authentication
  1009. * @k_encr: K_encr from the previous full authentication
  1010. * @k_aut: K_aut from the previous full authentication
  1011. * @k_re: 32-byte K_re from the previous full authentication
  1012. * Returns: 0 on success, -1 on failure
  1013. *
  1014. * This function adds a new re-authentication entry for an EAP-AKA' user.
  1015. * EAP-SIM DB is responsible of freeing reauth_id buffer once it is not needed
  1016. * anymore.
  1017. */
  1018. int eap_sim_db_add_reauth_prime(struct eap_sim_db_data *data,
  1019. const char *permanent, char *reauth_id,
  1020. u16 counter, const u8 *k_encr,
  1021. const u8 *k_aut, const u8 *k_re)
  1022. {
  1023. struct eap_sim_reauth *r;
  1024. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Add reauth_id '%s' for permanent "
  1025. "identity '%s'", reauth_id, permanent);
  1026. #ifdef CONFIG_SQLITE
  1027. if (data->sqlite_db)
  1028. return db_add_reauth(data, permanent, reauth_id, counter, NULL,
  1029. k_encr, k_aut, k_re);
  1030. #endif /* CONFIG_SQLITE */
  1031. r = eap_sim_db_add_reauth_data(data, permanent, reauth_id, counter);
  1032. if (r == NULL)
  1033. return -1;
  1034. os_memcpy(r->k_encr, k_encr, EAP_SIM_K_ENCR_LEN);
  1035. os_memcpy(r->k_aut, k_aut, EAP_AKA_PRIME_K_AUT_LEN);
  1036. os_memcpy(r->k_re, k_re, EAP_AKA_PRIME_K_RE_LEN);
  1037. return 0;
  1038. }
  1039. #endif /* EAP_SERVER_AKA_PRIME */
  1040. /**
  1041. * eap_sim_db_get_permanent - EAP-SIM DB: Get permanent identity
  1042. * @data: Private data pointer from eap_sim_db_init()
  1043. * @pseudonym: Pseudonym username
  1044. * Returns: Pointer to permanent username or %NULL if not found
  1045. */
  1046. const char *
  1047. eap_sim_db_get_permanent(struct eap_sim_db_data *data, const char *pseudonym)
  1048. {
  1049. struct eap_sim_pseudonym *p;
  1050. #ifdef CONFIG_SQLITE
  1051. if (data->sqlite_db)
  1052. return db_get_pseudonym(data, pseudonym);
  1053. #endif /* CONFIG_SQLITE */
  1054. p = data->pseudonyms;
  1055. while (p) {
  1056. if (os_strcmp(p->pseudonym, pseudonym) == 0)
  1057. return p->permanent;
  1058. p = p->next;
  1059. }
  1060. return NULL;
  1061. }
  1062. /**
  1063. * eap_sim_db_get_reauth_entry - EAP-SIM DB: Get re-authentication entry
  1064. * @data: Private data pointer from eap_sim_db_init()
  1065. * @reauth_id: Fast re-authentication username
  1066. * Returns: Pointer to the re-auth entry, or %NULL if not found
  1067. */
  1068. struct eap_sim_reauth *
  1069. eap_sim_db_get_reauth_entry(struct eap_sim_db_data *data,
  1070. const char *reauth_id)
  1071. {
  1072. struct eap_sim_reauth *r;
  1073. #ifdef CONFIG_SQLITE
  1074. if (data->sqlite_db)
  1075. return db_get_reauth(data, reauth_id);
  1076. #endif /* CONFIG_SQLITE */
  1077. r = data->reauths;
  1078. while (r) {
  1079. if (os_strcmp(r->reauth_id, reauth_id) == 0)
  1080. break;
  1081. r = r->next;
  1082. }
  1083. return r;
  1084. }
  1085. /**
  1086. * eap_sim_db_remove_reauth - EAP-SIM DB: Remove re-authentication entry
  1087. * @data: Private data pointer from eap_sim_db_init()
  1088. * @reauth: Pointer to re-authentication entry from
  1089. * eap_sim_db_get_reauth_entry()
  1090. */
  1091. void eap_sim_db_remove_reauth(struct eap_sim_db_data *data,
  1092. struct eap_sim_reauth *reauth)
  1093. {
  1094. struct eap_sim_reauth *r, *prev = NULL;
  1095. #ifdef CONFIG_SQLITE
  1096. if (data->sqlite_db) {
  1097. db_remove_reauth(data, reauth);
  1098. return;
  1099. }
  1100. #endif /* CONFIG_SQLITE */
  1101. r = data->reauths;
  1102. while (r) {
  1103. if (r == reauth) {
  1104. if (prev)
  1105. prev->next = r->next;
  1106. else
  1107. data->reauths = r->next;
  1108. eap_sim_db_free_reauth(r);
  1109. return;
  1110. }
  1111. prev = r;
  1112. r = r->next;
  1113. }
  1114. }
  1115. /**
  1116. * eap_sim_db_get_aka_auth - Get AKA authentication values
  1117. * @data: Private data pointer from eap_sim_db_init()
  1118. * @username: Permanent username (prefix | IMSI)
  1119. * @_rand: Buffer for RAND value
  1120. * @autn: Buffer for AUTN value
  1121. * @ik: Buffer for IK value
  1122. * @ck: Buffer for CK value
  1123. * @res: Buffer for RES value
  1124. * @res_len: Buffer for RES length
  1125. * @cb_session_ctx: Session callback context for get_complete_cb()
  1126. * Returns: 0 on success, -1 (EAP_SIM_DB_FAILURE) on error (e.g., user not
  1127. * found), or -2 (EAP_SIM_DB_PENDING) if results are not yet available. In this
  1128. * case, the callback function registered with eap_sim_db_init() will be
  1129. * called once the results become available.
  1130. *
  1131. * When using an external server for AKA authentication, this function can
  1132. * always start a request and return EAP_SIM_DB_PENDING immediately if
  1133. * authentication triplets are not available. Once the authentication data are
  1134. * received, callback function registered with eap_sim_db_init() is called to
  1135. * notify EAP state machine to reprocess the message. This
  1136. * eap_sim_db_get_aka_auth() function will then be called again and the newly
  1137. * received triplets will then be given to the caller.
  1138. */
  1139. int eap_sim_db_get_aka_auth(struct eap_sim_db_data *data, const char *username,
  1140. u8 *_rand, u8 *autn, u8 *ik, u8 *ck,
  1141. u8 *res, size_t *res_len, void *cb_session_ctx)
  1142. {
  1143. struct eap_sim_db_pending *entry;
  1144. int len;
  1145. char msg[40];
  1146. const char *imsi;
  1147. size_t imsi_len;
  1148. if (username == NULL ||
  1149. (username[0] != EAP_AKA_PERMANENT_PREFIX &&
  1150. username[0] != EAP_AKA_PRIME_PERMANENT_PREFIX) ||
  1151. username[1] == '\0' || os_strlen(username) > sizeof(entry->imsi)) {
  1152. wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
  1153. username);
  1154. return EAP_SIM_DB_FAILURE;
  1155. }
  1156. imsi = username + 1;
  1157. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get AKA auth for IMSI '%s'",
  1158. imsi);
  1159. entry = eap_sim_db_get_pending(data, imsi, 1);
  1160. if (entry) {
  1161. if (entry->state == FAILURE) {
  1162. os_free(entry);
  1163. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Failure");
  1164. return EAP_SIM_DB_FAILURE;
  1165. }
  1166. if (entry->state == PENDING) {
  1167. eap_sim_db_add_pending(data, entry);
  1168. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Pending");
  1169. return EAP_SIM_DB_PENDING;
  1170. }
  1171. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Returning successfully "
  1172. "received authentication data");
  1173. os_memcpy(_rand, entry->u.aka.rand, EAP_AKA_RAND_LEN);
  1174. os_memcpy(autn, entry->u.aka.autn, EAP_AKA_AUTN_LEN);
  1175. os_memcpy(ik, entry->u.aka.ik, EAP_AKA_IK_LEN);
  1176. os_memcpy(ck, entry->u.aka.ck, EAP_AKA_CK_LEN);
  1177. os_memcpy(res, entry->u.aka.res, EAP_AKA_RES_MAX_LEN);
  1178. *res_len = entry->u.aka.res_len;
  1179. os_free(entry);
  1180. return 0;
  1181. }
  1182. if (data->sock < 0) {
  1183. if (eap_sim_db_open_socket(data) < 0)
  1184. return EAP_SIM_DB_FAILURE;
  1185. }
  1186. imsi_len = os_strlen(imsi);
  1187. len = os_snprintf(msg, sizeof(msg), "AKA-REQ-AUTH ");
  1188. if (os_snprintf_error(sizeof(msg), len) ||
  1189. len + imsi_len >= sizeof(msg))
  1190. return EAP_SIM_DB_FAILURE;
  1191. os_memcpy(msg + len, imsi, imsi_len);
  1192. len += imsi_len;
  1193. wpa_printf(MSG_DEBUG, "EAP-SIM DB: requesting AKA authentication "
  1194. "data for IMSI '%s'", imsi);
  1195. if (eap_sim_db_send(data, msg, len) < 0)
  1196. return EAP_SIM_DB_FAILURE;
  1197. entry = os_zalloc(sizeof(*entry));
  1198. if (entry == NULL)
  1199. return EAP_SIM_DB_FAILURE;
  1200. entry->aka = 1;
  1201. os_strlcpy(entry->imsi, imsi, sizeof(entry->imsi));
  1202. entry->cb_session_ctx = cb_session_ctx;
  1203. entry->state = PENDING;
  1204. eap_sim_db_add_pending(data, entry);
  1205. eap_sim_db_expire_pending(data);
  1206. return EAP_SIM_DB_PENDING;
  1207. }
  1208. /**
  1209. * eap_sim_db_resynchronize - Resynchronize AKA AUTN
  1210. * @data: Private data pointer from eap_sim_db_init()
  1211. * @username: Permanent username
  1212. * @auts: AUTS value from the peer
  1213. * @_rand: RAND value used in the rejected message
  1214. * Returns: 0 on success, -1 on failure
  1215. *
  1216. * This function is called when the peer reports synchronization failure in the
  1217. * AUTN value by sending AUTS. The AUTS and RAND values should be sent to
  1218. * HLR/AuC to allow it to resynchronize with the peer. After this,
  1219. * eap_sim_db_get_aka_auth() will be called again to to fetch updated
  1220. * RAND/AUTN values for the next challenge.
  1221. */
  1222. int eap_sim_db_resynchronize(struct eap_sim_db_data *data,
  1223. const char *username,
  1224. const u8 *auts, const u8 *_rand)
  1225. {
  1226. const char *imsi;
  1227. size_t imsi_len;
  1228. if (username == NULL ||
  1229. (username[0] != EAP_AKA_PERMANENT_PREFIX &&
  1230. username[0] != EAP_AKA_PRIME_PERMANENT_PREFIX) ||
  1231. username[1] == '\0' || os_strlen(username) > 20) {
  1232. wpa_printf(MSG_DEBUG, "EAP-SIM DB: unexpected username '%s'",
  1233. username);
  1234. return -1;
  1235. }
  1236. imsi = username + 1;
  1237. wpa_printf(MSG_DEBUG, "EAP-SIM DB: Get AKA auth for IMSI '%s'",
  1238. imsi);
  1239. if (data->sock >= 0) {
  1240. char msg[100];
  1241. int len, ret;
  1242. imsi_len = os_strlen(imsi);
  1243. len = os_snprintf(msg, sizeof(msg), "AKA-AUTS ");
  1244. if (os_snprintf_error(sizeof(msg), len) ||
  1245. len + imsi_len >= sizeof(msg))
  1246. return -1;
  1247. os_memcpy(msg + len, imsi, imsi_len);
  1248. len += imsi_len;
  1249. ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
  1250. if (os_snprintf_error(sizeof(msg) - len, ret))
  1251. return -1;
  1252. len += ret;
  1253. len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
  1254. auts, EAP_AKA_AUTS_LEN);
  1255. ret = os_snprintf(msg + len, sizeof(msg) - len, " ");
  1256. if (os_snprintf_error(sizeof(msg) - len, ret))
  1257. return -1;
  1258. len += ret;
  1259. len += wpa_snprintf_hex(msg + len, sizeof(msg) - len,
  1260. _rand, EAP_AKA_RAND_LEN);
  1261. wpa_printf(MSG_DEBUG, "EAP-SIM DB: reporting AKA AUTS for "
  1262. "IMSI '%s'", imsi);
  1263. if (eap_sim_db_send(data, msg, len) < 0)
  1264. return -1;
  1265. }
  1266. return 0;
  1267. }
  1268. /**
  1269. * sim_get_username - Extract username from SIM identity
  1270. * @identity: Identity
  1271. * @identity_len: Identity length
  1272. * Returns: Allocated buffer with the username part of the identity
  1273. *
  1274. * Caller is responsible for freeing the returned buffer with os_free().
  1275. */
  1276. char * sim_get_username(const u8 *identity, size_t identity_len)
  1277. {
  1278. size_t pos;
  1279. if (identity == NULL)
  1280. return NULL;
  1281. for (pos = 0; pos < identity_len; pos++) {
  1282. if (identity[pos] == '@' || identity[pos] == '\0')
  1283. break;
  1284. }
  1285. return dup_binstr(identity, pos);
  1286. }