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
];
36 int num_id_req
, num_notification
;
42 unsigned int counter
, counter_too_small
;
43 u8
*last_eap_identity
;
44 size_t last_eap_identity_len
;
46 CONTINUE
, RESULT_SUCCESS
, SUCCESS
, FAILURE
48 int result_ind
, use_result_ind
;
53 #ifndef CONFIG_NO_STDOUT_DEBUG
54 static const char * eap_sim_state_txt(int state
)
60 return "RESULT_SUCCESS";
69 #endif /* CONFIG_NO_STDOUT_DEBUG */
72 static void eap_sim_state(struct eap_sim_data
*data
, int state
)
74 wpa_printf(MSG_DEBUG
, "EAP-SIM: %s -> %s",
75 eap_sim_state_txt(data
->state
),
76 eap_sim_state_txt(state
));
81 static void * eap_sim_init(struct eap_sm
*sm
)
83 struct eap_sim_data
*data
;
84 struct eap_peer_config
*config
= eap_get_config(sm
);
86 data
= os_zalloc(sizeof(*data
));
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 "
97 data
->min_num_chal
= 2;
98 if (config
&& config
->phase1
) {
99 char *pos
= os_strstr(config
->phase1
, "sim_min_num_chal=");
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
);
110 wpa_printf(MSG_DEBUG
, "EAP-SIM: Set minimum number of "
112 (unsigned long) data
->min_num_chal
);
115 data
->result_ind
= os_strstr(config
->phase1
, "result_ind=1") !=
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
;
129 eap_sim_state(data
, CONTINUE
);
135 static void eap_sim_clear_keys(struct eap_sim_data
*data
, int 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
);
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
);
149 static void eap_sim_deinit(struct eap_sm
*sm
, void *priv
)
151 struct eap_sim_data
*data
= priv
;
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);
163 static int eap_sim_ext_sim_req(struct eap_sm
*sm
, struct eap_sim_data
*data
)
165 char req
[200], *pos
, *end
;
168 wpa_printf(MSG_DEBUG
, "EAP-SIM: Use external SIM processing");
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
],
178 eap_sm_request_sim(sm
, req
);
183 static int eap_sim_ext_sim_result(struct eap_sm
*sm
, struct eap_sim_data
*data
,
184 struct eap_peer_config
*conf
)
189 wpa_printf(MSG_DEBUG
,
190 "EAP-SIM: Use result from external SIM processing");
192 resp
= conf
->external_sim_resp
;
193 conf
->external_sim_resp
= NULL
;
195 if (os_strncmp(resp
, "GSM-AUTH:", 9) != 0) {
196 wpa_printf(MSG_DEBUG
, "EAP-SIM: Unrecognized external SIM processing response");
202 for (i
= 0; i
< data
->num_chal
; i
++) {
203 wpa_hexdump(MSG_DEBUG
, "EAP-SIM: RAND",
204 data
->rand
[i
], GSM_RAND_LEN
);
206 if (hexstr2bin(pos
, data
->kc
[i
], EAP_SIM_KC_LEN
) < 0)
208 wpa_hexdump_key(MSG_DEBUG
, "EAP-SIM: Kc",
209 data
->kc
[i
], EAP_SIM_KC_LEN
);
210 pos
+= EAP_SIM_KC_LEN
* 2;
215 if (hexstr2bin(pos
, data
->sres
[i
], EAP_SIM_SRES_LEN
) < 0)
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
) {
231 wpa_printf(MSG_DEBUG
, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
237 static int eap_sim_gsm_auth(struct eap_sm
*sm
, struct eap_sim_data
*data
)
239 struct eap_peer_config
*conf
;
241 wpa_printf(MSG_DEBUG
, "EAP-SIM: GSM authentication algorithm");
243 conf
= eap_get_config(sm
);
247 if (sm
->external_sim
) {
248 if (conf
->external_sim_resp
)
249 return eap_sim_ext_sim_result(sm
, data
, conf
);
251 return eap_sim_ext_sim_req(sm
, data
);
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");
269 #endif /* PCSC_FUNCS */
271 #ifdef CONFIG_SIM_SIMULATOR
272 if (conf
->password
) {
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 "
283 pos
= (const char *) conf
->password
;
284 if (hexstr2bin(pos
, k
, 16))
291 if (hexstr2bin(pos
, opc
, 16))
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");
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
);
311 #endif /* CONFIG_SIM_SIMULATOR */
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. */
321 wpa_printf(MSG_DEBUG
, "EAP-SIM: Use hardcoded Kc and SRES "
322 "values for testing");
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",
329 os_memcpy(data
->sres
[i
], "\xd1\xd2\xd3\xd4",
331 } else if (data
->rand
[i
][0] == 0xbb) {
332 os_memcpy(data
->kc
[i
],
333 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7",
335 os_memcpy(data
->sres
[i
], "\xe1\xe2\xe3\xe4",
338 os_memcpy(data
->kc
[i
],
339 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
341 os_memcpy(data
->sres
[i
], "\xf1\xf2\xf3\xf4",
349 #else /* CONFIG_SIM_HARDCODED */
351 wpa_printf(MSG_DEBUG
, "EAP-SIM: No GSM authentication algorithm "
355 #endif /* CONFIG_SIM_HARDCODED */
359 static int eap_sim_supported_ver(int version
)
361 return version
== EAP_SIM_VERSION
;
365 #define CLEAR_PSEUDONYM 0x01
366 #define CLEAR_REAUTH_ID 0x02
367 #define CLEAR_EAP_ID 0x04
369 static void eap_sim_clear_identities(struct eap_sm
*sm
,
370 struct eap_sim_data
*data
, int id
)
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);
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;
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;
395 static int eap_sim_learn_ids(struct eap_sm
*sm
, struct eap_sim_data
*data
,
396 struct eap_sim_attrs
*attr
)
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;
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
);
412 for (realm
= identity
, realm_len
= identity_len
;
413 realm_len
> 0; realm_len
--, realm
++) {
418 data
->pseudonym
= os_malloc(attr
->next_pseudonym_len
+
420 if (data
->pseudonym
== NULL
) {
421 wpa_printf(MSG_INFO
, "EAP-SIM: (encr) No memory for "
423 data
->pseudonym_len
= 0;
426 os_memcpy(data
->pseudonym
, attr
->next_pseudonym
,
427 attr
->next_pseudonym_len
);
429 os_memcpy(data
->pseudonym
+ attr
->next_pseudonym_len
,
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
);
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 "
445 data
->reauth_id_len
= 0;
448 data
->reauth_id_len
= attr
->next_reauth_id_len
;
449 wpa_hexdump_ascii(MSG_DEBUG
,
450 "EAP-SIM: (encr) AT_NEXT_REAUTH_ID",
452 data
->reauth_id_len
);
459 static struct wpabuf
* eap_sim_client_error(struct eap_sim_data
*data
, u8 id
,
462 struct eap_sim_msg
*msg
;
464 eap_sim_state(data
, FAILURE
);
465 data
->num_id_req
= 0;
466 data
->num_notification
= 0;
468 wpa_printf(MSG_DEBUG
, "EAP-SIM: Send Client-Error (error code %d)",
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);
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
)
481 const u8
*identity
= NULL
;
482 size_t identity_len
= 0;
483 struct eap_sim_msg
*msg
;
486 if (id_req
== ANY_ID
&& data
->reauth_id
) {
487 identity
= data
->reauth_id
;
488 identity_len
= data
->reauth_id_len
;
490 } else if ((id_req
== ANY_ID
|| id_req
== FULLAUTH_ID
) &&
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
);
498 eap_sim_clear_identities(sm
, data
, CLEAR_PSEUDONYM
|
502 if (id_req
!= NO_ID_REQ
)
503 eap_sim_clear_identities(sm
, data
, CLEAR_EAP_ID
);
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
);
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);
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
);
526 return eap_sim_msg_finish(msg
, EAP_TYPE_SIM
, NULL
, NULL
, 0);
530 static struct wpabuf
* eap_sim_response_challenge(struct eap_sim_data
*data
,
533 struct eap_sim_msg
*msg
;
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);
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
,
546 data
->num_chal
* EAP_SIM_SRES_LEN
);
550 static struct wpabuf
* eap_sim_response_reauth(struct eap_sim_data
*data
,
551 u8 id
, int counter_too_small
,
554 struct eap_sim_msg
*msg
;
555 unsigned int counter
;
557 wpa_printf(MSG_DEBUG
, "Generating EAP-SIM Reauthentication (id=%d)",
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
);
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
;
570 counter
= data
->counter
;
572 wpa_printf(MSG_DEBUG
, " *AT_COUNTER %d", counter
);
573 eap_sim_msg_add(msg
, EAP_SIM_AT_COUNTER
, counter
, NULL
, 0);
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 "
578 eap_sim_msg_free(msg
);
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);
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
);
592 static struct wpabuf
* eap_sim_response_notification(struct eap_sim_data
*data
,
593 u8 id
, u16 notification
)
595 struct eap_sim_msg
*msg
;
596 u8
*k_aut
= (notification
& 0x4000) == 0 ? data
->k_aut
: NULL
;
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
,
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 "
613 eap_sim_msg_free(msg
);
618 wpa_printf(MSG_DEBUG
, " AT_MAC");
619 eap_sim_msg_add_mac(msg
, EAP_SIM_AT_MAC
);
621 return eap_sim_msg_finish(msg
, EAP_TYPE_SIM
, k_aut
, (u8
*) "", 0);
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
)
629 int selected_version
= -1, id_error
;
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 "
637 return eap_sim_client_error(data
, id
,
638 EAP_SIM_UNSUPPORTED_VERSION
);
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
);
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];
654 if (eap_sim_supported_ver(ver
)) {
655 selected_version
= ver
;
659 if (selected_version
< 0) {
660 wpa_printf(MSG_INFO
, "EAP-SIM: Could not find a supported "
662 return eap_sim_client_error(data
, id
,
663 EAP_SIM_UNSUPPORTED_VERSION
);
665 wpa_printf(MSG_DEBUG
, "EAP-SIM: Selected Version %d",
667 data
->selected_version
= selected_version
;
670 switch (attr
->id_req
) {
674 if (data
->num_id_req
> 0)
679 if (data
->num_id_req
> 1)
684 if (data
->num_id_req
> 2)
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
);
696 return eap_sim_response_start(sm
, data
, id
, attr
->id_req
);
700 static struct wpabuf
* eap_sim_process_challenge(struct eap_sm
*sm
,
701 struct eap_sim_data
*data
,
703 const struct wpabuf
*reqData
,
704 struct eap_sim_attrs
*attr
)
708 struct eap_sim_attrs eattr
;
711 wpa_printf(MSG_DEBUG
, "EAP-SIM: subtype Challenge");
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
);
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
);
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
);
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
);
751 os_memcpy(data
->rand
, attr
->rand
, attr
->num_chal
* GSM_RAND_LEN
);
752 data
->num_chal
= attr
->num_chal
;
754 res
= eap_sim_gsm_auth(sm
, data
);
756 wpa_printf(MSG_DEBUG
, "EAP-SIM: Wait for external SIM processing");
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
);
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
;
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
,
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
);
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
);
794 if (attr
->encr_data
) {
796 decrypted
= eap_sim_parse_encr(data
->k_encr
, attr
->encr_data
,
797 attr
->encr_data_len
, attr
->iv
,
799 if (decrypted
== NULL
) {
800 return eap_sim_client_error(
801 data
, id
, EAP_SIM_UNABLE_TO_PROCESS_PACKET
);
803 eap_sim_learn_ids(sm
, data
, &eattr
);
807 if (data
->result_ind
&& attr
->result_ind
)
808 data
->use_result_ind
= 1;
810 if (data
->state
!= FAILURE
) {
811 eap_sim_state(data
, data
->use_result_ind
?
812 RESULT_SUCCESS
: SUCCESS
);
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. */
821 return eap_sim_response_challenge(data
, id
);
825 static int eap_sim_process_notification_reauth(struct eap_sim_data
*data
,
826 struct eap_sim_attrs
*attr
)
828 struct eap_sim_attrs eattr
;
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");
837 decrypted
= eap_sim_parse_encr(data
->k_encr
, attr
->encr_data
,
838 attr
->encr_data_len
, attr
->iv
, &eattr
,
840 if (decrypted
== NULL
) {
841 wpa_printf(MSG_WARNING
, "EAP-SIM: Failed to parse encrypted "
842 "data from notification message");
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 "
859 static int eap_sim_process_notification_auth(struct eap_sim_data
*data
,
860 const struct wpabuf
*reqData
,
861 struct eap_sim_attrs
*attr
)
863 if (attr
->mac
== NULL
) {
864 wpa_printf(MSG_INFO
, "EAP-SIM: no AT_MAC in after_auth "
865 "Notification message");
869 if (eap_sim_verify_mac(data
->k_aut
, reqData
, attr
->mac
, (u8
*) "", 0))
871 wpa_printf(MSG_WARNING
, "EAP-SIM: Notification message "
872 "used invalid AT_MAC");
877 eap_sim_process_notification_reauth(data
, attr
)) {
878 wpa_printf(MSG_WARNING
, "EAP-SIM: Invalid notification "
879 "message after reauth");
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
)
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
);
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
);
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
);
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
);
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
)
926 struct eap_sim_attrs eattr
;
929 wpa_printf(MSG_DEBUG
, "EAP-SIM: subtype Reauthentication");
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
);
939 if (eap_sim_verify_mac(data
->k_aut
, reqData
, attr
->mac
, (u8
*) "", 0))
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
);
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
);
954 decrypted
= eap_sim_parse_encr(data
->k_encr
, attr
->encr_data
,
955 attr
->encr_data_len
, attr
->iv
, &eattr
,
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
);
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" : "");
969 return eap_sim_client_error(data
, id
,
970 EAP_SIM_UNABLE_TO_PROCESS_PACKET
);
973 if (eattr
.counter
< 0 || (size_t) eattr
.counter
<= data
->counter
) {
975 wpa_printf(MSG_INFO
, "EAP-SIM: (encr) Invalid counter "
976 "(%d <= %d)", eattr
.counter
, data
->counter
);
977 data
->counter_too_small
= eattr
.counter
;
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;
990 res
= eap_sim_response_reauth(data
, id
, 1, eattr
.nonce_s
);
995 data
->counter
= eattr
.counter
;
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
);
1001 eap_sim_derive_keys_reauth(data
->counter
,
1002 data
->reauth_id
, data
->reauth_id_len
,
1003 data
->nonce_s
, data
->mk
, data
->msk
,
1005 eap_sim_clear_identities(sm
, data
, CLEAR_REAUTH_ID
| CLEAR_EAP_ID
);
1006 eap_sim_learn_ids(sm
, data
, &eattr
);
1008 if (data
->result_ind
&& attr
->result_ind
)
1009 data
->use_result_ind
= 1;
1011 if (data
->state
!= FAILURE
) {
1012 eap_sim_state(data
, data
->use_result_ind
?
1013 RESULT_SUCCESS
: SUCCESS
);
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
);
1025 return eap_sim_response_reauth(data
, id
, 0, data
->nonce_s
);
1029 static struct wpabuf
* eap_sim_process(struct eap_sm
*sm
, void *priv
,
1030 struct eap_method_ret
*ret
,
1031 const struct wpabuf
*reqData
)
1033 struct eap_sim_data
*data
= priv
;
1034 const struct eap_hdr
*req
;
1038 struct eap_sim_attrs attr
;
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
);
1049 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_SIM
, reqData
, &len
);
1050 if (pos
== NULL
|| len
< 3) {
1054 req
= wpabuf_head(reqData
);
1055 id
= req
->identifier
;
1056 len
= be_to_host16(req
->length
);
1058 ret
->ignore
= FALSE
;
1059 ret
->methodState
= METHOD_MAY_CONT
;
1060 ret
->decision
= DECISION_FAIL
;
1061 ret
->allowNotifications
= TRUE
;
1064 wpa_printf(MSG_DEBUG
, "EAP-SIM: Subtype=%d", subtype
);
1065 pos
+= 2; /* Reserved */
1067 if (eap_sim_parse_attr(pos
, wpabuf_head_u8(reqData
) + len
, &attr
, 0,
1069 res
= eap_sim_client_error(data
, id
,
1070 EAP_SIM_UNABLE_TO_PROCESS_PACKET
);
1075 case EAP_SIM_SUBTYPE_START
:
1076 res
= eap_sim_process_start(sm
, data
, id
, &attr
);
1078 case EAP_SIM_SUBTYPE_CHALLENGE
:
1079 res
= eap_sim_process_challenge(sm
, data
, id
, reqData
, &attr
);
1081 case EAP_SIM_SUBTYPE_NOTIFICATION
:
1082 res
= eap_sim_process_notification(sm
, data
, id
, reqData
,
1085 case EAP_SIM_SUBTYPE_REAUTHENTICATION
:
1086 res
= eap_sim_process_reauthentication(sm
, data
, id
, reqData
,
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
);
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
);
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
;
1113 if (ret
->methodState
== METHOD_DONE
) {
1114 ret
->allowNotifications
= FALSE
;
1121 static Boolean
eap_sim_has_reauth_data(struct eap_sm
*sm
, void *priv
)
1123 struct eap_sim_data
*data
= priv
;
1124 return data
->pseudonym
|| data
->reauth_id
;
1128 static void eap_sim_deinit_for_reauth(struct eap_sm
*sm
, void *priv
)
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);
1137 static void * eap_sim_init_for_reauth(struct eap_sm
*sm
, void *priv
)
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 "
1143 eap_sim_deinit(sm
, data
);
1146 data
->num_id_req
= 0;
1147 data
->num_notification
= 0;
1148 eap_sim_state(data
, CONTINUE
);
1153 static const u8
* eap_sim_get_identity(struct eap_sm
*sm
, void *priv
,
1156 struct eap_sim_data
*data
= priv
;
1158 if (data
->reauth_id
) {
1159 *len
= data
->reauth_id_len
;
1160 return data
->reauth_id
;
1163 if (data
->pseudonym
) {
1164 *len
= data
->pseudonym_len
;
1165 return data
->pseudonym
;
1172 static Boolean
eap_sim_isKeyAvailable(struct eap_sm
*sm
, void *priv
)
1174 struct eap_sim_data
*data
= priv
;
1175 return data
->state
== SUCCESS
;
1179 static u8
* eap_sim_getKey(struct eap_sm
*sm
, void *priv
, size_t *len
)
1181 struct eap_sim_data
*data
= priv
;
1184 if (data
->state
!= SUCCESS
)
1187 key
= os_memdup(data
->msk
, EAP_SIM_KEYING_DATA_LEN
);
1191 *len
= EAP_SIM_KEYING_DATA_LEN
;
1197 static u8
* eap_sim_get_session_id(struct eap_sm
*sm
, void *priv
, size_t *len
)
1199 struct eap_sim_data
*data
= priv
;
1202 if (data
->state
!= SUCCESS
)
1205 *len
= 1 + data
->num_chal
* GSM_RAND_LEN
+ EAP_SIM_NONCE_MT_LEN
;
1206 id
= os_malloc(*len
);
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
);
1220 static u8
* eap_sim_get_emsk(struct eap_sm
*sm
, void *priv
, size_t *len
)
1222 struct eap_sim_data
*data
= priv
;
1225 if (data
->state
!= SUCCESS
)
1228 key
= os_memdup(data
->emsk
, EAP_EMSK_LEN
);
1232 *len
= EAP_EMSK_LEN
;
1238 int eap_peer_sim_register(void)
1240 struct eap_method
*eap
;
1242 eap
= eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION
,
1243 EAP_VENDOR_IETF
, EAP_TYPE_SIM
, "SIM");
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
;
1259 return eap_peer_method_register(eap
);