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