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