eap_sim_db.c 39 KB

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