]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/eap_peer/eap_sim.c
59a208b1ff7d49bb47b59eefd58430bf07cff843
[thirdparty/hostap.git] / src / eap_peer / eap_sim.c
1 /*
2 * EAP peer method: EAP-SIM (RFC 4186)
3 * Copyright (c) 2004-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
9 #include "includes.h"
10
11 #include "common.h"
12 #include "pcsc_funcs.h"
13 #include "crypto/milenage.h"
14 #include "crypto/random.h"
15 #include "eap_peer/eap_i.h"
16 #include "eap_config.h"
17 #include "eap_common/eap_sim_common.h"
18
19
20 struct 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];
35 u8 reauth_mac[EAP_SIM_MAC_LEN];
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 {
47 CONTINUE, RESULT_SUCCESS, SUCCESS, FAILURE
48 } state;
49 int result_ind, use_result_ind;
50 int use_pseudonym;
51 int error_code;
52 };
53
54
55 #ifndef CONFIG_NO_STDOUT_DEBUG
56 static 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";
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
74 static 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
83 static 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
92 if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
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
99 /* Zero is a valid error code, so we need to initialize */
100 data->error_code = NO_EAP_METHOD_ERROR;
101
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
124 data->use_pseudonym = !sm->init_phase2;
125 if (config && config->anonymous_identity && data->use_pseudonym) {
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
134 eap_sim_state(data, CONTINUE);
135
136 return data;
137 }
138
139
140 static 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
154 static 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);
162 eap_sim_clear_keys(data, 0);
163 os_free(data);
164 }
165 }
166
167
168 static 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
188 static 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
235 invalid:
236 wpa_printf(MSG_DEBUG, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
237 os_free(resp);
238 return -1;
239 }
240
241
242 static int eap_sim_gsm_auth(struct eap_sm *sm, struct eap_sim_data *data)
243 {
244 struct eap_peer_config *conf;
245
246 wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication algorithm");
247
248 conf = eap_get_config(sm);
249 if (conf == NULL)
250 return -1;
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
259 #ifdef PCSC_FUNCS
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 }
274 #endif /* PCSC_FUNCS */
275
276 #ifdef CONFIG_SIM_SIMULATOR
277 if (conf->password) {
278 u8 opc[16], k[16];
279 const char *pos;
280 size_t i;
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
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);
313 }
314 return 0;
315 }
316 #endif /* CONFIG_SIM_SIMULATOR */
317
318 #ifdef CONFIG_SIM_HARDCODED
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;
325
326 wpa_printf(MSG_DEBUG, "EAP-SIM: Use hardcoded Kc and SRES "
327 "values for testing");
328
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 }
351
352 return 0;
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 */
361 }
362
363
364 static 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
374 static void eap_sim_clear_identities(struct eap_sm *sm,
375 struct eap_sim_data *data, int id)
376 {
377 if ((id & CLEAR_PSEUDONYM) && data->pseudonym) {
378 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old pseudonym");
379 os_free(data->pseudonym);
380 data->pseudonym = NULL;
381 data->pseudonym_len = 0;
382 if (data->use_pseudonym)
383 eap_set_anon_id(sm, NULL, 0);
384 }
385 if ((id & CLEAR_REAUTH_ID) && data->reauth_id) {
386 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old reauth_id");
387 os_free(data->reauth_id);
388 data->reauth_id = NULL;
389 data->reauth_id_len = 0;
390 }
391 if ((id & CLEAR_EAP_ID) && data->last_eap_identity) {
392 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old eap_id");
393 os_free(data->last_eap_identity);
394 data->last_eap_identity = NULL;
395 data->last_eap_identity_len = 0;
396 }
397 }
398
399
400 static int eap_sim_learn_ids(struct eap_sm *sm, struct eap_sim_data *data,
401 struct eap_sim_attrs *attr)
402 {
403 if (attr->next_pseudonym) {
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);
413 os_free(data->pseudonym);
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);
425 if (data->pseudonym == NULL) {
426 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
427 "next pseudonym");
428 data->pseudonym_len = 0;
429 return -1;
430 }
431 os_memcpy(data->pseudonym, attr->next_pseudonym,
432 attr->next_pseudonym_len);
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;
438 if (data->use_pseudonym)
439 eap_set_anon_id(sm, data->pseudonym,
440 data->pseudonym_len);
441 }
442
443 if (attr->next_reauth_id) {
444 os_free(data->reauth_id);
445 data->reauth_id = os_memdup(attr->next_reauth_id,
446 attr->next_reauth_id_len);
447 if (data->reauth_id == NULL) {
448 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
449 "next reauth_id");
450 data->reauth_id_len = 0;
451 return -1;
452 }
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
464 static 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
473 wpa_printf(MSG_DEBUG, "EAP-SIM: Send Client-Error (error code %d)",
474 err);
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);
478 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
479 }
480
481
482 static 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) &&
496 data->pseudonym) {
497 identity = data->pseudonym;
498 identity_len = data->pseudonym_len;
499 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID);
500 } else if (id_req != NO_ID_REQ) {
501 identity = eap_get_config_identity(sm, &identity_len);
502 if (identity) {
503 eap_sim_clear_identities(sm, data, CLEAR_PSEUDONYM |
504 CLEAR_REAUTH_ID);
505 }
506 }
507 if (id_req != NO_ID_REQ)
508 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
509
510 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Start (id=%d)", id);
511 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
512 EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START);
513 if (!data->reauth) {
514 wpa_hexdump(MSG_DEBUG, " AT_NONCE_MT",
515 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
516 eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_MT, 0,
517 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
518 wpa_printf(MSG_DEBUG, " AT_SELECTED_VERSION %d",
519 data->selected_version);
520 eap_sim_msg_add(msg, EAP_SIM_AT_SELECTED_VERSION,
521 data->selected_version, NULL, 0);
522 }
523
524 if (identity) {
525 wpa_hexdump_ascii(MSG_DEBUG, " AT_IDENTITY",
526 identity, identity_len);
527 eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
528 identity, identity_len);
529 }
530
531 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
532 }
533
534
535 static struct wpabuf * eap_sim_response_challenge(struct eap_sim_data *data,
536 u8 id)
537 {
538 struct eap_sim_msg *msg;
539
540 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Challenge (id=%d)", id);
541 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
542 EAP_SIM_SUBTYPE_CHALLENGE);
543 if (data->use_result_ind) {
544 wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
545 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
546 }
547 wpa_printf(MSG_DEBUG, " AT_MAC");
548 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
549 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut,
550 (u8 *) data->sres,
551 data->num_chal * EAP_SIM_SRES_LEN);
552 }
553
554
555 static struct wpabuf * eap_sim_response_reauth(struct eap_sim_data *data,
556 u8 id, int counter_too_small,
557 const u8 *nonce_s)
558 {
559 struct eap_sim_msg *msg;
560 unsigned int counter;
561
562 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Reauthentication (id=%d)",
563 id);
564 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
565 EAP_SIM_SUBTYPE_REAUTHENTICATION);
566 wpa_printf(MSG_DEBUG, " AT_IV");
567 wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
568 eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
569
570 if (counter_too_small) {
571 wpa_printf(MSG_DEBUG, " *AT_COUNTER_TOO_SMALL");
572 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
573 counter = data->counter_too_small;
574 } else
575 counter = data->counter;
576
577 wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter);
578 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
579
580 if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
581 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
582 "AT_ENCR_DATA");
583 eap_sim_msg_free(msg);
584 return NULL;
585 }
586 if (data->use_result_ind) {
587 wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
588 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
589 }
590 wpa_printf(MSG_DEBUG, " AT_MAC");
591 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
592 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, nonce_s,
593 EAP_SIM_NONCE_S_LEN);
594 }
595
596
597 static struct wpabuf * eap_sim_response_notification(struct eap_sim_data *data,
598 u8 id, u16 notification)
599 {
600 struct eap_sim_msg *msg;
601 u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
602
603 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Notification (id=%d)", id);
604 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
605 EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION);
606 if (k_aut && data->reauth) {
607 wpa_printf(MSG_DEBUG, " AT_IV");
608 wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
609 eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
610 EAP_SIM_AT_ENCR_DATA);
611 wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", data->counter);
612 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
613 NULL, 0);
614 if (eap_sim_msg_add_encr_end(msg, data->k_encr,
615 EAP_SIM_AT_PADDING)) {
616 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
617 "AT_ENCR_DATA");
618 eap_sim_msg_free(msg);
619 return NULL;
620 }
621 }
622 if (k_aut) {
623 wpa_printf(MSG_DEBUG, " AT_MAC");
624 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
625 }
626 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, k_aut, (u8 *) "", 0);
627 }
628
629
630 static struct wpabuf * eap_sim_process_start(struct eap_sm *sm,
631 struct eap_sim_data *data, u8 id,
632 struct eap_sim_attrs *attr)
633 {
634 int selected_version = -1, id_error;
635 size_t i;
636 u8 *pos;
637
638 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Start");
639 if (attr->version_list == NULL) {
640 wpa_printf(MSG_INFO, "EAP-SIM: No AT_VERSION_LIST in "
641 "SIM/Start");
642 return eap_sim_client_error(data, id,
643 EAP_SIM_UNSUPPORTED_VERSION);
644 }
645
646 os_free(data->ver_list);
647 data->ver_list = os_memdup(attr->version_list, attr->version_list_len);
648 if (data->ver_list == NULL) {
649 wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to allocate "
650 "memory for version list");
651 return eap_sim_client_error(data, id,
652 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
653 }
654 data->ver_list_len = attr->version_list_len;
655 pos = data->ver_list;
656 for (i = 0; i < data->ver_list_len / 2; i++) {
657 int ver = pos[0] * 256 + pos[1];
658 pos += 2;
659 if (eap_sim_supported_ver(ver)) {
660 selected_version = ver;
661 break;
662 }
663 }
664 if (selected_version < 0) {
665 wpa_printf(MSG_INFO, "EAP-SIM: Could not find a supported "
666 "version");
667 return eap_sim_client_error(data, id,
668 EAP_SIM_UNSUPPORTED_VERSION);
669 }
670 wpa_printf(MSG_DEBUG, "EAP-SIM: Selected Version %d",
671 selected_version);
672 data->selected_version = selected_version;
673
674 id_error = 0;
675 switch (attr->id_req) {
676 case NO_ID_REQ:
677 break;
678 case ANY_ID:
679 if (data->num_id_req > 0)
680 id_error++;
681 data->num_id_req++;
682 break;
683 case FULLAUTH_ID:
684 if (data->num_id_req > 1)
685 id_error++;
686 data->num_id_req++;
687 break;
688 case PERMANENT_ID:
689 if (data->num_id_req > 2)
690 id_error++;
691 data->num_id_req++;
692 break;
693 }
694 if (id_error) {
695 wpa_printf(MSG_INFO, "EAP-SIM: Too many ID requests "
696 "used within one authentication");
697 return eap_sim_client_error(data, id,
698 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
699 }
700
701 return eap_sim_response_start(sm, data, id, attr->id_req);
702 }
703
704
705 static struct wpabuf * eap_sim_process_challenge(struct eap_sm *sm,
706 struct eap_sim_data *data,
707 u8 id,
708 const struct wpabuf *reqData,
709 struct eap_sim_attrs *attr)
710 {
711 const u8 *identity;
712 size_t identity_len;
713 struct eap_sim_attrs eattr;
714 int res;
715
716 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Challenge");
717 data->reauth = 0;
718 if (!attr->mac || !attr->rand) {
719 wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
720 "did not include%s%s",
721 !attr->mac ? " AT_MAC" : "",
722 !attr->rand ? " AT_RAND" : "");
723 return eap_sim_client_error(data, id,
724 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
725 }
726
727 wpa_printf(MSG_DEBUG, "EAP-SIM: %lu challenges",
728 (unsigned long) attr->num_chal);
729 if (attr->num_chal < data->min_num_chal) {
730 wpa_printf(MSG_INFO, "EAP-SIM: Insufficient number of "
731 "challenges (%lu)", (unsigned long) attr->num_chal);
732 return eap_sim_client_error(data, id,
733 EAP_SIM_INSUFFICIENT_NUM_OF_CHAL);
734 }
735 if (attr->num_chal > 3) {
736 wpa_printf(MSG_INFO, "EAP-SIM: Too many challenges "
737 "(%lu)", (unsigned long) attr->num_chal);
738 return eap_sim_client_error(data, id,
739 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
740 }
741
742 /* Verify that RANDs are different */
743 if (os_memcmp(attr->rand, attr->rand + GSM_RAND_LEN,
744 GSM_RAND_LEN) == 0 ||
745 (attr->num_chal > 2 &&
746 (os_memcmp(attr->rand, attr->rand + 2 * GSM_RAND_LEN,
747 GSM_RAND_LEN) == 0 ||
748 os_memcmp(attr->rand + GSM_RAND_LEN,
749 attr->rand + 2 * GSM_RAND_LEN,
750 GSM_RAND_LEN) == 0))) {
751 wpa_printf(MSG_INFO, "EAP-SIM: Same RAND used multiple times");
752 return eap_sim_client_error(data, id,
753 EAP_SIM_RAND_NOT_FRESH);
754 }
755
756 os_memcpy(data->rand, attr->rand, attr->num_chal * GSM_RAND_LEN);
757 data->num_chal = attr->num_chal;
758
759 res = eap_sim_gsm_auth(sm, data);
760 if (res > 0) {
761 wpa_printf(MSG_DEBUG, "EAP-SIM: Wait for external SIM processing");
762 return NULL;
763 }
764 if (res) {
765 wpa_printf(MSG_WARNING, "EAP-SIM: GSM authentication failed");
766 return eap_sim_client_error(data, id,
767 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
768 }
769 if (data->last_eap_identity) {
770 identity = data->last_eap_identity;
771 identity_len = data->last_eap_identity_len;
772 } else if (data->pseudonym) {
773 identity = data->pseudonym;
774 identity_len = data->pseudonym_len;
775 } else {
776 struct eap_peer_config *config;
777
778 config = eap_get_config(sm);
779 if (config && config->imsi_identity) {
780 identity = config->imsi_identity;
781 identity_len = config->imsi_identity_len;
782 } else {
783 identity = eap_get_config_identity(sm, &identity_len);
784 }
785 }
786 wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Selected identity for MK "
787 "derivation", identity, identity_len);
788 eap_sim_derive_mk(identity, identity_len, data->nonce_mt,
789 data->selected_version, data->ver_list,
790 data->ver_list_len, data->num_chal,
791 (const u8 *) data->kc, data->mk);
792 eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
793 data->emsk);
794 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, data->nonce_mt,
795 EAP_SIM_NONCE_MT_LEN)) {
796 wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
797 "used invalid AT_MAC");
798 return eap_sim_client_error(data, id,
799 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
800 }
801
802 /* Old reauthentication identity must not be used anymore. In
803 * other words, if no new reauth identity is received, full
804 * authentication will be used on next reauthentication (using
805 * pseudonym identity or permanent identity). */
806 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
807
808 if (attr->encr_data) {
809 u8 *decrypted;
810 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
811 attr->encr_data_len, attr->iv,
812 &eattr, 0);
813 if (decrypted == NULL) {
814 return eap_sim_client_error(
815 data, id, EAP_SIM_UNABLE_TO_PROCESS_PACKET);
816 }
817 eap_sim_learn_ids(sm, data, &eattr);
818 os_free(decrypted);
819 }
820
821 if (data->result_ind && attr->result_ind)
822 data->use_result_ind = 1;
823
824 if (data->state != FAILURE) {
825 eap_sim_state(data, data->use_result_ind ?
826 RESULT_SUCCESS : SUCCESS);
827 }
828
829 data->num_id_req = 0;
830 data->num_notification = 0;
831 /* RFC 4186 specifies that counter is initialized to one after
832 * fullauth, but initializing it to zero makes it easier to implement
833 * reauth verification. */
834 data->counter = 0;
835 return eap_sim_response_challenge(data, id);
836 }
837
838
839 static int eap_sim_process_notification_reauth(struct eap_sim_data *data,
840 struct eap_sim_attrs *attr)
841 {
842 struct eap_sim_attrs eattr;
843 u8 *decrypted;
844
845 if (attr->encr_data == NULL || attr->iv == NULL) {
846 wpa_printf(MSG_WARNING, "EAP-SIM: Notification message after "
847 "reauth did not include encrypted data");
848 return -1;
849 }
850
851 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
852 attr->encr_data_len, attr->iv, &eattr,
853 0);
854 if (decrypted == NULL) {
855 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
856 "data from notification message");
857 return -1;
858 }
859
860 if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
861 wpa_printf(MSG_WARNING, "EAP-SIM: Counter in notification "
862 "message does not match with counter in reauth "
863 "message");
864 os_free(decrypted);
865 return -1;
866 }
867
868 os_free(decrypted);
869 return 0;
870 }
871
872
873 static int eap_sim_process_notification_auth(struct eap_sim_data *data,
874 const struct wpabuf *reqData,
875 struct eap_sim_attrs *attr)
876 {
877 if (attr->mac == NULL) {
878 wpa_printf(MSG_INFO, "EAP-SIM: no AT_MAC in after_auth "
879 "Notification message");
880 return -1;
881 }
882
883 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
884 {
885 wpa_printf(MSG_WARNING, "EAP-SIM: Notification message "
886 "used invalid AT_MAC");
887 return -1;
888 }
889
890 if (data->reauth &&
891 eap_sim_process_notification_reauth(data, attr)) {
892 wpa_printf(MSG_WARNING, "EAP-SIM: Invalid notification "
893 "message after reauth");
894 return -1;
895 }
896
897 return 0;
898 }
899
900
901 static struct wpabuf * eap_sim_process_notification(
902 struct eap_sm *sm, struct eap_sim_data *data, u8 id,
903 const struct wpabuf *reqData, struct eap_sim_attrs *attr)
904 {
905 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Notification");
906 if (data->num_notification > 0) {
907 wpa_printf(MSG_INFO, "EAP-SIM: too many notification "
908 "rounds (only one allowed)");
909 return eap_sim_client_error(data, id,
910 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
911 }
912 data->num_notification++;
913 if (attr->notification == -1) {
914 wpa_printf(MSG_INFO, "EAP-SIM: no AT_NOTIFICATION in "
915 "Notification message");
916 return eap_sim_client_error(data, id,
917 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
918 }
919
920 if ((attr->notification & 0x4000) == 0 &&
921 eap_sim_process_notification_auth(data, reqData, attr)) {
922 return eap_sim_client_error(data, id,
923 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
924 }
925
926 eap_sim_report_notification(sm->msg_ctx, attr->notification, 0);
927 if (attr->notification >= 0 && attr->notification < 32768) {
928 data->error_code = attr->notification;
929 eap_sim_state(data, FAILURE);
930 } else if (attr->notification == EAP_SIM_SUCCESS &&
931 data->state == RESULT_SUCCESS)
932 eap_sim_state(data, SUCCESS);
933 return eap_sim_response_notification(data, id, attr->notification);
934 }
935
936
937 static struct wpabuf * eap_sim_process_reauthentication(
938 struct eap_sm *sm, struct eap_sim_data *data, u8 id,
939 const struct wpabuf *reqData, struct eap_sim_attrs *attr)
940 {
941 struct eap_sim_attrs eattr;
942 u8 *decrypted;
943
944 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Reauthentication");
945
946 if (data->reauth_id == NULL) {
947 wpa_printf(MSG_WARNING, "EAP-SIM: Server is trying "
948 "reauthentication, but no reauth_id available");
949 return eap_sim_client_error(data, id,
950 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
951 }
952
953 data->reauth = 1;
954 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
955 {
956 wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
957 "did not have valid AT_MAC");
958 return eap_sim_client_error(data, id,
959 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
960 }
961
962 /* At this stage the received MAC has been verified. Use this MAC for
963 * reauth Session-Id calculation if all other checks pass.
964 * The peer does not use the local MAC but the received MAC in deriving
965 * Session-Id. */
966 os_memcpy(data->reauth_mac, attr->mac, EAP_SIM_MAC_LEN);
967 wpa_hexdump(MSG_DEBUG, "EAP-SIM: Server MAC",
968 data->reauth_mac, EAP_SIM_MAC_LEN);
969
970 if (attr->encr_data == NULL || attr->iv == NULL) {
971 wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
972 "message did not include encrypted data");
973 return eap_sim_client_error(data, id,
974 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
975 }
976
977 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
978 attr->encr_data_len, attr->iv, &eattr,
979 0);
980 if (decrypted == NULL) {
981 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
982 "data from reauthentication message");
983 return eap_sim_client_error(data, id,
984 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
985 }
986
987 if (eattr.nonce_s == NULL || eattr.counter < 0) {
988 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No%s%s in reauth packet",
989 !eattr.nonce_s ? " AT_NONCE_S" : "",
990 eattr.counter < 0 ? " AT_COUNTER" : "");
991 os_free(decrypted);
992 return eap_sim_client_error(data, id,
993 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
994 }
995
996 if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
997 struct wpabuf *res;
998 wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid counter "
999 "(%d <= %d)", eattr.counter, data->counter);
1000 data->counter_too_small = eattr.counter;
1001
1002 /* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
1003 * reauth_id must not be used to start a new reauthentication.
1004 * However, since it was used in the last EAP-Response-Identity
1005 * packet, it has to saved for the following fullauth to be
1006 * used in MK derivation. */
1007 os_free(data->last_eap_identity);
1008 data->last_eap_identity = data->reauth_id;
1009 data->last_eap_identity_len = data->reauth_id_len;
1010 data->reauth_id = NULL;
1011 data->reauth_id_len = 0;
1012
1013 res = eap_sim_response_reauth(data, id, 1, eattr.nonce_s);
1014 os_free(decrypted);
1015
1016 return res;
1017 }
1018 data->counter = eattr.counter;
1019
1020 os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
1021 wpa_hexdump(MSG_DEBUG, "EAP-SIM: (encr) AT_NONCE_S",
1022 data->nonce_s, EAP_SIM_NONCE_S_LEN);
1023
1024 eap_sim_derive_keys_reauth(data->counter,
1025 data->reauth_id, data->reauth_id_len,
1026 data->nonce_s, data->mk, data->msk,
1027 data->emsk);
1028 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1029 eap_sim_learn_ids(sm, data, &eattr);
1030
1031 if (data->result_ind && attr->result_ind)
1032 data->use_result_ind = 1;
1033
1034 if (data->state != FAILURE) {
1035 eap_sim_state(data, data->use_result_ind ?
1036 RESULT_SUCCESS : SUCCESS);
1037 }
1038
1039 data->num_id_req = 0;
1040 data->num_notification = 0;
1041 if (data->counter > EAP_SIM_MAX_FAST_REAUTHS) {
1042 wpa_printf(MSG_DEBUG, "EAP-SIM: Maximum number of "
1043 "fast reauths performed - force fullauth");
1044 eap_sim_clear_identities(sm, data,
1045 CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1046 }
1047 os_free(decrypted);
1048 return eap_sim_response_reauth(data, id, 0, data->nonce_s);
1049 }
1050
1051
1052 static struct wpabuf * eap_sim_process(struct eap_sm *sm, void *priv,
1053 struct eap_method_ret *ret,
1054 const struct wpabuf *reqData)
1055 {
1056 struct eap_sim_data *data = priv;
1057 const struct eap_hdr *req;
1058 u8 subtype, id;
1059 struct wpabuf *res;
1060 const u8 *pos;
1061 struct eap_sim_attrs attr;
1062 size_t len;
1063
1064 wpa_hexdump_buf(MSG_DEBUG, "EAP-SIM: EAP data", reqData);
1065 if (eap_get_config_identity(sm, &len) == NULL) {
1066 wpa_printf(MSG_INFO, "EAP-SIM: Identity not configured");
1067 eap_sm_request_identity(sm);
1068 ret->ignore = TRUE;
1069 return NULL;
1070 }
1071
1072 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, reqData, &len);
1073 if (pos == NULL || len < 3) {
1074 ret->ignore = TRUE;
1075 return NULL;
1076 }
1077 req = wpabuf_head(reqData);
1078 id = req->identifier;
1079 len = be_to_host16(req->length);
1080
1081 ret->ignore = FALSE;
1082 ret->methodState = METHOD_MAY_CONT;
1083 ret->decision = DECISION_FAIL;
1084 ret->allowNotifications = TRUE;
1085
1086 subtype = *pos++;
1087 wpa_printf(MSG_DEBUG, "EAP-SIM: Subtype=%d", subtype);
1088 pos += 2; /* Reserved */
1089
1090 if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 0,
1091 0)) {
1092 res = eap_sim_client_error(data, id,
1093 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1094 goto done;
1095 }
1096
1097 switch (subtype) {
1098 case EAP_SIM_SUBTYPE_START:
1099 res = eap_sim_process_start(sm, data, id, &attr);
1100 break;
1101 case EAP_SIM_SUBTYPE_CHALLENGE:
1102 res = eap_sim_process_challenge(sm, data, id, reqData, &attr);
1103 break;
1104 case EAP_SIM_SUBTYPE_NOTIFICATION:
1105 res = eap_sim_process_notification(sm, data, id, reqData,
1106 &attr);
1107 break;
1108 case EAP_SIM_SUBTYPE_REAUTHENTICATION:
1109 res = eap_sim_process_reauthentication(sm, data, id, reqData,
1110 &attr);
1111 break;
1112 case EAP_SIM_SUBTYPE_CLIENT_ERROR:
1113 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Client-Error");
1114 res = eap_sim_client_error(data, id,
1115 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1116 break;
1117 default:
1118 wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown subtype=%d", subtype);
1119 res = eap_sim_client_error(data, id,
1120 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1121 break;
1122 }
1123
1124 done:
1125 if (data->state == FAILURE) {
1126 ret->decision = DECISION_FAIL;
1127 ret->methodState = METHOD_DONE;
1128 } else if (data->state == SUCCESS) {
1129 ret->decision = data->use_result_ind ?
1130 DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
1131 ret->methodState = data->use_result_ind ?
1132 METHOD_DONE : METHOD_MAY_CONT;
1133 } else if (data->state == RESULT_SUCCESS)
1134 ret->methodState = METHOD_CONT;
1135
1136 if (ret->methodState == METHOD_DONE) {
1137 ret->allowNotifications = FALSE;
1138 }
1139
1140 return res;
1141 }
1142
1143
1144 static Boolean eap_sim_has_reauth_data(struct eap_sm *sm, void *priv)
1145 {
1146 struct eap_sim_data *data = priv;
1147 return data->pseudonym || data->reauth_id;
1148 }
1149
1150
1151 static void eap_sim_deinit_for_reauth(struct eap_sm *sm, void *priv)
1152 {
1153 struct eap_sim_data *data = priv;
1154 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
1155 data->use_result_ind = 0;
1156 eap_sim_clear_keys(data, 1);
1157 }
1158
1159
1160 static void * eap_sim_init_for_reauth(struct eap_sm *sm, void *priv)
1161 {
1162 struct eap_sim_data *data = priv;
1163 if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
1164 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
1165 "for NONCE_MT");
1166 eap_sim_deinit(sm, data);
1167 return NULL;
1168 }
1169 data->num_id_req = 0;
1170 data->num_notification = 0;
1171 eap_sim_state(data, CONTINUE);
1172 return priv;
1173 }
1174
1175
1176 static const u8 * eap_sim_get_identity(struct eap_sm *sm, void *priv,
1177 size_t *len)
1178 {
1179 struct eap_sim_data *data = priv;
1180
1181 if (data->reauth_id) {
1182 *len = data->reauth_id_len;
1183 return data->reauth_id;
1184 }
1185
1186 if (data->pseudonym) {
1187 *len = data->pseudonym_len;
1188 return data->pseudonym;
1189 }
1190
1191 return NULL;
1192 }
1193
1194
1195 static Boolean eap_sim_isKeyAvailable(struct eap_sm *sm, void *priv)
1196 {
1197 struct eap_sim_data *data = priv;
1198 return data->state == SUCCESS;
1199 }
1200
1201
1202 static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
1203 {
1204 struct eap_sim_data *data = priv;
1205 u8 *key;
1206
1207 if (data->state != SUCCESS)
1208 return NULL;
1209
1210 key = os_memdup(data->msk, EAP_SIM_KEYING_DATA_LEN);
1211 if (key == NULL)
1212 return NULL;
1213
1214 *len = EAP_SIM_KEYING_DATA_LEN;
1215
1216 return key;
1217 }
1218
1219
1220 static u8 * eap_sim_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1221 {
1222 struct eap_sim_data *data = priv;
1223 u8 *id;
1224
1225 if (data->state != SUCCESS)
1226 return NULL;
1227
1228 if (!data->reauth)
1229 *len = 1 + data->num_chal * GSM_RAND_LEN + EAP_SIM_NONCE_MT_LEN;
1230 else
1231 *len = 1 + EAP_SIM_NONCE_S_LEN + EAP_SIM_MAC_LEN;
1232 id = os_malloc(*len);
1233 if (id == NULL)
1234 return NULL;
1235
1236 id[0] = EAP_TYPE_SIM;
1237 if (!data->reauth) {
1238 os_memcpy(id + 1, data->rand, data->num_chal * GSM_RAND_LEN);
1239 os_memcpy(id + 1 + data->num_chal * GSM_RAND_LEN,
1240 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
1241 } else {
1242 os_memcpy(id + 1, data->nonce_s, EAP_SIM_NONCE_S_LEN);
1243 os_memcpy(id + 1 + EAP_SIM_NONCE_S_LEN, data->reauth_mac,
1244 EAP_SIM_MAC_LEN);
1245 }
1246 wpa_hexdump(MSG_DEBUG, "EAP-SIM: Derived Session-Id", id, *len);
1247
1248 return id;
1249 }
1250
1251
1252 static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1253 {
1254 struct eap_sim_data *data = priv;
1255 u8 *key;
1256
1257 if (data->state != SUCCESS)
1258 return NULL;
1259
1260 key = os_memdup(data->emsk, EAP_EMSK_LEN);
1261 if (key == NULL)
1262 return NULL;
1263
1264 *len = EAP_EMSK_LEN;
1265
1266 return key;
1267 }
1268
1269
1270 static int eap_sim_get_error_code(void *priv)
1271 {
1272 struct eap_sim_data *data = priv;
1273 int current_data_error;
1274
1275 if (!data)
1276 return NO_EAP_METHOD_ERROR;
1277
1278 current_data_error = data->error_code;
1279
1280 /* Now reset for next transaction */
1281 data->error_code = NO_EAP_METHOD_ERROR;
1282
1283 return current_data_error;
1284 }
1285
1286
1287 int eap_peer_sim_register(void)
1288 {
1289 struct eap_method *eap;
1290
1291 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1292 EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
1293 if (eap == NULL)
1294 return -1;
1295
1296 eap->init = eap_sim_init;
1297 eap->deinit = eap_sim_deinit;
1298 eap->process = eap_sim_process;
1299 eap->isKeyAvailable = eap_sim_isKeyAvailable;
1300 eap->getKey = eap_sim_getKey;
1301 eap->getSessionId = eap_sim_get_session_id;
1302 eap->has_reauth_data = eap_sim_has_reauth_data;
1303 eap->deinit_for_reauth = eap_sim_deinit_for_reauth;
1304 eap->init_for_reauth = eap_sim_init_for_reauth;
1305 eap->get_identity = eap_sim_get_identity;
1306 eap->get_emsk = eap_sim_get_emsk;
1307 eap->get_error_code = eap_sim_get_error_code;
1308
1309 return eap_peer_method_register(eap);
1310 }