1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
* hostapd / EAP-SIM database/authenticator gateway
* Copyright (c) 2005, Jouni Malinen <jkmaline@cc.hut.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*/
/* This is an example implementation of the EAP-SIM database/authentication
* gateway interface that is expected to be replaced with an implementation of
* SS7 gateway to GSM authentication center (HLR/AuC) or a local
* implementation of SIM triplet generator.
*
* The example implementation here reads triplets from a text file in
* IMSI:Kc:SRES:RAND format, IMSI in ASCII, other fields as hex strings. This
* is used to simulate an HLR/AuC. As such, it is not very useful for real life
* authentication, but it is useful both as an example implementation and for
* EAP-SIM testing.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
#include "eap_sim_common.h"
#include "eap_sim_db.h"
/* TODO: add an alternative callback based version of the interface. This is
* needed to work better with the single threaded design of hostapd. For this,
* the EAP data has to be stored somewhere and eap_sim_db is given a context
* pointer for this and a callback function. The callback function will re-send
* the EAP data through normal operations which will eventually end up calling
* eap_sim_db_get_gsm_triplets() again for the same user. This time, eap_sim_db
* should have the triplets available immediately. */
struct eap_sim_db_data {
char *fname;
};
#define KC_LEN 8
#define SRES_LEN 4
#define RAND_LEN 16
/* Initialize EAP-SIM database/authentication gateway interface.
* Returns pointer to a private data structure. */
void * eap_sim_db_init(const char *config)
{
struct eap_sim_db_data *data;
data = malloc(sizeof(*data));
if (data == NULL) {
return NULL;
}
memset(data, 0, sizeof(*data));
data->fname = strdup(config);
if (data->fname == NULL) {
free(data);
return NULL;
}
return data;
}
/* Deinitialize EAP-SIM database/authentication gateway interface.
* priv is the pointer from eap_sim_db_init(). */
void eap_sim_db_deinit(void *priv)
{
struct eap_sim_db_data *data = priv;
free(data->fname);
free(data);
}
/* Get GSM triplets for user name identity (identity_len bytes). In most cases,
* the user name is '1' | IMSI, i.e., 1 followed by the IMSI in ASCII format.
* The identity may also include NAI realm (@realm).
* priv is the pointer from eap_sim_db_init().
* Returns the number of triplets received (has to be less than or equal to
* max_chal) or -1 on error (e.g., user not found). rand, kc, and sres are
* pointers to data areas for the triplets. */
int eap_sim_db_get_gsm_triplets(void *priv, const u8 *identity,
size_t identity_len, int max_chal,
u8 *rand, u8 *kc, u8 *sres)
{
struct eap_sim_db_data *data = priv;
FILE *f;
int count, i;
char buf[80], *pos, *next;
f = fopen(data->fname, "r");
if (f == NULL) {
wpa_printf(MSG_DEBUG, "EAP-SIM DB: could not open triplet "
"file '%s'", data->fname);
return -1;
}
if (identity_len < 2 || identity[0] != '1') {
wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: unexpected identity",
identity, identity_len);
return -1;
}
identity++;
identity_len--;
for (i = 0; i < identity_len; i++) {
if (identity[i] == '@') {
identity_len = i;
break;
}
}
wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: get triplets for IMSI",
identity, identity_len);
count = 0;
while (count < max_chal && fgets(buf, sizeof(buf), f)) {
/* Parse IMSI:Kc:SRES:RAND and match IMSI with identity. */
buf[sizeof(buf) - 1] = '\0';
pos = buf;
while (*pos != '\0' && *pos != '\n')
pos++;
if (*pos == '\n')
*pos = '\0';
if (pos - buf < 60 || pos[0] == '#')
continue;
pos = strchr(buf, ':');
if (pos == NULL)
continue;
*pos++ = '\0';
if (strlen(buf) != identity_len ||
memcmp(buf, identity, identity_len) != 0)
continue;
next = strchr(pos, ':');
if (next == NULL)
continue;
*next++ = '\0';
if (hexstr2bin(pos, &kc[count * KC_LEN], KC_LEN) < 0)
continue;
pos = next;
next = strchr(pos, ':');
if (next == NULL)
continue;
*next++ = '\0';
if (hexstr2bin(pos, &sres[count * SRES_LEN], SRES_LEN) < 0)
continue;
if (hexstr2bin(next, &rand[count * RAND_LEN], RAND_LEN) < 0)
continue;
count++;
}
fclose(f);
if (count == 0) {
wpa_printf(MSG_DEBUG, "EAP-SIM DB: no triplets found");
count = -1;
}
return count;
}
/* Verify whether the given user identity (identity_len bytes) is known. In
* most cases, the user name is '1' | IMSI, i.e., 1 followed by the IMSI in
* ASCII format.
* priv is the pointer from eap_sim_db_init().
* Returns 0 if the user is found and GSM triplets would be available for it or
* -1 on error (e.g., user not found or no triplets available). */
int eap_sim_db_identity_known(void *priv, const u8 *identity,
size_t identity_len)
{
struct eap_sim_db_data *data = priv;
FILE *f;
char buf[80], *pos;
int i;
if (identity_len < 1 || identity[0] != '1') {
return -1;
}
f = fopen(data->fname, "r");
if (f == NULL) {
wpa_printf(MSG_DEBUG, "EAP-SIM DB: could not open triplet "
"file '%s'", data->fname);
return -1;
}
if (identity_len < 2 || identity[0] != '1') {
wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM DB: unexpected identity",
identity, identity_len);
return -1;
}
identity++;
identity_len--;
for (i = 0; i < identity_len; i++) {
if (identity[i] == '@') {
identity_len = i;
break;
}
}
while (fgets(buf, sizeof(buf), f)) {
/* Parse IMSI:Kc:SRES:RAND and match IMSI with identity. */
buf[sizeof(buf) - 1] = '\0';
pos = buf;
while (*pos != '\0' && *pos != '\n')
pos++;
if (*pos == '\n')
*pos = '\0';
if (pos - buf < 60 || pos[0] == '#')
continue;
pos = strchr(buf, ':');
if (pos == NULL)
continue;
*pos++ = '\0';
if (strlen(buf) != identity_len ||
memcmp(buf, identity, identity_len) != 0)
continue;
fclose(f);
return 0;
}
/* IMSI not found */
fclose(f);
return -1;
}
|