]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/eap_peer/eap_sim.c
EAP-SIM/AKA: Add support for anonymous@realm
[thirdparty/hostap.git] / src / eap_peer / eap_sim.c
CommitLineData
6fc6879b
JM
1/*
2 * EAP peer method: EAP-SIM (RFC 4186)
e026159a 3 * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
43df4cc2
JM
12#include "pcsc_funcs.h"
13#include "crypto/milenage.h"
3642c431 14#include "crypto/random.h"
6fc6879b
JM
15#include "eap_peer/eap_i.h"
16#include "eap_config.h"
6fc6879b
JM
17#include "eap_common/eap_sim_common.h"
18
19
20struct eap_sim_data {
21 u8 *ver_list;
22 size_t ver_list_len;
23 int selected_version;
24 size_t min_num_chal, num_chal;
25
26 u8 kc[3][EAP_SIM_KC_LEN];
27 u8 sres[3][EAP_SIM_SRES_LEN];
28 u8 nonce_mt[EAP_SIM_NONCE_MT_LEN], nonce_s[EAP_SIM_NONCE_S_LEN];
29 u8 mk[EAP_SIM_MK_LEN];
30 u8 k_aut[EAP_SIM_K_AUT_LEN];
31 u8 k_encr[EAP_SIM_K_ENCR_LEN];
32 u8 msk[EAP_SIM_KEYING_DATA_LEN];
33 u8 emsk[EAP_EMSK_LEN];
34 u8 rand[3][GSM_RAND_LEN];
1c16b257 35 u8 reauth_mac[EAP_SIM_MAC_LEN];
6fc6879b
JM
36
37 int num_id_req, num_notification;
38 u8 *pseudonym;
39 size_t pseudonym_len;
40 u8 *reauth_id;
41 size_t reauth_id_len;
42 int reauth;
43 unsigned int counter, counter_too_small;
44 u8 *last_eap_identity;
45 size_t last_eap_identity_len;
46 enum {
79122f9f 47 CONTINUE, RESULT_SUCCESS, SUCCESS, FAILURE
6fc6879b
JM
48 } state;
49 int result_ind, use_result_ind;
9e2afe10 50 int use_pseudonym;
45f7574d 51 int error_code;
6fc6879b
JM
52};
53
54
55#ifndef CONFIG_NO_STDOUT_DEBUG
56static const char * eap_sim_state_txt(int state)
57{
58 switch (state) {
59 case CONTINUE:
60 return "CONTINUE";
61 case RESULT_SUCCESS:
62 return "RESULT_SUCCESS";
6fc6879b
JM
63 case SUCCESS:
64 return "SUCCESS";
65 case FAILURE:
66 return "FAILURE";
67 default:
68 return "?";
69 }
70}
71#endif /* CONFIG_NO_STDOUT_DEBUG */
72
73
74static void eap_sim_state(struct eap_sim_data *data, int state)
75{
76 wpa_printf(MSG_DEBUG, "EAP-SIM: %s -> %s",
77 eap_sim_state_txt(data->state),
78 eap_sim_state_txt(state));
79 data->state = state;
80}
81
82
83static void * eap_sim_init(struct eap_sm *sm)
84{
85 struct eap_sim_data *data;
86 struct eap_peer_config *config = eap_get_config(sm);
87
88 data = os_zalloc(sizeof(*data));
89 if (data == NULL)
90 return NULL;
91
3642c431 92 if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
6fc6879b
JM
93 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
94 "for NONCE_MT");
95 os_free(data);
96 return NULL;
97 }
98
45f7574d
AE
99 /* Zero is a valid error code, so we need to initialize */
100 data->error_code = NO_EAP_METHOD_ERROR;
101
6fc6879b
JM
102 data->min_num_chal = 2;
103 if (config && config->phase1) {
104 char *pos = os_strstr(config->phase1, "sim_min_num_chal=");
105 if (pos) {
106 data->min_num_chal = atoi(pos + 17);
107 if (data->min_num_chal < 2 || data->min_num_chal > 3) {
108 wpa_printf(MSG_WARNING, "EAP-SIM: Invalid "
109 "sim_min_num_chal configuration "
110 "(%lu, expected 2 or 3)",
111 (unsigned long) data->min_num_chal);
112 os_free(data);
113 return NULL;
114 }
115 wpa_printf(MSG_DEBUG, "EAP-SIM: Set minimum number of "
116 "challenges to %lu",
117 (unsigned long) data->min_num_chal);
118 }
119
120 data->result_ind = os_strstr(config->phase1, "result_ind=1") !=
121 NULL;
122 }
123
9e2afe10
PS
124 data->use_pseudonym = !sm->init_phase2;
125 if (config && config->anonymous_identity && data->use_pseudonym) {
e026159a
JM
126 data->pseudonym = os_malloc(config->anonymous_identity_len);
127 if (data->pseudonym) {
128 os_memcpy(data->pseudonym, config->anonymous_identity,
129 config->anonymous_identity_len);
130 data->pseudonym_len = config->anonymous_identity_len;
131 }
132 }
133
6fc6879b
JM
134 eap_sim_state(data, CONTINUE);
135
136 return data;
137}
138
139
f534ee08
JM
140static void eap_sim_clear_keys(struct eap_sim_data *data, int reauth)
141{
142 if (!reauth) {
143 os_memset(data->mk, 0, EAP_SIM_MK_LEN);
144 os_memset(data->k_aut, 0, EAP_SIM_K_AUT_LEN);
145 os_memset(data->k_encr, 0, EAP_SIM_K_ENCR_LEN);
146 }
147 os_memset(data->kc, 0, 3 * EAP_SIM_KC_LEN);
148 os_memset(data->sres, 0, 3 * EAP_SIM_SRES_LEN);
149 os_memset(data->msk, 0, EAP_SIM_KEYING_DATA_LEN);
150 os_memset(data->emsk, 0, EAP_EMSK_LEN);
151}
152
153
6fc6879b
JM
154static void eap_sim_deinit(struct eap_sm *sm, void *priv)
155{
156 struct eap_sim_data *data = priv;
157 if (data) {
158 os_free(data->ver_list);
159 os_free(data->pseudonym);
160 os_free(data->reauth_id);
161 os_free(data->last_eap_identity);
f534ee08 162 eap_sim_clear_keys(data, 0);
6fc6879b
JM
163 os_free(data);
164 }
165}
166
167
569ccf71
JM
168static int eap_sim_ext_sim_req(struct eap_sm *sm, struct eap_sim_data *data)
169{
170 char req[200], *pos, *end;
171 size_t i;
172
173 wpa_printf(MSG_DEBUG, "EAP-SIM: Use external SIM processing");
174 pos = req;
175 end = pos + sizeof(req);
176 pos += os_snprintf(pos, end - pos, "GSM-AUTH");
177 for (i = 0; i < data->num_chal; i++) {
178 pos += os_snprintf(pos, end - pos, ":");
179 pos += wpa_snprintf_hex(pos, end - pos, data->rand[i],
180 GSM_RAND_LEN);
181 }
182
183 eap_sm_request_sim(sm, req);
184 return 1;
185}
186
187
188static int eap_sim_ext_sim_result(struct eap_sm *sm, struct eap_sim_data *data,
189 struct eap_peer_config *conf)
190{
191 char *resp, *pos;
192 size_t i;
193
194 wpa_printf(MSG_DEBUG,
195 "EAP-SIM: Use result from external SIM processing");
196
197 resp = conf->external_sim_resp;
198 conf->external_sim_resp = NULL;
199
200 if (os_strncmp(resp, "GSM-AUTH:", 9) != 0) {
201 wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized external SIM processing response");
202 os_free(resp);
203 return -1;
204 }
205
206 pos = resp + 9;
207 for (i = 0; i < data->num_chal; i++) {
208 wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
209 data->rand[i], GSM_RAND_LEN);
210
211 if (hexstr2bin(pos, data->kc[i], EAP_SIM_KC_LEN) < 0)
212 goto invalid;
213 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
214 data->kc[i], EAP_SIM_KC_LEN);
215 pos += EAP_SIM_KC_LEN * 2;
216 if (*pos != ':')
217 goto invalid;
218 pos++;
219
220 if (hexstr2bin(pos, data->sres[i], EAP_SIM_SRES_LEN) < 0)
221 goto invalid;
222 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
223 data->sres[i], EAP_SIM_SRES_LEN);
224 pos += EAP_SIM_SRES_LEN * 2;
225 if (i + 1 < data->num_chal) {
226 if (*pos != ':')
227 goto invalid;
228 pos++;
229 }
230 }
231
232 os_free(resp);
233 return 0;
234
235invalid:
236 wpa_printf(MSG_DEBUG, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
237 os_free(resp);
238 return -1;
239}
240
241
6fc6879b
JM
242static int eap_sim_gsm_auth(struct eap_sm *sm, struct eap_sim_data *data)
243{
81eec387
JM
244 struct eap_peer_config *conf;
245
6fc6879b 246 wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication algorithm");
81eec387
JM
247
248 conf = eap_get_config(sm);
249 if (conf == NULL)
6fc6879b 250 return -1;
569ccf71
JM
251
252 if (sm->external_sim) {
253 if (conf->external_sim_resp)
254 return eap_sim_ext_sim_result(sm, data, conf);
255 else
256 return eap_sim_ext_sim_req(sm, data);
257 }
258
269f9d5d 259#ifdef PCSC_FUNCS
81eec387
JM
260 if (conf->pcsc) {
261 if (scard_gsm_auth(sm->scard_ctx, data->rand[0],
262 data->sres[0], data->kc[0]) ||
263 scard_gsm_auth(sm->scard_ctx, data->rand[1],
264 data->sres[1], data->kc[1]) ||
265 (data->num_chal > 2 &&
266 scard_gsm_auth(sm->scard_ctx, data->rand[2],
267 data->sres[2], data->kc[2]))) {
268 wpa_printf(MSG_DEBUG, "EAP-SIM: GSM SIM "
269 "authentication could not be completed");
270 return -1;
271 }
272 return 0;
273 }
269f9d5d 274#endif /* PCSC_FUNCS */
81eec387
JM
275
276#ifdef CONFIG_SIM_SIMULATOR
277 if (conf->password) {
278 u8 opc[16], k[16];
279 const char *pos;
a532c108 280 size_t i;
81eec387
JM
281 wpa_printf(MSG_DEBUG, "EAP-SIM: Use internal GSM-Milenage "
282 "implementation for authentication");
283 if (conf->password_len < 65) {
284 wpa_printf(MSG_DEBUG, "EAP-SIM: invalid GSM-Milenage "
285 "password");
286 return -1;
287 }
288 pos = (const char *) conf->password;
289 if (hexstr2bin(pos, k, 16))
290 return -1;
291 pos += 32;
292 if (*pos != ':')
293 return -1;
294 pos++;
295
296 if (hexstr2bin(pos, opc, 16))
297 return -1;
298
a532c108
JM
299 for (i = 0; i < data->num_chal; i++) {
300 if (gsm_milenage(opc, k, data->rand[i],
301 data->sres[i], data->kc[i])) {
302 wpa_printf(MSG_DEBUG, "EAP-SIM: "
303 "GSM-Milenage authentication "
304 "could not be completed");
305 return -1;
306 }
307 wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
308 data->rand[i], GSM_RAND_LEN);
309 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
310 data->sres[i], EAP_SIM_SRES_LEN);
311 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
312 data->kc[i], EAP_SIM_KC_LEN);
81eec387
JM
313 }
314 return 0;
6fc6879b 315 }
81eec387
JM
316#endif /* CONFIG_SIM_SIMULATOR */
317
318#ifdef CONFIG_SIM_HARDCODED
6fc6879b
JM
319 /* These hardcoded Kc and SRES values are used for testing. RAND to
320 * KC/SREC mapping is very bogus as far as real authentication is
321 * concerned, but it is quite useful for cases where the AS is rotating
322 * the order of pre-configured values. */
323 {
324 size_t i;
81eec387
JM
325
326 wpa_printf(MSG_DEBUG, "EAP-SIM: Use hardcoded Kc and SRES "
327 "values for testing");
328
6fc6879b
JM
329 for (i = 0; i < data->num_chal; i++) {
330 if (data->rand[i][0] == 0xaa) {
331 os_memcpy(data->kc[i],
332 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7",
333 EAP_SIM_KC_LEN);
334 os_memcpy(data->sres[i], "\xd1\xd2\xd3\xd4",
335 EAP_SIM_SRES_LEN);
336 } else if (data->rand[i][0] == 0xbb) {
337 os_memcpy(data->kc[i],
338 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7",
339 EAP_SIM_KC_LEN);
340 os_memcpy(data->sres[i], "\xe1\xe2\xe3\xe4",
341 EAP_SIM_SRES_LEN);
342 } else {
343 os_memcpy(data->kc[i],
344 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
345 EAP_SIM_KC_LEN);
346 os_memcpy(data->sres[i], "\xf1\xf2\xf3\xf4",
347 EAP_SIM_SRES_LEN);
348 }
349 }
350 }
81eec387 351
6fc6879b 352 return 0;
81eec387
JM
353
354#else /* CONFIG_SIM_HARDCODED */
355
356 wpa_printf(MSG_DEBUG, "EAP-SIM: No GSM authentication algorithm "
357 "enabled");
358 return -1;
359
360#endif /* CONFIG_SIM_HARDCODED */
6fc6879b
JM
361}
362
363
364static int eap_sim_supported_ver(int version)
365{
366 return version == EAP_SIM_VERSION;
367}
368
369
370#define CLEAR_PSEUDONYM 0x01
371#define CLEAR_REAUTH_ID 0x02
372#define CLEAR_EAP_ID 0x04
373
e026159a
JM
374static void eap_sim_clear_identities(struct eap_sm *sm,
375 struct eap_sim_data *data, int id)
6fc6879b 376{
1037235c
SB
377 if ((id & CLEAR_PSEUDONYM) && data->pseudonym) {
378 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old pseudonym");
6fc6879b
JM
379 os_free(data->pseudonym);
380 data->pseudonym = NULL;
381 data->pseudonym_len = 0;
9e2afe10
PS
382 if (data->use_pseudonym)
383 eap_set_anon_id(sm, NULL, 0);
6fc6879b 384 }
1037235c
SB
385 if ((id & CLEAR_REAUTH_ID) && data->reauth_id) {
386 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old reauth_id");
6fc6879b
JM
387 os_free(data->reauth_id);
388 data->reauth_id = NULL;
389 data->reauth_id_len = 0;
390 }
1037235c
SB
391 if ((id & CLEAR_EAP_ID) && data->last_eap_identity) {
392 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old eap_id");
6fc6879b
JM
393 os_free(data->last_eap_identity);
394 data->last_eap_identity = NULL;
395 data->last_eap_identity_len = 0;
396 }
397}
398
399
a6689be8 400static int eap_sim_learn_ids(struct eap_sm *sm, struct eap_sim_data *data,
6fc6879b
JM
401 struct eap_sim_attrs *attr)
402{
403 if (attr->next_pseudonym) {
a6689be8
SB
404 const u8 *identity = NULL;
405 size_t identity_len = 0;
406 const u8 *realm = NULL;
407 size_t realm_len = 0;
408
409 wpa_hexdump_ascii(MSG_DEBUG,
410 "EAP-SIM: (encr) AT_NEXT_PSEUDONYM",
411 attr->next_pseudonym,
412 attr->next_pseudonym_len);
6fc6879b 413 os_free(data->pseudonym);
a6689be8
SB
414 /* Look for the realm of the permanent identity */
415 identity = eap_get_config_identity(sm, &identity_len);
416 if (identity) {
417 for (realm = identity, realm_len = identity_len;
418 realm_len > 0; realm_len--, realm++) {
419 if (*realm == '@')
420 break;
421 }
422 }
423 data->pseudonym = os_malloc(attr->next_pseudonym_len +
424 realm_len);
6fc6879b
JM
425 if (data->pseudonym == NULL) {
426 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
427 "next pseudonym");
a6689be8 428 data->pseudonym_len = 0;
6fc6879b
JM
429 return -1;
430 }
431 os_memcpy(data->pseudonym, attr->next_pseudonym,
432 attr->next_pseudonym_len);
a6689be8
SB
433 if (realm_len) {
434 os_memcpy(data->pseudonym + attr->next_pseudonym_len,
435 realm, realm_len);
436 }
437 data->pseudonym_len = attr->next_pseudonym_len + realm_len;
9e2afe10
PS
438 if (data->use_pseudonym)
439 eap_set_anon_id(sm, data->pseudonym,
440 data->pseudonym_len);
6fc6879b
JM
441 }
442
443 if (attr->next_reauth_id) {
444 os_free(data->reauth_id);
a1f11e34
JB
445 data->reauth_id = os_memdup(attr->next_reauth_id,
446 attr->next_reauth_id_len);
6fc6879b
JM
447 if (data->reauth_id == NULL) {
448 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
449 "next reauth_id");
a6689be8 450 data->reauth_id_len = 0;
6fc6879b
JM
451 return -1;
452 }
6fc6879b
JM
453 data->reauth_id_len = attr->next_reauth_id_len;
454 wpa_hexdump_ascii(MSG_DEBUG,
455 "EAP-SIM: (encr) AT_NEXT_REAUTH_ID",
456 data->reauth_id,
457 data->reauth_id_len);
458 }
459
460 return 0;
461}
462
463
464static struct wpabuf * eap_sim_client_error(struct eap_sim_data *data, u8 id,
465 int err)
466{
467 struct eap_sim_msg *msg;
468
469 eap_sim_state(data, FAILURE);
470 data->num_id_req = 0;
471 data->num_notification = 0;
472
93434989
JM
473 wpa_printf(MSG_DEBUG, "EAP-SIM: Send Client-Error (error code %d)",
474 err);
6fc6879b
JM
475 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
476 EAP_SIM_SUBTYPE_CLIENT_ERROR);
477 eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
b2b8a4cb 478 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
6fc6879b
JM
479}
480
481
482static struct wpabuf * eap_sim_response_start(struct eap_sm *sm,
483 struct eap_sim_data *data, u8 id,
484 enum eap_sim_id_req id_req)
485{
486 const u8 *identity = NULL;
487 size_t identity_len = 0;
488 struct eap_sim_msg *msg;
489
490 data->reauth = 0;
491 if (id_req == ANY_ID && data->reauth_id) {
492 identity = data->reauth_id;
493 identity_len = data->reauth_id_len;
494 data->reauth = 1;
495 } else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
4df41339
HS
496 data->pseudonym &&
497 !eap_sim_anonymous_username(data->pseudonym,
498 data->pseudonym_len)) {
6fc6879b
JM
499 identity = data->pseudonym;
500 identity_len = data->pseudonym_len;
e026159a 501 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID);
6fc6879b
JM
502 } else if (id_req != NO_ID_REQ) {
503 identity = eap_get_config_identity(sm, &identity_len);
504 if (identity) {
e026159a 505 eap_sim_clear_identities(sm, data, CLEAR_PSEUDONYM |
6fc6879b
JM
506 CLEAR_REAUTH_ID);
507 }
508 }
509 if (id_req != NO_ID_REQ)
e026159a 510 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
6fc6879b
JM
511
512 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Start (id=%d)", id);
513 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
514 EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START);
515 if (!data->reauth) {
516 wpa_hexdump(MSG_DEBUG, " AT_NONCE_MT",
517 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
518 eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_MT, 0,
519 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
520 wpa_printf(MSG_DEBUG, " AT_SELECTED_VERSION %d",
521 data->selected_version);
522 eap_sim_msg_add(msg, EAP_SIM_AT_SELECTED_VERSION,
523 data->selected_version, NULL, 0);
524 }
525
526 if (identity) {
527 wpa_hexdump_ascii(MSG_DEBUG, " AT_IDENTITY",
528 identity, identity_len);
529 eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
530 identity, identity_len);
531 }
532
b2b8a4cb 533 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
6fc6879b
JM
534}
535
536
537static struct wpabuf * eap_sim_response_challenge(struct eap_sim_data *data,
538 u8 id)
539{
540 struct eap_sim_msg *msg;
541
542 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Challenge (id=%d)", id);
543 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
544 EAP_SIM_SUBTYPE_CHALLENGE);
545 if (data->use_result_ind) {
546 wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
547 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
548 }
549 wpa_printf(MSG_DEBUG, " AT_MAC");
550 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
b2b8a4cb
JM
551 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut,
552 (u8 *) data->sres,
6fc6879b
JM
553 data->num_chal * EAP_SIM_SRES_LEN);
554}
555
556
557static struct wpabuf * eap_sim_response_reauth(struct eap_sim_data *data,
5d65ca51
JM
558 u8 id, int counter_too_small,
559 const u8 *nonce_s)
6fc6879b
JM
560{
561 struct eap_sim_msg *msg;
562 unsigned int counter;
563
564 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Reauthentication (id=%d)",
565 id);
566 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
567 EAP_SIM_SUBTYPE_REAUTHENTICATION);
568 wpa_printf(MSG_DEBUG, " AT_IV");
569 wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
570 eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
571
572 if (counter_too_small) {
573 wpa_printf(MSG_DEBUG, " *AT_COUNTER_TOO_SMALL");
574 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
575 counter = data->counter_too_small;
576 } else
577 counter = data->counter;
578
579 wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter);
580 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
581
582 if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
583 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
584 "AT_ENCR_DATA");
585 eap_sim_msg_free(msg);
586 return NULL;
587 }
588 if (data->use_result_ind) {
589 wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
590 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
591 }
592 wpa_printf(MSG_DEBUG, " AT_MAC");
593 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
b2b8a4cb 594 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, nonce_s,
6fc6879b
JM
595 EAP_SIM_NONCE_S_LEN);
596}
597
598
599static struct wpabuf * eap_sim_response_notification(struct eap_sim_data *data,
600 u8 id, u16 notification)
601{
602 struct eap_sim_msg *msg;
603 u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
604
605 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Notification (id=%d)", id);
606 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
607 EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION);
6fc6879b
JM
608 if (k_aut && data->reauth) {
609 wpa_printf(MSG_DEBUG, " AT_IV");
610 wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
611 eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
612 EAP_SIM_AT_ENCR_DATA);
613 wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", data->counter);
614 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
615 NULL, 0);
616 if (eap_sim_msg_add_encr_end(msg, data->k_encr,
617 EAP_SIM_AT_PADDING)) {
618 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
619 "AT_ENCR_DATA");
620 eap_sim_msg_free(msg);
621 return NULL;
622 }
623 }
624 if (k_aut) {
625 wpa_printf(MSG_DEBUG, " AT_MAC");
626 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
627 }
b2b8a4cb 628 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, k_aut, (u8 *) "", 0);
6fc6879b
JM
629}
630
631
632static struct wpabuf * eap_sim_process_start(struct eap_sm *sm,
633 struct eap_sim_data *data, u8 id,
634 struct eap_sim_attrs *attr)
635{
636 int selected_version = -1, id_error;
637 size_t i;
638 u8 *pos;
639
640 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Start");
641 if (attr->version_list == NULL) {
642 wpa_printf(MSG_INFO, "EAP-SIM: No AT_VERSION_LIST in "
643 "SIM/Start");
644 return eap_sim_client_error(data, id,
645 EAP_SIM_UNSUPPORTED_VERSION);
646 }
647
648 os_free(data->ver_list);
a1f11e34 649 data->ver_list = os_memdup(attr->version_list, attr->version_list_len);
6fc6879b
JM
650 if (data->ver_list == NULL) {
651 wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to allocate "
652 "memory for version list");
653 return eap_sim_client_error(data, id,
654 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
655 }
6fc6879b
JM
656 data->ver_list_len = attr->version_list_len;
657 pos = data->ver_list;
658 for (i = 0; i < data->ver_list_len / 2; i++) {
659 int ver = pos[0] * 256 + pos[1];
660 pos += 2;
661 if (eap_sim_supported_ver(ver)) {
662 selected_version = ver;
663 break;
664 }
665 }
666 if (selected_version < 0) {
667 wpa_printf(MSG_INFO, "EAP-SIM: Could not find a supported "
668 "version");
669 return eap_sim_client_error(data, id,
670 EAP_SIM_UNSUPPORTED_VERSION);
671 }
672 wpa_printf(MSG_DEBUG, "EAP-SIM: Selected Version %d",
673 selected_version);
674 data->selected_version = selected_version;
675
676 id_error = 0;
677 switch (attr->id_req) {
678 case NO_ID_REQ:
679 break;
680 case ANY_ID:
681 if (data->num_id_req > 0)
682 id_error++;
683 data->num_id_req++;
684 break;
685 case FULLAUTH_ID:
686 if (data->num_id_req > 1)
687 id_error++;
688 data->num_id_req++;
689 break;
690 case PERMANENT_ID:
691 if (data->num_id_req > 2)
692 id_error++;
693 data->num_id_req++;
694 break;
695 }
696 if (id_error) {
697 wpa_printf(MSG_INFO, "EAP-SIM: Too many ID requests "
698 "used within one authentication");
699 return eap_sim_client_error(data, id,
700 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
701 }
702
703 return eap_sim_response_start(sm, data, id, attr->id_req);
704}
705
706
707static struct wpabuf * eap_sim_process_challenge(struct eap_sm *sm,
708 struct eap_sim_data *data,
709 u8 id,
710 const struct wpabuf *reqData,
711 struct eap_sim_attrs *attr)
712{
713 const u8 *identity;
714 size_t identity_len;
715 struct eap_sim_attrs eattr;
569ccf71 716 int res;
6fc6879b
JM
717
718 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Challenge");
719 data->reauth = 0;
720 if (!attr->mac || !attr->rand) {
721 wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
722 "did not include%s%s",
723 !attr->mac ? " AT_MAC" : "",
724 !attr->rand ? " AT_RAND" : "");
725 return eap_sim_client_error(data, id,
726 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
727 }
728
729 wpa_printf(MSG_DEBUG, "EAP-SIM: %lu challenges",
730 (unsigned long) attr->num_chal);
731 if (attr->num_chal < data->min_num_chal) {
732 wpa_printf(MSG_INFO, "EAP-SIM: Insufficient number of "
733 "challenges (%lu)", (unsigned long) attr->num_chal);
734 return eap_sim_client_error(data, id,
735 EAP_SIM_INSUFFICIENT_NUM_OF_CHAL);
736 }
737 if (attr->num_chal > 3) {
738 wpa_printf(MSG_INFO, "EAP-SIM: Too many challenges "
739 "(%lu)", (unsigned long) attr->num_chal);
740 return eap_sim_client_error(data, id,
741 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
742 }
743
744 /* Verify that RANDs are different */
745 if (os_memcmp(attr->rand, attr->rand + GSM_RAND_LEN,
746 GSM_RAND_LEN) == 0 ||
747 (attr->num_chal > 2 &&
748 (os_memcmp(attr->rand, attr->rand + 2 * GSM_RAND_LEN,
749 GSM_RAND_LEN) == 0 ||
750 os_memcmp(attr->rand + GSM_RAND_LEN,
751 attr->rand + 2 * GSM_RAND_LEN,
752 GSM_RAND_LEN) == 0))) {
753 wpa_printf(MSG_INFO, "EAP-SIM: Same RAND used multiple times");
754 return eap_sim_client_error(data, id,
755 EAP_SIM_RAND_NOT_FRESH);
756 }
757
758 os_memcpy(data->rand, attr->rand, attr->num_chal * GSM_RAND_LEN);
759 data->num_chal = attr->num_chal;
569ccf71
JM
760
761 res = eap_sim_gsm_auth(sm, data);
762 if (res > 0) {
763 wpa_printf(MSG_DEBUG, "EAP-SIM: Wait for external SIM processing");
764 return NULL;
765 }
766 if (res) {
6fc6879b
JM
767 wpa_printf(MSG_WARNING, "EAP-SIM: GSM authentication failed");
768 return eap_sim_client_error(data, id,
769 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
770 }
771 if (data->last_eap_identity) {
772 identity = data->last_eap_identity;
773 identity_len = data->last_eap_identity_len;
4df41339
HS
774 } else if (data->pseudonym &&
775 !eap_sim_anonymous_username(data->pseudonym,
776 data->pseudonym_len)) {
6fc6879b
JM
777 identity = data->pseudonym;
778 identity_len = data->pseudonym_len;
9e834fc6
JM
779 } else {
780 struct eap_peer_config *config;
781
782 config = eap_get_config(sm);
783 if (config && config->imsi_identity) {
784 identity = config->imsi_identity;
785 identity_len = config->imsi_identity_len;
786 } else {
787 identity = eap_get_config_identity(sm, &identity_len);
788 }
789 }
6fc6879b
JM
790 wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Selected identity for MK "
791 "derivation", identity, identity_len);
792 eap_sim_derive_mk(identity, identity_len, data->nonce_mt,
793 data->selected_version, data->ver_list,
794 data->ver_list_len, data->num_chal,
795 (const u8 *) data->kc, data->mk);
796 eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
797 data->emsk);
798 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, data->nonce_mt,
799 EAP_SIM_NONCE_MT_LEN)) {
800 wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
801 "used invalid AT_MAC");
802 return eap_sim_client_error(data, id,
803 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
804 }
805
a9f40ae7
SB
806 /* Old reauthentication identity must not be used anymore. In
807 * other words, if no new reauth identity is received, full
808 * authentication will be used on next reauthentication (using
809 * pseudonym identity or permanent identity). */
e026159a 810 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
6fc6879b
JM
811
812 if (attr->encr_data) {
813 u8 *decrypted;
814 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
815 attr->encr_data_len, attr->iv,
816 &eattr, 0);
817 if (decrypted == NULL) {
818 return eap_sim_client_error(
819 data, id, EAP_SIM_UNABLE_TO_PROCESS_PACKET);
820 }
a6689be8 821 eap_sim_learn_ids(sm, data, &eattr);
6fc6879b
JM
822 os_free(decrypted);
823 }
824
825 if (data->result_ind && attr->result_ind)
826 data->use_result_ind = 1;
827
79122f9f 828 if (data->state != FAILURE) {
6fc6879b
JM
829 eap_sim_state(data, data->use_result_ind ?
830 RESULT_SUCCESS : SUCCESS);
831 }
832
833 data->num_id_req = 0;
834 data->num_notification = 0;
835 /* RFC 4186 specifies that counter is initialized to one after
836 * fullauth, but initializing it to zero makes it easier to implement
837 * reauth verification. */
838 data->counter = 0;
839 return eap_sim_response_challenge(data, id);
840}
841
842
843static int eap_sim_process_notification_reauth(struct eap_sim_data *data,
844 struct eap_sim_attrs *attr)
845{
846 struct eap_sim_attrs eattr;
847 u8 *decrypted;
848
849 if (attr->encr_data == NULL || attr->iv == NULL) {
850 wpa_printf(MSG_WARNING, "EAP-SIM: Notification message after "
851 "reauth did not include encrypted data");
852 return -1;
853 }
854
855 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
856 attr->encr_data_len, attr->iv, &eattr,
857 0);
858 if (decrypted == NULL) {
859 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
860 "data from notification message");
861 return -1;
862 }
863
864 if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
865 wpa_printf(MSG_WARNING, "EAP-SIM: Counter in notification "
866 "message does not match with counter in reauth "
867 "message");
868 os_free(decrypted);
869 return -1;
870 }
871
872 os_free(decrypted);
873 return 0;
874}
875
876
877static int eap_sim_process_notification_auth(struct eap_sim_data *data,
878 const struct wpabuf *reqData,
879 struct eap_sim_attrs *attr)
880{
881 if (attr->mac == NULL) {
882 wpa_printf(MSG_INFO, "EAP-SIM: no AT_MAC in after_auth "
883 "Notification message");
884 return -1;
885 }
886
887 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
888 {
889 wpa_printf(MSG_WARNING, "EAP-SIM: Notification message "
890 "used invalid AT_MAC");
891 return -1;
892 }
893
894 if (data->reauth &&
895 eap_sim_process_notification_reauth(data, attr)) {
896 wpa_printf(MSG_WARNING, "EAP-SIM: Invalid notification "
897 "message after reauth");
898 return -1;
899 }
900
901 return 0;
902}
903
904
905static struct wpabuf * eap_sim_process_notification(
906 struct eap_sm *sm, struct eap_sim_data *data, u8 id,
907 const struct wpabuf *reqData, struct eap_sim_attrs *attr)
908{
909 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Notification");
910 if (data->num_notification > 0) {
911 wpa_printf(MSG_INFO, "EAP-SIM: too many notification "
912 "rounds (only one allowed)");
913 return eap_sim_client_error(data, id,
914 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
915 }
916 data->num_notification++;
917 if (attr->notification == -1) {
918 wpa_printf(MSG_INFO, "EAP-SIM: no AT_NOTIFICATION in "
919 "Notification message");
920 return eap_sim_client_error(data, id,
921 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
922 }
923
924 if ((attr->notification & 0x4000) == 0 &&
925 eap_sim_process_notification_auth(data, reqData, attr)) {
926 return eap_sim_client_error(data, id,
927 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
928 }
929
930 eap_sim_report_notification(sm->msg_ctx, attr->notification, 0);
931 if (attr->notification >= 0 && attr->notification < 32768) {
45f7574d 932 data->error_code = attr->notification;
6fc6879b
JM
933 eap_sim_state(data, FAILURE);
934 } else if (attr->notification == EAP_SIM_SUCCESS &&
935 data->state == RESULT_SUCCESS)
936 eap_sim_state(data, SUCCESS);
937 return eap_sim_response_notification(data, id, attr->notification);
938}
939
940
941static struct wpabuf * eap_sim_process_reauthentication(
942 struct eap_sm *sm, struct eap_sim_data *data, u8 id,
943 const struct wpabuf *reqData, struct eap_sim_attrs *attr)
944{
945 struct eap_sim_attrs eattr;
946 u8 *decrypted;
947
948 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Reauthentication");
949
950 if (data->reauth_id == NULL) {
951 wpa_printf(MSG_WARNING, "EAP-SIM: Server is trying "
952 "reauthentication, but no reauth_id available");
953 return eap_sim_client_error(data, id,
954 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
955 }
956
957 data->reauth = 1;
958 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
959 {
960 wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
961 "did not have valid AT_MAC");
962 return eap_sim_client_error(data, id,
963 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
964 }
965
1c16b257
MS
966 /* At this stage the received MAC has been verified. Use this MAC for
967 * reauth Session-Id calculation if all other checks pass.
968 * The peer does not use the local MAC but the received MAC in deriving
969 * Session-Id. */
970 os_memcpy(data->reauth_mac, attr->mac, EAP_SIM_MAC_LEN);
971 wpa_hexdump(MSG_DEBUG, "EAP-SIM: Server MAC",
972 data->reauth_mac, EAP_SIM_MAC_LEN);
973
6fc6879b
JM
974 if (attr->encr_data == NULL || attr->iv == NULL) {
975 wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
976 "message did not include encrypted data");
977 return eap_sim_client_error(data, id,
978 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
979 }
980
981 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
982 attr->encr_data_len, attr->iv, &eattr,
983 0);
984 if (decrypted == NULL) {
985 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
986 "data from reauthentication message");
987 return eap_sim_client_error(data, id,
988 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
989 }
990
991 if (eattr.nonce_s == NULL || eattr.counter < 0) {
992 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No%s%s in reauth packet",
993 !eattr.nonce_s ? " AT_NONCE_S" : "",
994 eattr.counter < 0 ? " AT_COUNTER" : "");
995 os_free(decrypted);
996 return eap_sim_client_error(data, id,
997 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
998 }
999
1000 if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
04cad507 1001 struct wpabuf *res;
6fc6879b
JM
1002 wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid counter "
1003 "(%d <= %d)", eattr.counter, data->counter);
1004 data->counter_too_small = eattr.counter;
04cad507 1005
6fc6879b
JM
1006 /* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
1007 * reauth_id must not be used to start a new reauthentication.
1008 * However, since it was used in the last EAP-Response-Identity
1009 * packet, it has to saved for the following fullauth to be
1010 * used in MK derivation. */
1011 os_free(data->last_eap_identity);
1012 data->last_eap_identity = data->reauth_id;
1013 data->last_eap_identity_len = data->reauth_id_len;
1014 data->reauth_id = NULL;
1015 data->reauth_id_len = 0;
04cad507
JM
1016
1017 res = eap_sim_response_reauth(data, id, 1, eattr.nonce_s);
6fc6879b 1018 os_free(decrypted);
04cad507
JM
1019
1020 return res;
6fc6879b
JM
1021 }
1022 data->counter = eattr.counter;
1023
1024 os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
1025 wpa_hexdump(MSG_DEBUG, "EAP-SIM: (encr) AT_NONCE_S",
1026 data->nonce_s, EAP_SIM_NONCE_S_LEN);
1027
1028 eap_sim_derive_keys_reauth(data->counter,
1029 data->reauth_id, data->reauth_id_len,
1030 data->nonce_s, data->mk, data->msk,
1031 data->emsk);
e026159a 1032 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
a6689be8 1033 eap_sim_learn_ids(sm, data, &eattr);
6fc6879b
JM
1034
1035 if (data->result_ind && attr->result_ind)
1036 data->use_result_ind = 1;
1037
79122f9f 1038 if (data->state != FAILURE) {
6fc6879b
JM
1039 eap_sim_state(data, data->use_result_ind ?
1040 RESULT_SUCCESS : SUCCESS);
1041 }
1042
1043 data->num_id_req = 0;
1044 data->num_notification = 0;
1045 if (data->counter > EAP_SIM_MAX_FAST_REAUTHS) {
1046 wpa_printf(MSG_DEBUG, "EAP-SIM: Maximum number of "
1047 "fast reauths performed - force fullauth");
e026159a
JM
1048 eap_sim_clear_identities(sm, data,
1049 CLEAR_REAUTH_ID | CLEAR_EAP_ID);
6fc6879b
JM
1050 }
1051 os_free(decrypted);
5d65ca51 1052 return eap_sim_response_reauth(data, id, 0, data->nonce_s);
6fc6879b
JM
1053}
1054
1055
1056static struct wpabuf * eap_sim_process(struct eap_sm *sm, void *priv,
1057 struct eap_method_ret *ret,
1058 const struct wpabuf *reqData)
1059{
1060 struct eap_sim_data *data = priv;
1061 const struct eap_hdr *req;
1062 u8 subtype, id;
1063 struct wpabuf *res;
1064 const u8 *pos;
1065 struct eap_sim_attrs attr;
1066 size_t len;
1067
1068 wpa_hexdump_buf(MSG_DEBUG, "EAP-SIM: EAP data", reqData);
1069 if (eap_get_config_identity(sm, &len) == NULL) {
1070 wpa_printf(MSG_INFO, "EAP-SIM: Identity not configured");
1071 eap_sm_request_identity(sm);
1072 ret->ignore = TRUE;
1073 return NULL;
1074 }
1075
1076 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, reqData, &len);
ff4a6d43 1077 if (pos == NULL || len < 3) {
6fc6879b
JM
1078 ret->ignore = TRUE;
1079 return NULL;
1080 }
1081 req = wpabuf_head(reqData);
1082 id = req->identifier;
1083 len = be_to_host16(req->length);
1084
1085 ret->ignore = FALSE;
1086 ret->methodState = METHOD_MAY_CONT;
1087 ret->decision = DECISION_FAIL;
1088 ret->allowNotifications = TRUE;
1089
1090 subtype = *pos++;
1091 wpa_printf(MSG_DEBUG, "EAP-SIM: Subtype=%d", subtype);
1092 pos += 2; /* Reserved */
1093
1094 if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 0,
1095 0)) {
1096 res = eap_sim_client_error(data, id,
1097 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1098 goto done;
1099 }
1100
1101 switch (subtype) {
1102 case EAP_SIM_SUBTYPE_START:
1103 res = eap_sim_process_start(sm, data, id, &attr);
1104 break;
1105 case EAP_SIM_SUBTYPE_CHALLENGE:
1106 res = eap_sim_process_challenge(sm, data, id, reqData, &attr);
1107 break;
1108 case EAP_SIM_SUBTYPE_NOTIFICATION:
1109 res = eap_sim_process_notification(sm, data, id, reqData,
1110 &attr);
1111 break;
1112 case EAP_SIM_SUBTYPE_REAUTHENTICATION:
1113 res = eap_sim_process_reauthentication(sm, data, id, reqData,
1114 &attr);
1115 break;
1116 case EAP_SIM_SUBTYPE_CLIENT_ERROR:
1117 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Client-Error");
1118 res = eap_sim_client_error(data, id,
1119 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1120 break;
1121 default:
1122 wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown subtype=%d", subtype);
1123 res = eap_sim_client_error(data, id,
1124 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1125 break;
1126 }
1127
1128done:
1129 if (data->state == FAILURE) {
1130 ret->decision = DECISION_FAIL;
1131 ret->methodState = METHOD_DONE;
1132 } else if (data->state == SUCCESS) {
1133 ret->decision = data->use_result_ind ?
1134 DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
1135 ret->methodState = data->use_result_ind ?
1136 METHOD_DONE : METHOD_MAY_CONT;
79122f9f 1137 } else if (data->state == RESULT_SUCCESS)
6fc6879b
JM
1138 ret->methodState = METHOD_CONT;
1139
1140 if (ret->methodState == METHOD_DONE) {
1141 ret->allowNotifications = FALSE;
1142 }
1143
1144 return res;
1145}
1146
1147
1148static Boolean eap_sim_has_reauth_data(struct eap_sm *sm, void *priv)
1149{
1150 struct eap_sim_data *data = priv;
1151 return data->pseudonym || data->reauth_id;
1152}
1153
1154
1155static void eap_sim_deinit_for_reauth(struct eap_sm *sm, void *priv)
1156{
1157 struct eap_sim_data *data = priv;
e026159a 1158 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
6fc6879b 1159 data->use_result_ind = 0;
f534ee08 1160 eap_sim_clear_keys(data, 1);
6fc6879b
JM
1161}
1162
1163
1164static void * eap_sim_init_for_reauth(struct eap_sm *sm, void *priv)
1165{
1166 struct eap_sim_data *data = priv;
3642c431 1167 if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
6fc6879b
JM
1168 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
1169 "for NONCE_MT");
ea52a46e 1170 eap_sim_deinit(sm, data);
6fc6879b
JM
1171 return NULL;
1172 }
1173 data->num_id_req = 0;
1174 data->num_notification = 0;
1175 eap_sim_state(data, CONTINUE);
1176 return priv;
1177}
1178
1179
1180static const u8 * eap_sim_get_identity(struct eap_sm *sm, void *priv,
1181 size_t *len)
1182{
1183 struct eap_sim_data *data = priv;
1184
1185 if (data->reauth_id) {
1186 *len = data->reauth_id_len;
1187 return data->reauth_id;
1188 }
1189
1190 if (data->pseudonym) {
1191 *len = data->pseudonym_len;
1192 return data->pseudonym;
1193 }
1194
1195 return NULL;
1196}
1197
1198
1199static Boolean eap_sim_isKeyAvailable(struct eap_sm *sm, void *priv)
1200{
1201 struct eap_sim_data *data = priv;
1202 return data->state == SUCCESS;
1203}
1204
1205
1206static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
1207{
1208 struct eap_sim_data *data = priv;
1209 u8 *key;
1210
1211 if (data->state != SUCCESS)
1212 return NULL;
1213
a1f11e34 1214 key = os_memdup(data->msk, EAP_SIM_KEYING_DATA_LEN);
6fc6879b
JM
1215 if (key == NULL)
1216 return NULL;
1217
1218 *len = EAP_SIM_KEYING_DATA_LEN;
6fc6879b
JM
1219
1220 return key;
1221}
1222
1223
9ca84274
JM
1224static u8 * eap_sim_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1225{
1226 struct eap_sim_data *data = priv;
1227 u8 *id;
1228
1229 if (data->state != SUCCESS)
1230 return NULL;
1231
1c16b257
MS
1232 if (!data->reauth)
1233 *len = 1 + data->num_chal * GSM_RAND_LEN + EAP_SIM_NONCE_MT_LEN;
1234 else
1235 *len = 1 + EAP_SIM_NONCE_S_LEN + EAP_SIM_MAC_LEN;
9ca84274
JM
1236 id = os_malloc(*len);
1237 if (id == NULL)
1238 return NULL;
1239
1240 id[0] = EAP_TYPE_SIM;
1c16b257
MS
1241 if (!data->reauth) {
1242 os_memcpy(id + 1, data->rand, data->num_chal * GSM_RAND_LEN);
1243 os_memcpy(id + 1 + data->num_chal * GSM_RAND_LEN,
1244 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
1245 } else {
1246 os_memcpy(id + 1, data->nonce_s, EAP_SIM_NONCE_S_LEN);
1247 os_memcpy(id + 1 + EAP_SIM_NONCE_S_LEN, data->reauth_mac,
1248 EAP_SIM_MAC_LEN);
1249 }
9ca84274
JM
1250 wpa_hexdump(MSG_DEBUG, "EAP-SIM: Derived Session-Id", id, *len);
1251
1252 return id;
1253}
1254
1255
6fc6879b
JM
1256static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1257{
1258 struct eap_sim_data *data = priv;
1259 u8 *key;
1260
1261 if (data->state != SUCCESS)
1262 return NULL;
1263
a1f11e34 1264 key = os_memdup(data->emsk, EAP_EMSK_LEN);
6fc6879b
JM
1265 if (key == NULL)
1266 return NULL;
1267
1268 *len = EAP_EMSK_LEN;
6fc6879b
JM
1269
1270 return key;
1271}
1272
1273
45f7574d
AE
1274static int eap_sim_get_error_code(void *priv)
1275{
1276 struct eap_sim_data *data = priv;
1277 int current_data_error;
1278
1279 if (!data)
1280 return NO_EAP_METHOD_ERROR;
1281
1282 current_data_error = data->error_code;
1283
1284 /* Now reset for next transaction */
1285 data->error_code = NO_EAP_METHOD_ERROR;
1286
1287 return current_data_error;
1288}
1289
1290
6fc6879b
JM
1291int eap_peer_sim_register(void)
1292{
1293 struct eap_method *eap;
6fc6879b
JM
1294
1295 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1296 EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
1297 if (eap == NULL)
1298 return -1;
1299
1300 eap->init = eap_sim_init;
1301 eap->deinit = eap_sim_deinit;
1302 eap->process = eap_sim_process;
1303 eap->isKeyAvailable = eap_sim_isKeyAvailable;
1304 eap->getKey = eap_sim_getKey;
9ca84274 1305 eap->getSessionId = eap_sim_get_session_id;
6fc6879b
JM
1306 eap->has_reauth_data = eap_sim_has_reauth_data;
1307 eap->deinit_for_reauth = eap_sim_deinit_for_reauth;
1308 eap->init_for_reauth = eap_sim_init_for_reauth;
1309 eap->get_identity = eap_sim_get_identity;
1310 eap->get_emsk = eap_sim_get_emsk;
45f7574d 1311 eap->get_error_code = eap_sim_get_error_code;
6fc6879b 1312
49a26bb3 1313 return eap_peer_method_register(eap);
6fc6879b 1314}