2 * EAP peer method: EAP-SIM (RFC 4186)
3 * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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"
24 size_t min_num_chal
, num_chal
;
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
];
37 int num_id_req
, num_notification
;
43 unsigned int counter
, counter_too_small
;
44 u8
*last_eap_identity
;
45 size_t last_eap_identity_len
;
47 CONTINUE
, RESULT_SUCCESS
, SUCCESS
, FAILURE
49 int result_ind
, use_result_ind
;
55 #ifndef CONFIG_NO_STDOUT_DEBUG
56 static const char * eap_sim_state_txt(int state
)
62 return "RESULT_SUCCESS";
71 #endif /* CONFIG_NO_STDOUT_DEBUG */
74 static void eap_sim_state(struct eap_sim_data
*data
, int state
)
76 wpa_printf(MSG_DEBUG
, "EAP-SIM: %s -> %s",
77 eap_sim_state_txt(data
->state
),
78 eap_sim_state_txt(state
));
83 static void * eap_sim_init(struct eap_sm
*sm
)
85 struct eap_sim_data
*data
;
86 struct eap_peer_config
*config
= eap_get_config(sm
);
88 data
= os_zalloc(sizeof(*data
));
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 "
99 /* Zero is a valid error code, so we need to initialize */
100 data
->error_code
= NO_EAP_METHOD_ERROR
;
102 data
->min_num_chal
= 2;
103 if (config
&& config
->phase1
) {
104 char *pos
= os_strstr(config
->phase1
, "sim_min_num_chal=");
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
);
115 wpa_printf(MSG_DEBUG
, "EAP-SIM: Set minimum number of "
117 (unsigned long) data
->min_num_chal
);
120 data
->result_ind
= os_strstr(config
->phase1
, "result_ind=1") !=
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
;
134 eap_sim_state(data
, CONTINUE
);
140 static void eap_sim_clear_keys(struct eap_sim_data
*data
, int 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
);
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
);
154 static void eap_sim_deinit(struct eap_sm
*sm
, void *priv
)
156 struct eap_sim_data
*data
= priv
;
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);
168 static int eap_sim_ext_sim_req(struct eap_sm
*sm
, struct eap_sim_data
*data
)
170 char req
[200], *pos
, *end
;
173 wpa_printf(MSG_DEBUG
, "EAP-SIM: Use external SIM processing");
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
],
183 eap_sm_request_sim(sm
, req
);
188 static int eap_sim_ext_sim_result(struct eap_sm
*sm
, struct eap_sim_data
*data
,
189 struct eap_peer_config
*conf
)
194 wpa_printf(MSG_DEBUG
,
195 "EAP-SIM: Use result from external SIM processing");
197 resp
= conf
->external_sim_resp
;
198 conf
->external_sim_resp
= NULL
;
200 if (os_strncmp(resp
, "GSM-AUTH:", 9) != 0) {
201 wpa_printf(MSG_DEBUG
, "EAP-SIM: Unrecognized external SIM processing response");
207 for (i
= 0; i
< data
->num_chal
; i
++) {
208 wpa_hexdump(MSG_DEBUG
, "EAP-SIM: RAND",
209 data
->rand
[i
], GSM_RAND_LEN
);
211 if (hexstr2bin(pos
, data
->kc
[i
], EAP_SIM_KC_LEN
) < 0)
213 wpa_hexdump_key(MSG_DEBUG
, "EAP-SIM: Kc",
214 data
->kc
[i
], EAP_SIM_KC_LEN
);
215 pos
+= EAP_SIM_KC_LEN
* 2;
220 if (hexstr2bin(pos
, data
->sres
[i
], EAP_SIM_SRES_LEN
) < 0)
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
) {
236 wpa_printf(MSG_DEBUG
, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
242 static int eap_sim_gsm_auth(struct eap_sm
*sm
, struct eap_sim_data
*data
)
244 struct eap_peer_config
*conf
;
246 wpa_printf(MSG_DEBUG
, "EAP-SIM: GSM authentication algorithm");
248 conf
= eap_get_config(sm
);
252 if (sm
->external_sim
) {
253 if (conf
->external_sim_resp
)
254 return eap_sim_ext_sim_result(sm
, data
, conf
);
256 return eap_sim_ext_sim_req(sm
, data
);
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");
274 #endif /* PCSC_FUNCS */
276 #ifdef CONFIG_SIM_SIMULATOR
277 if (conf
->password
) {
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 "
288 pos
= (const char *) conf
->password
;
289 if (hexstr2bin(pos
, k
, 16))
296 if (hexstr2bin(pos
, opc
, 16))
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");
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
);
316 #endif /* CONFIG_SIM_SIMULATOR */
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. */
326 wpa_printf(MSG_DEBUG
, "EAP-SIM: Use hardcoded Kc and SRES "
327 "values for testing");
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",
334 os_memcpy(data
->sres
[i
], "\xd1\xd2\xd3\xd4",
336 } else if (data
->rand
[i
][0] == 0xbb) {
337 os_memcpy(data
->kc
[i
],
338 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7",
340 os_memcpy(data
->sres
[i
], "\xe1\xe2\xe3\xe4",
343 os_memcpy(data
->kc
[i
],
344 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
346 os_memcpy(data
->sres
[i
], "\xf1\xf2\xf3\xf4",
354 #else /* CONFIG_SIM_HARDCODED */
356 wpa_printf(MSG_DEBUG
, "EAP-SIM: No GSM authentication algorithm "
360 #endif /* CONFIG_SIM_HARDCODED */
364 static int eap_sim_supported_ver(int version
)
366 return version
== EAP_SIM_VERSION
;
370 #define CLEAR_PSEUDONYM 0x01
371 #define CLEAR_REAUTH_ID 0x02
372 #define CLEAR_EAP_ID 0x04
374 static void eap_sim_clear_identities(struct eap_sm
*sm
,
375 struct eap_sim_data
*data
, int id
)
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);
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;
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;
400 static int eap_sim_learn_ids(struct eap_sm
*sm
, struct eap_sim_data
*data
,
401 struct eap_sim_attrs
*attr
)
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;
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
);
417 for (realm
= identity
, realm_len
= identity_len
;
418 realm_len
> 0; realm_len
--, realm
++) {
423 data
->pseudonym
= os_malloc(attr
->next_pseudonym_len
+
425 if (data
->pseudonym
== NULL
) {
426 wpa_printf(MSG_INFO
, "EAP-SIM: (encr) No memory for "
428 data
->pseudonym_len
= 0;
431 os_memcpy(data
->pseudonym
, attr
->next_pseudonym
,
432 attr
->next_pseudonym_len
);
434 os_memcpy(data
->pseudonym
+ attr
->next_pseudonym_len
,
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
);
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 "
450 data
->reauth_id_len
= 0;
453 data
->reauth_id_len
= attr
->next_reauth_id_len
;
454 wpa_hexdump_ascii(MSG_DEBUG
,
455 "EAP-SIM: (encr) AT_NEXT_REAUTH_ID",
457 data
->reauth_id_len
);
464 static struct wpabuf
* eap_sim_client_error(struct eap_sim_data
*data
, u8 id
,
467 struct eap_sim_msg
*msg
;
469 eap_sim_state(data
, FAILURE
);
470 data
->num_id_req
= 0;
471 data
->num_notification
= 0;
473 wpa_printf(MSG_DEBUG
, "EAP-SIM: Send Client-Error (error code %d)",
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);
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
)
486 const u8
*identity
= NULL
;
487 size_t identity_len
= 0;
488 struct eap_sim_msg
*msg
;
491 if (id_req
== ANY_ID
&& data
->reauth_id
) {
492 identity
= data
->reauth_id
;
493 identity_len
= data
->reauth_id_len
;
495 } else if ((id_req
== ANY_ID
|| id_req
== FULLAUTH_ID
) &&
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
);
503 eap_sim_clear_identities(sm
, data
, CLEAR_PSEUDONYM
|
507 if (id_req
!= NO_ID_REQ
)
508 eap_sim_clear_identities(sm
, data
, CLEAR_EAP_ID
);
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
);
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);
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
);
531 return eap_sim_msg_finish(msg
, EAP_TYPE_SIM
, NULL
, NULL
, 0);
535 static struct wpabuf
* eap_sim_response_challenge(struct eap_sim_data
*data
,
538 struct eap_sim_msg
*msg
;
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);
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
,
551 data
->num_chal
* EAP_SIM_SRES_LEN
);
555 static struct wpabuf
* eap_sim_response_reauth(struct eap_sim_data
*data
,
556 u8 id
, int counter_too_small
,
559 struct eap_sim_msg
*msg
;
560 unsigned int counter
;
562 wpa_printf(MSG_DEBUG
, "Generating EAP-SIM Reauthentication (id=%d)",
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
);
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
;
575 counter
= data
->counter
;
577 wpa_printf(MSG_DEBUG
, " *AT_COUNTER %d", counter
);
578 eap_sim_msg_add(msg
, EAP_SIM_AT_COUNTER
, counter
, NULL
, 0);
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 "
583 eap_sim_msg_free(msg
);
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);
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
);
597 static struct wpabuf
* eap_sim_response_notification(struct eap_sim_data
*data
,
598 u8 id
, u16 notification
)
600 struct eap_sim_msg
*msg
;
601 u8
*k_aut
= (notification
& 0x4000) == 0 ? data
->k_aut
: NULL
;
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
,
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 "
618 eap_sim_msg_free(msg
);
623 wpa_printf(MSG_DEBUG
, " AT_MAC");
624 eap_sim_msg_add_mac(msg
, EAP_SIM_AT_MAC
);
626 return eap_sim_msg_finish(msg
, EAP_TYPE_SIM
, k_aut
, (u8
*) "", 0);
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
)
634 int selected_version
= -1, id_error
;
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 "
642 return eap_sim_client_error(data
, id
,
643 EAP_SIM_UNSUPPORTED_VERSION
);
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
);
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];
659 if (eap_sim_supported_ver(ver
)) {
660 selected_version
= ver
;
664 if (selected_version
< 0) {
665 wpa_printf(MSG_INFO
, "EAP-SIM: Could not find a supported "
667 return eap_sim_client_error(data
, id
,
668 EAP_SIM_UNSUPPORTED_VERSION
);
670 wpa_printf(MSG_DEBUG
, "EAP-SIM: Selected Version %d",
672 data
->selected_version
= selected_version
;
675 switch (attr
->id_req
) {
679 if (data
->num_id_req
> 0)
684 if (data
->num_id_req
> 1)
689 if (data
->num_id_req
> 2)
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
);
701 return eap_sim_response_start(sm
, data
, id
, attr
->id_req
);
705 static struct wpabuf
* eap_sim_process_challenge(struct eap_sm
*sm
,
706 struct eap_sim_data
*data
,
708 const struct wpabuf
*reqData
,
709 struct eap_sim_attrs
*attr
)
713 struct eap_sim_attrs eattr
;
716 wpa_printf(MSG_DEBUG
, "EAP-SIM: subtype Challenge");
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
);
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
);
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
);
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
);
756 os_memcpy(data
->rand
, attr
->rand
, attr
->num_chal
* GSM_RAND_LEN
);
757 data
->num_chal
= attr
->num_chal
;
759 res
= eap_sim_gsm_auth(sm
, data
);
761 wpa_printf(MSG_DEBUG
, "EAP-SIM: Wait for external SIM processing");
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
);
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
;
776 struct eap_peer_config
*config
;
778 config
= eap_get_config(sm
);
779 if (config
&& config
->imsi_identity
) {
780 identity
= config
->imsi_identity
;
781 identity_len
= config
->imsi_identity_len
;
783 identity
= eap_get_config_identity(sm
, &identity_len
);
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
,
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
);
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
);
808 if (attr
->encr_data
) {
810 decrypted
= eap_sim_parse_encr(data
->k_encr
, attr
->encr_data
,
811 attr
->encr_data_len
, attr
->iv
,
813 if (decrypted
== NULL
) {
814 return eap_sim_client_error(
815 data
, id
, EAP_SIM_UNABLE_TO_PROCESS_PACKET
);
817 eap_sim_learn_ids(sm
, data
, &eattr
);
821 if (data
->result_ind
&& attr
->result_ind
)
822 data
->use_result_ind
= 1;
824 if (data
->state
!= FAILURE
) {
825 eap_sim_state(data
, data
->use_result_ind
?
826 RESULT_SUCCESS
: SUCCESS
);
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. */
835 return eap_sim_response_challenge(data
, id
);
839 static int eap_sim_process_notification_reauth(struct eap_sim_data
*data
,
840 struct eap_sim_attrs
*attr
)
842 struct eap_sim_attrs eattr
;
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");
851 decrypted
= eap_sim_parse_encr(data
->k_encr
, attr
->encr_data
,
852 attr
->encr_data_len
, attr
->iv
, &eattr
,
854 if (decrypted
== NULL
) {
855 wpa_printf(MSG_WARNING
, "EAP-SIM: Failed to parse encrypted "
856 "data from notification message");
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 "
873 static int eap_sim_process_notification_auth(struct eap_sim_data
*data
,
874 const struct wpabuf
*reqData
,
875 struct eap_sim_attrs
*attr
)
877 if (attr
->mac
== NULL
) {
878 wpa_printf(MSG_INFO
, "EAP-SIM: no AT_MAC in after_auth "
879 "Notification message");
883 if (eap_sim_verify_mac(data
->k_aut
, reqData
, attr
->mac
, (u8
*) "", 0))
885 wpa_printf(MSG_WARNING
, "EAP-SIM: Notification message "
886 "used invalid AT_MAC");
891 eap_sim_process_notification_reauth(data
, attr
)) {
892 wpa_printf(MSG_WARNING
, "EAP-SIM: Invalid notification "
893 "message after reauth");
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
)
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
);
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
);
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
);
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
);
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
)
941 struct eap_sim_attrs eattr
;
944 wpa_printf(MSG_DEBUG
, "EAP-SIM: subtype Reauthentication");
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
);
954 if (eap_sim_verify_mac(data
->k_aut
, reqData
, attr
->mac
, (u8
*) "", 0))
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
);
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
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
);
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
);
977 decrypted
= eap_sim_parse_encr(data
->k_encr
, attr
->encr_data
,
978 attr
->encr_data_len
, attr
->iv
, &eattr
,
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
);
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" : "");
992 return eap_sim_client_error(data
, id
,
993 EAP_SIM_UNABLE_TO_PROCESS_PACKET
);
996 if (eattr
.counter
< 0 || (size_t) eattr
.counter
<= data
->counter
) {
998 wpa_printf(MSG_INFO
, "EAP-SIM: (encr) Invalid counter "
999 "(%d <= %d)", eattr
.counter
, data
->counter
);
1000 data
->counter_too_small
= eattr
.counter
;
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;
1013 res
= eap_sim_response_reauth(data
, id
, 1, eattr
.nonce_s
);
1018 data
->counter
= eattr
.counter
;
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
);
1024 eap_sim_derive_keys_reauth(data
->counter
,
1025 data
->reauth_id
, data
->reauth_id_len
,
1026 data
->nonce_s
, data
->mk
, data
->msk
,
1028 eap_sim_clear_identities(sm
, data
, CLEAR_REAUTH_ID
| CLEAR_EAP_ID
);
1029 eap_sim_learn_ids(sm
, data
, &eattr
);
1031 if (data
->result_ind
&& attr
->result_ind
)
1032 data
->use_result_ind
= 1;
1034 if (data
->state
!= FAILURE
) {
1035 eap_sim_state(data
, data
->use_result_ind
?
1036 RESULT_SUCCESS
: SUCCESS
);
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
);
1048 return eap_sim_response_reauth(data
, id
, 0, data
->nonce_s
);
1052 static struct wpabuf
* eap_sim_process(struct eap_sm
*sm
, void *priv
,
1053 struct eap_method_ret
*ret
,
1054 const struct wpabuf
*reqData
)
1056 struct eap_sim_data
*data
= priv
;
1057 const struct eap_hdr
*req
;
1061 struct eap_sim_attrs attr
;
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
);
1072 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_SIM
, reqData
, &len
);
1073 if (pos
== NULL
|| len
< 3) {
1077 req
= wpabuf_head(reqData
);
1078 id
= req
->identifier
;
1079 len
= be_to_host16(req
->length
);
1081 ret
->ignore
= FALSE
;
1082 ret
->methodState
= METHOD_MAY_CONT
;
1083 ret
->decision
= DECISION_FAIL
;
1084 ret
->allowNotifications
= TRUE
;
1087 wpa_printf(MSG_DEBUG
, "EAP-SIM: Subtype=%d", subtype
);
1088 pos
+= 2; /* Reserved */
1090 if (eap_sim_parse_attr(pos
, wpabuf_head_u8(reqData
) + len
, &attr
, 0,
1092 res
= eap_sim_client_error(data
, id
,
1093 EAP_SIM_UNABLE_TO_PROCESS_PACKET
);
1098 case EAP_SIM_SUBTYPE_START
:
1099 res
= eap_sim_process_start(sm
, data
, id
, &attr
);
1101 case EAP_SIM_SUBTYPE_CHALLENGE
:
1102 res
= eap_sim_process_challenge(sm
, data
, id
, reqData
, &attr
);
1104 case EAP_SIM_SUBTYPE_NOTIFICATION
:
1105 res
= eap_sim_process_notification(sm
, data
, id
, reqData
,
1108 case EAP_SIM_SUBTYPE_REAUTHENTICATION
:
1109 res
= eap_sim_process_reauthentication(sm
, data
, id
, reqData
,
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
);
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
);
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
;
1136 if (ret
->methodState
== METHOD_DONE
) {
1137 ret
->allowNotifications
= FALSE
;
1144 static Boolean
eap_sim_has_reauth_data(struct eap_sm
*sm
, void *priv
)
1146 struct eap_sim_data
*data
= priv
;
1147 return data
->pseudonym
|| data
->reauth_id
;
1151 static void eap_sim_deinit_for_reauth(struct eap_sm
*sm
, void *priv
)
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);
1160 static void * eap_sim_init_for_reauth(struct eap_sm
*sm
, void *priv
)
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 "
1166 eap_sim_deinit(sm
, data
);
1169 data
->num_id_req
= 0;
1170 data
->num_notification
= 0;
1171 eap_sim_state(data
, CONTINUE
);
1176 static const u8
* eap_sim_get_identity(struct eap_sm
*sm
, void *priv
,
1179 struct eap_sim_data
*data
= priv
;
1181 if (data
->reauth_id
) {
1182 *len
= data
->reauth_id_len
;
1183 return data
->reauth_id
;
1186 if (data
->pseudonym
) {
1187 *len
= data
->pseudonym_len
;
1188 return data
->pseudonym
;
1195 static Boolean
eap_sim_isKeyAvailable(struct eap_sm
*sm
, void *priv
)
1197 struct eap_sim_data
*data
= priv
;
1198 return data
->state
== SUCCESS
;
1202 static u8
* eap_sim_getKey(struct eap_sm
*sm
, void *priv
, size_t *len
)
1204 struct eap_sim_data
*data
= priv
;
1207 if (data
->state
!= SUCCESS
)
1210 key
= os_memdup(data
->msk
, EAP_SIM_KEYING_DATA_LEN
);
1214 *len
= EAP_SIM_KEYING_DATA_LEN
;
1220 static u8
* eap_sim_get_session_id(struct eap_sm
*sm
, void *priv
, size_t *len
)
1222 struct eap_sim_data
*data
= priv
;
1225 if (data
->state
!= SUCCESS
)
1229 *len
= 1 + data
->num_chal
* GSM_RAND_LEN
+ EAP_SIM_NONCE_MT_LEN
;
1231 *len
= 1 + EAP_SIM_NONCE_S_LEN
+ EAP_SIM_MAC_LEN
;
1232 id
= os_malloc(*len
);
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
);
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
,
1246 wpa_hexdump(MSG_DEBUG
, "EAP-SIM: Derived Session-Id", id
, *len
);
1252 static u8
* eap_sim_get_emsk(struct eap_sm
*sm
, void *priv
, size_t *len
)
1254 struct eap_sim_data
*data
= priv
;
1257 if (data
->state
!= SUCCESS
)
1260 key
= os_memdup(data
->emsk
, EAP_EMSK_LEN
);
1264 *len
= EAP_EMSK_LEN
;
1270 static int eap_sim_get_error_code(void *priv
)
1272 struct eap_sim_data
*data
= priv
;
1273 int current_data_error
;
1276 return NO_EAP_METHOD_ERROR
;
1278 current_data_error
= data
->error_code
;
1280 /* Now reset for next transaction */
1281 data
->error_code
= NO_EAP_METHOD_ERROR
;
1283 return current_data_error
;
1287 int eap_peer_sim_register(void)
1289 struct eap_method
*eap
;
1291 eap
= eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION
,
1292 EAP_VENDOR_IETF
, EAP_TYPE_SIM
, "SIM");
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
;
1309 return eap_peer_method_register(eap
);