2
* EAP peer method: EAP-SIM (RFC 4186)
3
* Copyright (c) 2004-2006, Jouni Malinen <jkmaline@cc.hut.fi>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation.
9
* Alternatively, this software may be distributed under the terms of BSD
12
* See README and COPYING for more details.
19
#include "wpa_supplicant.h"
20
#include "config_ssid.h"
22
#include "pcsc_funcs.h"
23
#include "eap_sim_common.h"
30
size_t min_num_chal, num_chal;
32
u8 kc[3][EAP_SIM_KC_LEN];
33
u8 sres[3][EAP_SIM_SRES_LEN];
34
u8 nonce_mt[EAP_SIM_NONCE_MT_LEN], nonce_s[EAP_SIM_NONCE_S_LEN];
35
u8 mk[EAP_SIM_MK_LEN];
36
u8 k_aut[EAP_SIM_K_AUT_LEN];
37
u8 k_encr[EAP_SIM_K_ENCR_LEN];
38
u8 msk[EAP_SIM_KEYING_DATA_LEN];
39
u8 rand[3][GSM_RAND_LEN];
41
int num_id_req, num_notification;
47
unsigned int counter, counter_too_small;
48
u8 *last_eap_identity;
49
size_t last_eap_identity_len;
50
enum { CONTINUE, SUCCESS, FAILURE } state;
54
static void * eap_sim_init(struct eap_sm *sm)
56
struct eap_sim_data *data;
57
struct wpa_ssid *config = eap_get_config(sm);
59
data = wpa_zalloc(sizeof(*data));
63
if (hostapd_get_rand(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
64
wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
70
data->min_num_chal = 2;
71
if (config && config->phase1) {
72
char *pos = strstr(config->phase1, "sim_min_num_chal=");
74
data->min_num_chal = atoi(pos + 17);
75
if (data->min_num_chal < 2 || data->min_num_chal > 3) {
76
wpa_printf(MSG_WARNING, "EAP-SIM: Invalid "
77
"sim_min_num_chal configuration "
78
"(%lu, expected 2 or 3)",
79
(unsigned long) data->min_num_chal);
83
wpa_printf(MSG_DEBUG, "EAP-SIM: Set minimum number of "
85
(unsigned long) data->min_num_chal);
89
data->state = CONTINUE;
95
static void eap_sim_deinit(struct eap_sm *sm, void *priv)
97
struct eap_sim_data *data = priv;
100
free(data->pseudonym);
101
free(data->reauth_id);
102
free(data->last_eap_identity);
108
static int eap_sim_gsm_auth(struct eap_sm *sm, struct eap_sim_data *data)
110
wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication algorithm");
112
if (scard_gsm_auth(sm->scard_ctx, data->rand[0],
113
data->sres[0], data->kc[0]) ||
114
scard_gsm_auth(sm->scard_ctx, data->rand[1],
115
data->sres[1], data->kc[1]) ||
116
(data->num_chal > 2 &&
117
scard_gsm_auth(sm->scard_ctx, data->rand[2],
118
data->sres[2], data->kc[2]))) {
119
wpa_printf(MSG_DEBUG, "EAP-SIM: GSM SIM authentication could "
123
#else /* PCSC_FUNCS */
124
/* These hardcoded Kc and SRES values are used for testing. RAND to
125
* KC/SREC mapping is very bogus as far as real authentication is
126
* concerned, but it is quite useful for cases where the AS is rotating
127
* the order of pre-configured values. */
130
for (i = 0; i < data->num_chal; i++) {
131
if (data->rand[i][0] == 0xaa) {
133
"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7",
135
memcpy(data->sres[i], "\xd1\xd2\xd3\xd4",
137
} else if (data->rand[i][0] == 0xbb) {
139
"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7",
141
memcpy(data->sres[i], "\xe1\xe2\xe3\xe4",
145
"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
147
memcpy(data->sres[i], "\xf1\xf2\xf3\xf4",
152
#endif /* PCSC_FUNCS */
157
static int eap_sim_supported_ver(int version)
159
return version == EAP_SIM_VERSION;
163
#define CLEAR_PSEUDONYM 0x01
164
#define CLEAR_REAUTH_ID 0x02
165
#define CLEAR_EAP_ID 0x04
167
static void eap_sim_clear_identities(struct eap_sim_data *data, int id)
169
wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old%s%s%s",
170
id & CLEAR_PSEUDONYM ? " pseudonym" : "",
171
id & CLEAR_REAUTH_ID ? " reauth_id" : "",
172
id & CLEAR_EAP_ID ? " eap_id" : "");
173
if (id & CLEAR_PSEUDONYM) {
174
free(data->pseudonym);
175
data->pseudonym = NULL;
176
data->pseudonym_len = 0;
178
if (id & CLEAR_REAUTH_ID) {
179
free(data->reauth_id);
180
data->reauth_id = NULL;
181
data->reauth_id_len = 0;
183
if (id & CLEAR_EAP_ID) {
184
free(data->last_eap_identity);
185
data->last_eap_identity = NULL;
186
data->last_eap_identity_len = 0;
191
static int eap_sim_learn_ids(struct eap_sim_data *data,
192
struct eap_sim_attrs *attr)
194
if (attr->next_pseudonym) {
195
free(data->pseudonym);
196
data->pseudonym = malloc(attr->next_pseudonym_len);
197
if (data->pseudonym == NULL) {
198
wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
202
memcpy(data->pseudonym, attr->next_pseudonym,
203
attr->next_pseudonym_len);
204
data->pseudonym_len = attr->next_pseudonym_len;
205
wpa_hexdump_ascii(MSG_DEBUG,
206
"EAP-SIM: (encr) AT_NEXT_PSEUDONYM",
208
data->pseudonym_len);
211
if (attr->next_reauth_id) {
212
free(data->reauth_id);
213
data->reauth_id = malloc(attr->next_reauth_id_len);
214
if (data->reauth_id == NULL) {
215
wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
219
memcpy(data->reauth_id, attr->next_reauth_id,
220
attr->next_reauth_id_len);
221
data->reauth_id_len = attr->next_reauth_id_len;
222
wpa_hexdump_ascii(MSG_DEBUG,
223
"EAP-SIM: (encr) AT_NEXT_REAUTH_ID",
225
data->reauth_id_len);
232
static u8 * eap_sim_client_error(struct eap_sim_data *data,
233
const struct eap_hdr *req,
234
size_t *respDataLen, int err)
236
struct eap_sim_msg *msg;
238
data->state = FAILURE;
239
data->num_id_req = 0;
240
data->num_notification = 0;
242
msg = eap_sim_msg_init(EAP_CODE_RESPONSE, req->identifier,
243
EAP_TYPE_SIM, EAP_SIM_SUBTYPE_CLIENT_ERROR);
244
eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
245
return eap_sim_msg_finish(msg, respDataLen, NULL, NULL, 0);
249
static u8 * eap_sim_response_start(struct eap_sm *sm,
250
struct eap_sim_data *data,
251
const struct eap_hdr *req,
253
enum eap_sim_id_req id_req)
255
const u8 *identity = NULL;
256
size_t identity_len = 0;
257
struct eap_sim_msg *msg;
260
if (id_req == ANY_ID && data->reauth_id) {
261
identity = data->reauth_id;
262
identity_len = data->reauth_id_len;
264
} else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
266
identity = data->pseudonym;
267
identity_len = data->pseudonym_len;
268
eap_sim_clear_identities(data, CLEAR_REAUTH_ID);
269
} else if (id_req != NO_ID_REQ) {
270
identity = eap_get_config_identity(sm, &identity_len);
272
eap_sim_clear_identities(data, CLEAR_PSEUDONYM |
276
if (id_req != NO_ID_REQ)
277
eap_sim_clear_identities(data, CLEAR_EAP_ID);
279
wpa_printf(MSG_DEBUG, "Generating EAP-SIM Start (id=%d)",
281
msg = eap_sim_msg_init(EAP_CODE_RESPONSE, req->identifier,
282
EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START);
283
wpa_hexdump(MSG_DEBUG, " AT_NONCE_MT",
284
data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
285
eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_MT, 0,
286
data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
287
wpa_printf(MSG_DEBUG, " AT_SELECTED_VERSION %d",
288
data->selected_version);
289
eap_sim_msg_add(msg, EAP_SIM_AT_SELECTED_VERSION,
290
data->selected_version, NULL, 0);
293
wpa_hexdump_ascii(MSG_DEBUG, " AT_IDENTITY",
294
identity, identity_len);
295
eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
296
identity, identity_len);
299
return eap_sim_msg_finish(msg, respDataLen, NULL, NULL, 0);
303
static u8 * eap_sim_response_challenge(struct eap_sim_data *data,
304
const struct eap_hdr *req,
307
struct eap_sim_msg *msg;
309
wpa_printf(MSG_DEBUG, "Generating EAP-SIM Challenge (id=%d)",
311
msg = eap_sim_msg_init(EAP_CODE_RESPONSE, req->identifier,
312
EAP_TYPE_SIM, EAP_SIM_SUBTYPE_CHALLENGE);
313
wpa_printf(MSG_DEBUG, " AT_MAC");
314
eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
315
return eap_sim_msg_finish(msg, respDataLen, data->k_aut,
317
data->num_chal * EAP_SIM_SRES_LEN);
321
static u8 * eap_sim_response_reauth(struct eap_sim_data *data,
322
const struct eap_hdr *req,
323
size_t *respDataLen, int counter_too_small)
325
struct eap_sim_msg *msg;
326
unsigned int counter;
328
wpa_printf(MSG_DEBUG, "Generating EAP-SIM Reauthentication (id=%d)",
330
msg = eap_sim_msg_init(EAP_CODE_RESPONSE, req->identifier,
332
EAP_SIM_SUBTYPE_REAUTHENTICATION);
333
wpa_printf(MSG_DEBUG, " AT_IV");
334
wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
335
eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
337
if (counter_too_small) {
338
wpa_printf(MSG_DEBUG, " *AT_COUNTER_TOO_SMALL");
339
eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
340
counter = data->counter_too_small;
342
counter = data->counter;
344
wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter);
345
eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
347
if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
348
wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
350
eap_sim_msg_free(msg);
353
wpa_printf(MSG_DEBUG, " AT_MAC");
354
eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
355
return eap_sim_msg_finish(msg, respDataLen, data->k_aut, data->nonce_s,
356
EAP_SIM_NONCE_S_LEN);
360
static u8 * eap_sim_response_notification(struct eap_sim_data *data,
361
const struct eap_hdr *req,
365
struct eap_sim_msg *msg;
366
u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
368
wpa_printf(MSG_DEBUG, "Generating EAP-SIM Notification (id=%d)",
370
msg = eap_sim_msg_init(EAP_CODE_RESPONSE, req->identifier,
371
EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION);
372
wpa_printf(MSG_DEBUG, " AT_NOTIFICATION");
373
eap_sim_msg_add(msg, EAP_SIM_AT_NOTIFICATION, notification, NULL, 0);
374
if (k_aut && data->reauth) {
375
wpa_printf(MSG_DEBUG, " AT_IV");
376
wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
377
eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
378
EAP_SIM_AT_ENCR_DATA);
379
wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", data->counter);
380
eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
382
if (eap_sim_msg_add_encr_end(msg, data->k_encr,
383
EAP_SIM_AT_PADDING)) {
384
wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
386
eap_sim_msg_free(msg);
391
wpa_printf(MSG_DEBUG, " AT_MAC");
392
eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
394
return eap_sim_msg_finish(msg, respDataLen, k_aut, (u8 *) "", 0);
398
static u8 * eap_sim_process_start(struct eap_sm *sm, struct eap_sim_data *data,
399
const struct eap_hdr *req,
401
struct eap_sim_attrs *attr)
403
int selected_version = -1, id_error;
407
wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Start");
408
if (attr->version_list == NULL) {
409
wpa_printf(MSG_INFO, "EAP-SIM: No AT_VERSION_LIST in "
411
return eap_sim_client_error(data, req, respDataLen,
412
EAP_SIM_UNSUPPORTED_VERSION);
415
free(data->ver_list);
416
data->ver_list = malloc(attr->version_list_len);
417
if (data->ver_list == NULL) {
418
wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to allocate "
419
"memory for version list");
420
return eap_sim_client_error(data, req, respDataLen,
421
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
423
memcpy(data->ver_list, attr->version_list, attr->version_list_len);
424
data->ver_list_len = attr->version_list_len;
425
pos = data->ver_list;
426
for (i = 0; i < data->ver_list_len / 2; i++) {
427
int ver = pos[0] * 256 + pos[1];
429
if (eap_sim_supported_ver(ver)) {
430
selected_version = ver;
434
if (selected_version < 0) {
435
wpa_printf(MSG_INFO, "EAP-SIM: Could not find a supported "
437
return eap_sim_client_error(data, req, respDataLen,
438
EAP_SIM_UNSUPPORTED_VERSION);
440
wpa_printf(MSG_DEBUG, "EAP-SIM: Selected Version %d",
442
data->selected_version = selected_version;
445
switch (attr->id_req) {
449
if (data->num_id_req > 0)
454
if (data->num_id_req > 1)
459
if (data->num_id_req > 2)
465
wpa_printf(MSG_INFO, "EAP-SIM: Too many ID requests "
466
"used within one authentication");
467
return eap_sim_client_error(data, req, respDataLen,
468
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
471
return eap_sim_response_start(sm, data, req, respDataLen,
476
static u8 * eap_sim_process_challenge(struct eap_sm *sm,
477
struct eap_sim_data *data,
478
const struct eap_hdr *req,
481
struct eap_sim_attrs *attr)
485
struct eap_sim_attrs eattr;
487
wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Challenge");
489
if (!attr->mac || !attr->rand) {
490
wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
491
"did not include%s%s",
492
!attr->mac ? " AT_MAC" : "",
493
!attr->rand ? " AT_RAND" : "");
494
return eap_sim_client_error(data, req, respDataLen,
495
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
498
wpa_printf(MSG_DEBUG, "EAP-SIM: %lu challenges",
499
(unsigned long) attr->num_chal);
500
if (attr->num_chal < data->min_num_chal) {
501
wpa_printf(MSG_INFO, "EAP-SIM: Insufficient number of "
502
"challenges (%lu)", (unsigned long) attr->num_chal);
503
return eap_sim_client_error(data, req, respDataLen,
504
EAP_SIM_INSUFFICIENT_NUM_OF_CHAL);
506
if (attr->num_chal > 3) {
507
wpa_printf(MSG_INFO, "EAP-SIM: Too many challenges "
508
"(%lu)", (unsigned long) attr->num_chal);
509
return eap_sim_client_error(data, req, respDataLen,
510
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
513
/* Verify that RANDs are different */
514
if (memcmp(attr->rand, attr->rand + GSM_RAND_LEN,
515
GSM_RAND_LEN) == 0 ||
516
(attr->num_chal > 2 &&
517
(memcmp(attr->rand, attr->rand + 2 * GSM_RAND_LEN,
518
GSM_RAND_LEN) == 0 ||
519
memcmp(attr->rand + GSM_RAND_LEN,
520
attr->rand + 2 * GSM_RAND_LEN,
521
GSM_RAND_LEN) == 0))) {
522
wpa_printf(MSG_INFO, "EAP-SIM: Same RAND used multiple times");
523
return eap_sim_client_error(data, req, respDataLen,
524
EAP_SIM_RAND_NOT_FRESH);
527
memcpy(data->rand, attr->rand, attr->num_chal * GSM_RAND_LEN);
528
data->num_chal = attr->num_chal;
530
if (eap_sim_gsm_auth(sm, data)) {
531
wpa_printf(MSG_WARNING, "EAP-SIM: GSM authentication failed");
532
return eap_sim_client_error(data, req, respDataLen,
533
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
535
if (data->last_eap_identity) {
536
identity = data->last_eap_identity;
537
identity_len = data->last_eap_identity_len;
538
} else if (data->pseudonym) {
539
identity = data->pseudonym;
540
identity_len = data->pseudonym_len;
542
identity = eap_get_config_identity(sm, &identity_len);
543
wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Selected identity for MK "
544
"derivation", identity, identity_len);
545
eap_sim_derive_mk(identity, identity_len, data->nonce_mt,
546
data->selected_version, data->ver_list,
547
data->ver_list_len, data->num_chal,
548
(const u8 *) data->kc, data->mk);
549
eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk);
550
if (eap_sim_verify_mac(data->k_aut, (const u8 *) req, reqDataLen,
551
attr->mac, data->nonce_mt,
552
EAP_SIM_NONCE_MT_LEN)) {
553
wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
554
"used invalid AT_MAC");
555
return eap_sim_client_error(data, req, respDataLen,
556
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
559
/* Old reauthentication and pseudonym identities must not be used
560
* anymore. In other words, if no new identities are received, full
561
* authentication will be used on next reauthentication. */
562
eap_sim_clear_identities(data, CLEAR_PSEUDONYM | CLEAR_REAUTH_ID |
565
if (attr->encr_data) {
567
decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
568
attr->encr_data_len, attr->iv,
570
if (decrypted == NULL) {
571
return eap_sim_client_error(
572
data, req, respDataLen,
573
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
575
eap_sim_learn_ids(data, &eattr);
579
if (data->state != FAILURE)
580
data->state = SUCCESS;
582
data->num_id_req = 0;
583
data->num_notification = 0;
584
/* RFC 4186 specifies that counter is initialized to one after
585
* fullauth, but initializing it to zero makes it easier to implement
586
* reauth verification. */
588
return eap_sim_response_challenge(data, req, respDataLen);
592
static int eap_sim_process_notification_reauth(struct eap_sim_data *data,
593
struct eap_sim_attrs *attr)
595
struct eap_sim_attrs eattr;
598
if (attr->encr_data == NULL || attr->iv == NULL) {
599
wpa_printf(MSG_WARNING, "EAP-SIM: Notification message after "
600
"reauth did not include encrypted data");
604
decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
605
attr->encr_data_len, attr->iv, &eattr,
607
if (decrypted == NULL) {
608
wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
609
"data from notification message");
613
if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
614
wpa_printf(MSG_WARNING, "EAP-SIM: Counter in notification "
615
"message does not match with counter in reauth "
626
static int eap_sim_process_notification_auth(struct eap_sim_data *data,
627
const struct eap_hdr *req,
629
struct eap_sim_attrs *attr)
631
if (attr->mac == NULL) {
632
wpa_printf(MSG_INFO, "EAP-SIM: no AT_MAC in after_auth "
633
"Notification message");
637
if (eap_sim_verify_mac(data->k_aut, (u8 *) req, reqDataLen, attr->mac,
639
wpa_printf(MSG_WARNING, "EAP-SIM: Notification message "
640
"used invalid AT_MAC");
645
eap_sim_process_notification_reauth(data, attr)) {
646
wpa_printf(MSG_WARNING, "EAP-SIM: Invalid notification "
647
"message after reauth");
655
static u8 * eap_sim_process_notification(struct eap_sm *sm,
656
struct eap_sim_data *data,
657
const struct eap_hdr *req,
660
struct eap_sim_attrs *attr)
662
wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Notification");
663
if (data->num_notification > 0) {
664
wpa_printf(MSG_INFO, "EAP-SIM: too many notification "
665
"rounds (only one allowed)");
666
return eap_sim_client_error(data, req, respDataLen,
667
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
669
data->num_notification++;
670
if (attr->notification == -1) {
671
wpa_printf(MSG_INFO, "EAP-SIM: no AT_NOTIFICATION in "
672
"Notification message");
673
return eap_sim_client_error(data, req, respDataLen,
674
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
677
if ((attr->notification & 0x4000) == 0 &&
678
eap_sim_process_notification_auth(data, req, reqDataLen, attr)) {
679
return eap_sim_client_error(data, req, respDataLen,
680
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
683
eap_sim_report_notification(sm->msg_ctx, attr->notification, 0);
684
if (attr->notification >= 0 && attr->notification < 32768) {
685
data->state = FAILURE;
687
return eap_sim_response_notification(data, req, respDataLen,
692
static u8 * eap_sim_process_reauthentication(struct eap_sm *sm,
693
struct eap_sim_data *data,
694
const struct eap_hdr *req,
697
struct eap_sim_attrs *attr)
699
struct eap_sim_attrs eattr;
702
wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Reauthentication");
704
if (data->reauth_id == NULL) {
705
wpa_printf(MSG_WARNING, "EAP-SIM: Server is trying "
706
"reauthentication, but no reauth_id available");
707
return eap_sim_client_error(data, req, respDataLen,
708
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
712
if (eap_sim_verify_mac(data->k_aut, (const u8 *) req, reqDataLen,
713
attr->mac, (u8 *) "", 0)) {
714
wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
715
"did not have valid AT_MAC");
716
return eap_sim_client_error(data, req, respDataLen,
717
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
720
if (attr->encr_data == NULL || attr->iv == NULL) {
721
wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
722
"message did not include encrypted data");
723
return eap_sim_client_error(data, req, respDataLen,
724
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
727
decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
728
attr->encr_data_len, attr->iv, &eattr,
730
if (decrypted == NULL) {
731
wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
732
"data from reauthentication message");
733
return eap_sim_client_error(data, req, respDataLen,
734
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
737
if (eattr.nonce_s == NULL || eattr.counter < 0) {
738
wpa_printf(MSG_INFO, "EAP-SIM: (encr) No%s%s in reauth packet",
739
!eattr.nonce_s ? " AT_NONCE_S" : "",
740
eattr.counter < 0 ? " AT_COUNTER" : "");
742
return eap_sim_client_error(data, req, respDataLen,
743
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
746
if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
747
wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid counter "
748
"(%d <= %d)", eattr.counter, data->counter);
749
data->counter_too_small = eattr.counter;
750
/* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
751
* reauth_id must not be used to start a new reauthentication.
752
* However, since it was used in the last EAP-Response-Identity
753
* packet, it has to saved for the following fullauth to be
754
* used in MK derivation. */
755
free(data->last_eap_identity);
756
data->last_eap_identity = data->reauth_id;
757
data->last_eap_identity_len = data->reauth_id_len;
758
data->reauth_id = NULL;
759
data->reauth_id_len = 0;
761
return eap_sim_response_reauth(data, req, respDataLen, 1);
763
data->counter = eattr.counter;
765
memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
766
wpa_hexdump(MSG_DEBUG, "EAP-SIM: (encr) AT_NONCE_S",
767
data->nonce_s, EAP_SIM_NONCE_S_LEN);
769
eap_sim_derive_keys_reauth(data->counter,
770
data->reauth_id, data->reauth_id_len,
771
data->nonce_s, data->mk, data->msk);
772
eap_sim_clear_identities(data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
773
eap_sim_learn_ids(data, &eattr);
775
if (data->state != FAILURE)
776
data->state = SUCCESS;
778
data->num_id_req = 0;
779
data->num_notification = 0;
780
if (data->counter > EAP_SIM_MAX_FAST_REAUTHS) {
781
wpa_printf(MSG_DEBUG, "EAP-SIM: Maximum number of "
782
"fast reauths performed - force fullauth");
783
eap_sim_clear_identities(data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
786
return eap_sim_response_reauth(data, req, respDataLen, 0);
790
static u8 * eap_sim_process(struct eap_sm *sm, void *priv,
791
struct eap_method_ret *ret,
792
const u8 *reqData, size_t reqDataLen,
795
struct eap_sim_data *data = priv;
796
const struct eap_hdr *req;
799
struct eap_sim_attrs attr;
802
wpa_hexdump(MSG_DEBUG, "EAP-SIM: EAP data", reqData, reqDataLen);
803
if (eap_get_config_identity(sm, &len) == NULL) {
804
wpa_printf(MSG_INFO, "EAP-SIM: Identity not configured");
805
eap_sm_request_identity(sm);
810
pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM,
811
reqData, reqDataLen, &len);
812
if (pos == NULL || len < 1) {
816
req = (const struct eap_hdr *) reqData;
817
len = be_to_host16(req->length);
820
ret->methodState = METHOD_MAY_CONT;
821
ret->decision = DECISION_FAIL;
822
ret->allowNotifications = TRUE;
825
wpa_printf(MSG_DEBUG, "EAP-SIM: Subtype=%d", subtype);
826
pos += 2; /* Reserved */
828
if (eap_sim_parse_attr(pos, reqData + len, &attr, 0, 0)) {
829
res = eap_sim_client_error(data, req, respDataLen,
830
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
835
case EAP_SIM_SUBTYPE_START:
836
res = eap_sim_process_start(sm, data, req,
839
case EAP_SIM_SUBTYPE_CHALLENGE:
840
res = eap_sim_process_challenge(sm, data, req, len,
843
case EAP_SIM_SUBTYPE_NOTIFICATION:
844
res = eap_sim_process_notification(sm, data, req, len,
847
case EAP_SIM_SUBTYPE_REAUTHENTICATION:
848
res = eap_sim_process_reauthentication(sm, data, req, len,
851
case EAP_SIM_SUBTYPE_CLIENT_ERROR:
852
wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Client-Error");
853
res = eap_sim_client_error(data, req, respDataLen,
854
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
857
wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown subtype=%d", subtype);
858
res = eap_sim_client_error(data, req, respDataLen,
859
EAP_SIM_UNABLE_TO_PROCESS_PACKET);
864
if (data->state == FAILURE) {
865
ret->decision = DECISION_FAIL;
866
ret->methodState = METHOD_DONE;
867
} else if (data->state == SUCCESS) {
868
ret->decision = DECISION_COND_SUCC;
869
ret->methodState = METHOD_DONE;
872
if (ret->methodState == METHOD_DONE) {
873
ret->allowNotifications = FALSE;
880
static Boolean eap_sim_has_reauth_data(struct eap_sm *sm, void *priv)
882
struct eap_sim_data *data = priv;
883
return data->pseudonym || data->reauth_id;
887
static void eap_sim_deinit_for_reauth(struct eap_sm *sm, void *priv)
889
struct eap_sim_data *data = priv;
890
eap_sim_clear_identities(data, CLEAR_EAP_ID);
894
static void * eap_sim_init_for_reauth(struct eap_sm *sm, void *priv)
896
struct eap_sim_data *data = priv;
897
if (hostapd_get_rand(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
898
wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
903
data->num_id_req = 0;
904
data->num_notification = 0;
905
data->state = CONTINUE;
910
static const u8 * eap_sim_get_identity(struct eap_sm *sm, void *priv,
913
struct eap_sim_data *data = priv;
915
if (data->reauth_id) {
916
*len = data->reauth_id_len;
917
return data->reauth_id;
920
if (data->pseudonym) {
921
*len = data->pseudonym_len;
922
return data->pseudonym;
929
static Boolean eap_sim_isKeyAvailable(struct eap_sm *sm, void *priv)
931
struct eap_sim_data *data = priv;
932
return data->state == SUCCESS;
936
static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
938
struct eap_sim_data *data = priv;
941
if (data->state != SUCCESS)
944
key = malloc(EAP_SIM_KEYING_DATA_LEN);
948
*len = EAP_SIM_KEYING_DATA_LEN;
949
memcpy(key, data->msk, EAP_SIM_KEYING_DATA_LEN);
955
int eap_peer_sim_register(void)
957
struct eap_method *eap;
960
eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
961
EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
965
eap->init = eap_sim_init;
966
eap->deinit = eap_sim_deinit;
967
eap->process = eap_sim_process;
968
eap->isKeyAvailable = eap_sim_isKeyAvailable;
969
eap->getKey = eap_sim_getKey;
970
eap->has_reauth_data = eap_sim_has_reauth_data;
971
eap->deinit_for_reauth = eap_sim_deinit_for_reauth;
972
eap->init_for_reauth = eap_sim_init_for_reauth;
973
eap->get_identity = eap_sim_get_identity;
975
ret = eap_peer_method_register(eap);
977
eap_peer_method_free(eap);