eap_sim_db.c 41 KB

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