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