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