Remove tcpslice(1).
[dragonfly.git] / contrib / hostapd-0.5.8 / eap_aka.c
1 /*
2  * hostapd / EAP-AKA (RFC 4187)
3  * Copyright (c) 2005-2007, 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 "hostapd.h"
18 #include "common.h"
19 #include "crypto.h"
20 #include "eap_i.h"
21 #include "eap_sim_common.h"
22 #include "eap_sim_db.h"
23
24
25 struct eap_aka_data {
26         u8 mk[EAP_SIM_MK_LEN];
27         u8 nonce_s[EAP_SIM_NONCE_S_LEN];
28         u8 k_aut[EAP_SIM_K_AUT_LEN];
29         u8 k_encr[EAP_SIM_K_ENCR_LEN];
30         u8 msk[EAP_SIM_KEYING_DATA_LEN];
31         u8 emsk[EAP_EMSK_LEN];
32         u8 rand[EAP_AKA_RAND_LEN];
33         u8 autn[EAP_AKA_AUTN_LEN];
34         u8 ck[EAP_AKA_CK_LEN];
35         u8 ik[EAP_AKA_IK_LEN];
36         u8 res[EAP_AKA_RES_MAX_LEN];
37         size_t res_len;
38         enum {
39                 IDENTITY, CHALLENGE, REAUTH, NOTIFICATION, SUCCESS, FAILURE
40         } state;
41         char *next_pseudonym;
42         char *next_reauth_id;
43         u16 counter;
44         struct eap_sim_reauth *reauth;
45         int auts_reported; /* whether the current AUTS has been reported to the
46                             * eap_sim_db */
47         u16 notification;
48 };
49
50
51 static void eap_aka_determine_identity(struct eap_sm *sm,
52                                        struct eap_aka_data *data,
53                                        int before_identity, int after_reauth);
54
55
56 static const char * eap_aka_state_txt(int state)
57 {
58         switch (state) {
59         case IDENTITY:
60                 return "IDENTITY";
61         case CHALLENGE:
62                 return "CHALLENGE";
63         case REAUTH:
64                 return "REAUTH";
65         case SUCCESS:
66                 return "SUCCESS";
67         case FAILURE:
68                 return "FAILURE";
69         case NOTIFICATION:
70                 return "NOTIFICATION";
71         default:
72                 return "Unknown?!";
73         }
74 }
75
76
77 static void eap_aka_state(struct eap_aka_data *data, int state)
78 {
79         wpa_printf(MSG_DEBUG, "EAP-AKA: %s -> %s",
80                    eap_aka_state_txt(data->state),
81                    eap_aka_state_txt(state));
82         data->state = state;
83 }
84
85
86 static void * eap_aka_init(struct eap_sm *sm)
87 {
88         struct eap_aka_data *data;
89
90         if (sm->eap_sim_db_priv == NULL) {
91                 wpa_printf(MSG_WARNING, "EAP-AKA: eap_sim_db not configured");
92                 return NULL;
93         }
94
95         data = wpa_zalloc(sizeof(*data));
96         if (data == NULL)
97                 return NULL;
98         data->state = IDENTITY;
99         eap_aka_determine_identity(sm, data, 1, 0);
100
101         return data;
102 }
103
104
105 static void eap_aka_reset(struct eap_sm *sm, void *priv)
106 {
107         struct eap_aka_data *data = priv;
108         free(data->next_pseudonym);
109         free(data->next_reauth_id);
110         free(data);
111 }
112
113
114 static u8 * eap_aka_build_identity(struct eap_sm *sm,
115                                    struct eap_aka_data *data,
116                                    int id, size_t *reqDataLen)
117 {
118         struct eap_sim_msg *msg;
119
120         wpa_printf(MSG_DEBUG, "EAP-AKA: Generating Identity");
121         msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_AKA,
122                                EAP_AKA_SUBTYPE_IDENTITY);
123         if (eap_sim_db_identity_known(sm->eap_sim_db_priv, sm->identity,
124                                       sm->identity_len)) {
125                 wpa_printf(MSG_DEBUG, "   AT_PERMANENT_ID_REQ");
126                 eap_sim_msg_add(msg, EAP_SIM_AT_PERMANENT_ID_REQ, 0, NULL, 0);
127         }
128         return eap_sim_msg_finish(msg, reqDataLen, NULL, NULL, 0);
129 }
130
131
132 static int eap_aka_build_encr(struct eap_sm *sm, struct eap_aka_data *data,
133                               struct eap_sim_msg *msg, u16 counter,
134                               const u8 *nonce_s)
135 {
136         free(data->next_pseudonym);
137         data->next_pseudonym =
138                 eap_sim_db_get_next_pseudonym(sm->eap_sim_db_priv, 1);
139         free(data->next_reauth_id);
140         if (data->counter <= EAP_AKA_MAX_FAST_REAUTHS) {
141                 data->next_reauth_id =
142                         eap_sim_db_get_next_reauth_id(sm->eap_sim_db_priv, 1);
143         } else {
144                 wpa_printf(MSG_DEBUG, "EAP-AKA: Max fast re-authentication "
145                            "count exceeded - force full authentication");
146                 data->next_reauth_id = NULL;
147         }
148
149         if (data->next_pseudonym == NULL && data->next_reauth_id == NULL &&
150             counter == 0 && nonce_s == NULL)
151                 return 0;
152
153         wpa_printf(MSG_DEBUG, "   AT_IV");
154         wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
155         eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
156
157         if (counter > 0) {
158                 wpa_printf(MSG_DEBUG, "   *AT_COUNTER (%u)", counter);
159                 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
160         }
161
162         if (nonce_s) {
163                 wpa_printf(MSG_DEBUG, "   *AT_NONCE_S");
164                 eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_S, 0, nonce_s,
165                                 EAP_SIM_NONCE_S_LEN);
166         }
167
168         if (data->next_pseudonym) {
169                 wpa_printf(MSG_DEBUG, "   *AT_NEXT_PSEUDONYM (%s)",
170                            data->next_pseudonym);
171                 eap_sim_msg_add(msg, EAP_SIM_AT_NEXT_PSEUDONYM,
172                                 strlen(data->next_pseudonym),
173                                 (u8 *) data->next_pseudonym,
174                                 strlen(data->next_pseudonym));
175         }
176
177         if (data->next_reauth_id) {
178                 wpa_printf(MSG_DEBUG, "   *AT_NEXT_REAUTH_ID (%s)",
179                            data->next_reauth_id);
180                 eap_sim_msg_add(msg, EAP_SIM_AT_NEXT_REAUTH_ID,
181                                 strlen(data->next_reauth_id),
182                                 (u8 *) data->next_reauth_id,
183                                 strlen(data->next_reauth_id));
184         }
185
186         if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
187                 wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt "
188                            "AT_ENCR_DATA");
189                 return -1;
190         }
191
192         return 0;
193 }
194
195
196 static u8 * eap_aka_build_challenge(struct eap_sm *sm,
197                                     struct eap_aka_data *data,
198                                     int id, size_t *reqDataLen)
199 {
200         struct eap_sim_msg *msg;
201
202         wpa_printf(MSG_DEBUG, "EAP-AKA: Generating Challenge");
203         msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_AKA,
204                                EAP_AKA_SUBTYPE_CHALLENGE);
205         wpa_printf(MSG_DEBUG, "   AT_RAND");
206         eap_sim_msg_add(msg, EAP_SIM_AT_RAND, 0, data->rand, EAP_AKA_RAND_LEN);
207         eap_sim_msg_add(msg, EAP_SIM_AT_AUTN, 0, data->autn, EAP_AKA_AUTN_LEN);
208
209         if (eap_aka_build_encr(sm, data, msg, 0, NULL)) {
210                 eap_sim_msg_free(msg);
211                 return NULL;
212         }
213
214         wpa_printf(MSG_DEBUG, "   AT_MAC");
215         eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
216         return eap_sim_msg_finish(msg, reqDataLen, data->k_aut, NULL, 0);
217 }
218
219
220 static u8 * eap_aka_build_reauth(struct eap_sm *sm,
221                                  struct eap_aka_data *data,
222                                  int id, size_t *reqDataLen)
223 {
224         struct eap_sim_msg *msg;
225
226         wpa_printf(MSG_DEBUG, "EAP-AKA: Generating Re-authentication");
227
228         if (hostapd_get_rand(data->nonce_s, EAP_SIM_NONCE_S_LEN))
229                 return NULL;
230         wpa_hexdump_key(MSG_MSGDUMP, "EAP-AKA: NONCE_S",
231                         data->nonce_s, EAP_SIM_NONCE_S_LEN);
232
233         eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
234                             data->emsk);
235         eap_sim_derive_keys_reauth(data->counter, sm->identity,
236                                    sm->identity_len, data->nonce_s, data->mk,
237                                    data->msk, data->emsk);
238
239         msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_AKA,
240                                EAP_AKA_SUBTYPE_REAUTHENTICATION);
241
242         if (eap_aka_build_encr(sm, data, msg, data->counter, data->nonce_s)) {
243                 eap_sim_msg_free(msg);
244                 return NULL;
245         }
246
247         wpa_printf(MSG_DEBUG, "   AT_MAC");
248         eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
249         return eap_sim_msg_finish(msg, reqDataLen, data->k_aut, NULL, 0);
250 }
251
252
253 static u8 * eap_aka_build_notification(struct eap_sm *sm,
254                                        struct eap_aka_data *data,
255                                        int id, size_t *reqDataLen)
256 {
257         struct eap_sim_msg *msg;
258
259         wpa_printf(MSG_DEBUG, "EAP-AKA: Generating Notification");
260         msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_AKA,
261                                EAP_AKA_SUBTYPE_NOTIFICATION);
262         wpa_printf(MSG_DEBUG, "   AT_NOTIFICATION");
263         eap_sim_msg_add(msg, EAP_SIM_AT_NOTIFICATION, data->notification,
264                         NULL, 0);
265         return eap_sim_msg_finish(msg, reqDataLen, NULL, NULL, 0);
266 }
267
268
269 static u8 * eap_aka_buildReq(struct eap_sm *sm, void *priv, int id,
270                              size_t *reqDataLen)
271 {
272         struct eap_aka_data *data = priv;
273
274         data->auts_reported = 0;
275         switch (data->state) {
276         case IDENTITY:
277                 return eap_aka_build_identity(sm, data, id, reqDataLen);
278         case CHALLENGE:
279                 return eap_aka_build_challenge(sm, data, id, reqDataLen);
280         case REAUTH:
281                 return eap_aka_build_reauth(sm, data, id, reqDataLen);
282         case NOTIFICATION:
283                 return eap_aka_build_notification(sm, data, id, reqDataLen);
284         default:
285                 wpa_printf(MSG_DEBUG, "EAP-AKA: Unknown state %d in "
286                            "buildReq", data->state);
287                 break;
288         }
289         return NULL;
290 }
291
292
293 static Boolean eap_aka_check(struct eap_sm *sm, void *priv,
294                              u8 *respData, size_t respDataLen)
295 {
296         struct eap_hdr *resp;
297         u8 *pos;
298
299         resp = (struct eap_hdr *) respData;
300         pos = (u8 *) (resp + 1);
301         if (respDataLen < sizeof(*resp) + 4 || *pos != EAP_TYPE_AKA ||
302             (ntohs(resp->length)) > respDataLen) {
303                 wpa_printf(MSG_INFO, "EAP-AKA: Invalid frame");
304                 return TRUE;
305         }
306
307         return FALSE;
308 }
309
310
311 static Boolean eap_aka_subtype_ok(struct eap_aka_data *data, u8 subtype)
312 {
313         if (subtype == EAP_AKA_SUBTYPE_CLIENT_ERROR ||
314             subtype == EAP_AKA_SUBTYPE_AUTHENTICATION_REJECT)
315                 return FALSE;
316
317         switch (data->state) {
318         case IDENTITY:
319                 if (subtype != EAP_AKA_SUBTYPE_IDENTITY) {
320                         wpa_printf(MSG_INFO, "EAP-AKA: Unexpected response "
321                                    "subtype %d", subtype);
322                         return TRUE;
323                 }
324                 break;
325         case CHALLENGE:
326                 if (subtype != EAP_AKA_SUBTYPE_CHALLENGE &&
327                     subtype != EAP_AKA_SUBTYPE_SYNCHRONIZATION_FAILURE) {
328                         wpa_printf(MSG_INFO, "EAP-AKA: Unexpected response "
329                                    "subtype %d", subtype);
330                         return TRUE;
331                 }
332                 break;
333         case REAUTH:
334                 if (subtype != EAP_AKA_SUBTYPE_REAUTHENTICATION) {
335                         wpa_printf(MSG_INFO, "EAP-AKA: Unexpected response "
336                                    "subtype %d", subtype);
337                         return TRUE;
338                 }
339                 break;
340         case NOTIFICATION:
341                 if (subtype != EAP_AKA_SUBTYPE_NOTIFICATION) {
342                         wpa_printf(MSG_INFO, "EAP-AKA: Unexpected response "
343                                    "subtype %d", subtype);
344                         return TRUE;
345                 }
346                 break;
347         default:
348                 wpa_printf(MSG_INFO, "EAP-AKA: Unexpected state (%d) for "
349                            "processing a response", data->state);
350                 return TRUE;
351         }
352
353         return FALSE;
354 }
355
356
357 static void eap_aka_determine_identity(struct eap_sm *sm,
358                                        struct eap_aka_data *data,
359                                        int before_identity, int after_reauth)
360 {
361         const u8 *identity;
362         size_t identity_len;
363         int res;
364
365         identity = NULL;
366         identity_len = 0;
367
368         if (after_reauth && data->reauth) {
369                 identity = data->reauth->identity;
370                 identity_len = data->reauth->identity_len;
371         } else if (sm->identity && sm->identity_len > 0 &&
372                    sm->identity[0] == EAP_AKA_PERMANENT_PREFIX) {
373                 identity = sm->identity;
374                 identity_len = sm->identity_len;
375         } else {
376                 identity = eap_sim_db_get_permanent(sm->eap_sim_db_priv,
377                                                     sm->identity,
378                                                     sm->identity_len,
379                                                     &identity_len);
380                 if (identity == NULL) {
381                         data->reauth = eap_sim_db_get_reauth_entry(
382                                 sm->eap_sim_db_priv, sm->identity,
383                                 sm->identity_len);
384                         if (data->reauth) {
385                                 wpa_printf(MSG_DEBUG, "EAP-AKA: Using fast "
386                                            "re-authentication");
387                                 identity = data->reauth->identity;
388                                 identity_len = data->reauth->identity_len;
389                                 data->counter = data->reauth->counter;
390                                 memcpy(data->mk, data->reauth->mk,
391                                        EAP_SIM_MK_LEN);
392                         }
393                 }
394         }
395
396         if (identity == NULL ||
397             eap_sim_db_identity_known(sm->eap_sim_db_priv, sm->identity,
398                                       sm->identity_len) < 0) {
399                 if (before_identity) {
400                         wpa_printf(MSG_DEBUG, "EAP-AKA: Permanent user name "
401                                    "not known - send AKA-Identity request");
402                         eap_aka_state(data, IDENTITY);
403                         return;
404                 } else {
405                         wpa_printf(MSG_DEBUG, "EAP-AKA: Unknown whether the "
406                                    "permanent user name is known; try to use "
407                                    "it");
408                         /* eap_sim_db_get_aka_auth() will report failure, if
409                          * this identity is not known. */
410                 }
411         }
412
413         wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA: Identity",
414                           identity, identity_len);
415
416         if (!after_reauth && data->reauth) {
417                 eap_aka_state(data, REAUTH);
418                 return;
419         }
420
421         res = eap_sim_db_get_aka_auth(sm->eap_sim_db_priv, identity,
422                                       identity_len, data->rand, data->autn,
423                                       data->ik, data->ck, data->res,
424                                       &data->res_len, sm);
425         if (res == EAP_SIM_DB_PENDING) {
426                 wpa_printf(MSG_DEBUG, "EAP-AKA: AKA authentication data "
427                            "not yet available - pending request");
428                 sm->method_pending = METHOD_PENDING_WAIT;
429                 return;
430         }
431
432         data->reauth = NULL;
433         data->counter = 0; /* reset re-auth counter since this is full auth */
434
435         if (res != 0) {
436                 wpa_printf(MSG_INFO, "EAP-AKA: Failed to get AKA "
437                            "authentication data for the peer");
438                 data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
439                 eap_aka_state(data, NOTIFICATION);
440                 return;
441         }
442         if (sm->method_pending == METHOD_PENDING_WAIT) {
443                 wpa_printf(MSG_DEBUG, "EAP-AKA: AKA authentication data "
444                            "available - abort pending wait");
445                 sm->method_pending = METHOD_PENDING_NONE;
446         }
447
448         wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA: Identity for MK derivation",
449                           sm->identity, sm->identity_len);
450
451         eap_aka_derive_mk(sm->identity, sm->identity_len, data->ik, data->ck,
452                           data->mk);
453         eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
454                             data->emsk);
455
456         eap_aka_state(data, CHALLENGE);
457 }
458
459
460 static void eap_aka_process_identity(struct eap_sm *sm,
461                                      struct eap_aka_data *data,
462                                      u8 *respData, size_t respDataLen,
463                                      struct eap_sim_attrs *attr)
464 {
465         wpa_printf(MSG_DEBUG, "EAP-AKA: Processing Identity");
466
467         if (attr->mac || attr->iv || attr->encr_data) {
468                 wpa_printf(MSG_WARNING, "EAP-AKA: Unexpected attribute "
469                            "received in EAP-Response/AKA-Identity");
470                 data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
471                 eap_aka_state(data, NOTIFICATION);
472                 return;
473         }
474
475         if (attr->identity) {
476                 free(sm->identity);
477                 sm->identity = malloc(attr->identity_len);
478                 if (sm->identity) {
479                         memcpy(sm->identity, attr->identity,
480                                attr->identity_len);
481                         sm->identity_len = attr->identity_len;
482                 }
483         }
484
485         eap_aka_determine_identity(sm, data, 0, 0);
486 }
487
488
489 static void eap_aka_process_challenge(struct eap_sm *sm,
490                                       struct eap_aka_data *data,
491                                       u8 *respData, size_t respDataLen,
492                                       struct eap_sim_attrs *attr)
493 {
494         const u8 *identity;
495         size_t identity_len;
496
497         wpa_printf(MSG_DEBUG, "EAP-AKA: Processing Challenge");
498
499         if (attr->mac == NULL ||
500             eap_sim_verify_mac(data->k_aut, respData, respDataLen, attr->mac,
501                                NULL, 0)) {
502                 wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message "
503                            "did not include valid AT_MAC");
504                 data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
505                 eap_aka_state(data, NOTIFICATION);
506                 return;
507         }
508
509         if (attr->res == NULL || attr->res_len != data->res_len ||
510             memcmp(attr->res, data->res, data->res_len) != 0) {
511                 wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message did not "
512                            "include valid AT_RES");
513                 data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
514                 eap_aka_state(data, NOTIFICATION);
515                 return;
516         }
517
518         wpa_printf(MSG_DEBUG, "EAP-AKA: Challenge response includes the "
519                    "correct AT_MAC");
520         eap_aka_state(data, SUCCESS);
521
522         identity = eap_sim_db_get_permanent(sm->eap_sim_db_priv, sm->identity,
523                                             sm->identity_len, &identity_len);
524         if (identity == NULL) {
525                 identity = sm->identity;
526                 identity_len = sm->identity_len;
527         }
528
529         if (data->next_pseudonym) {
530                 eap_sim_db_add_pseudonym(sm->eap_sim_db_priv, identity,
531                                          identity_len,
532                                          data->next_pseudonym);
533                 data->next_pseudonym = NULL;
534         }
535         if (data->next_reauth_id) {
536                 eap_sim_db_add_reauth(sm->eap_sim_db_priv, identity,
537                                       identity_len,
538                                       data->next_reauth_id, data->counter + 1,
539                                       data->mk);
540                 data->next_reauth_id = NULL;
541         }
542 }
543
544
545 static void eap_aka_process_sync_failure(struct eap_sm *sm,
546                                          struct eap_aka_data *data,
547                                          u8 *respData, size_t respDataLen,
548                                          struct eap_sim_attrs *attr)
549 {
550         wpa_printf(MSG_DEBUG, "EAP-AKA: Processing Synchronization-Failure");
551
552         if (attr->auts == NULL) {
553                 wpa_printf(MSG_WARNING, "EAP-AKA: Synchronization-Failure "
554                            "message did not include valid AT_AUTS");
555                 data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
556                 eap_aka_state(data, NOTIFICATION);
557                 return;
558         }
559
560         /* Avoid re-reporting AUTS when processing pending EAP packet by
561          * maintaining a local flag stating whether this AUTS has already been
562          * reported. */
563         if (!data->auts_reported &&
564             eap_sim_db_resynchronize(sm->eap_sim_db_priv, sm->identity,
565                                      sm->identity_len, attr->auts,
566                                      data->rand)) {
567                 wpa_printf(MSG_WARNING, "EAP-AKA: Resynchronization failed");
568                 data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
569                 eap_aka_state(data, NOTIFICATION);
570                 return;
571         }
572         data->auts_reported = 1;
573
574         /* Try again after resynchronization */
575         eap_aka_determine_identity(sm, data, 0, 0);
576 }
577
578
579 static void eap_aka_process_reauth(struct eap_sm *sm,
580                                    struct eap_aka_data *data,
581                                    u8 *respData, size_t respDataLen,
582                                    struct eap_sim_attrs *attr)
583 {
584         struct eap_sim_attrs eattr;
585         u8 *decrypted = NULL;
586         const u8 *identity, *id2;
587         size_t identity_len, id2_len;
588
589         wpa_printf(MSG_DEBUG, "EAP-AKA: Processing Reauthentication");
590
591         if (attr->mac == NULL ||
592             eap_sim_verify_mac(data->k_aut, respData, respDataLen, attr->mac,
593                                data->nonce_s, EAP_SIM_NONCE_S_LEN)) {
594                 wpa_printf(MSG_WARNING, "EAP-AKA: Re-authentication message "
595                            "did not include valid AT_MAC");
596                 goto fail;
597         }
598
599         if (attr->encr_data == NULL || attr->iv == NULL) {
600                 wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication "
601                            "message did not include encrypted data");
602                 goto fail;
603         }
604
605         decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
606                                        attr->encr_data_len, attr->iv, &eattr,
607                                        0);
608         if (decrypted == NULL) {
609                 wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted "
610                            "data from reauthentication message");
611                 goto fail;
612         }
613
614         if (eattr.counter != data->counter) {
615                 wpa_printf(MSG_WARNING, "EAP-AKA: Re-authentication message "
616                            "used incorrect counter %u, expected %u",
617                            eattr.counter, data->counter);
618                 goto fail;
619         }
620         free(decrypted);
621         decrypted = NULL;
622
623         wpa_printf(MSG_DEBUG, "EAP-AKA: Re-authentication response includes "
624                    "the correct AT_MAC");
625
626         if (eattr.counter_too_small) {
627                 wpa_printf(MSG_DEBUG, "EAP-AKA: Re-authentication response "
628                            "included AT_COUNTER_TOO_SMALL - starting full "
629                            "authentication");
630                 eap_aka_determine_identity(sm, data, 0, 1);
631                 return;
632         }
633
634         eap_aka_state(data, SUCCESS);
635
636         if (data->reauth) {
637                 identity = data->reauth->identity;
638                 identity_len = data->reauth->identity_len;
639         } else {
640                 identity = sm->identity;
641                 identity_len = sm->identity_len;
642         }
643
644         id2 = eap_sim_db_get_permanent(sm->eap_sim_db_priv, identity,
645                                        identity_len, &id2_len);
646         if (id2) {
647                 identity = id2;
648                 identity_len = id2_len;
649         }
650
651         if (data->next_pseudonym) {
652                 eap_sim_db_add_pseudonym(sm->eap_sim_db_priv, identity,
653                                          identity_len, data->next_pseudonym);
654                 data->next_pseudonym = NULL;
655         }
656         if (data->next_reauth_id) {
657                 eap_sim_db_add_reauth(sm->eap_sim_db_priv, identity,
658                                       identity_len, data->next_reauth_id,
659                                       data->counter + 1, data->mk);
660                 data->next_reauth_id = NULL;
661         } else {
662                 eap_sim_db_remove_reauth(sm->eap_sim_db_priv, data->reauth);
663                 data->reauth = NULL;
664         }
665
666         return;
667
668 fail:
669         data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
670         eap_aka_state(data, NOTIFICATION);
671         eap_sim_db_remove_reauth(sm->eap_sim_db_priv, data->reauth);
672         data->reauth = NULL;
673         free(decrypted);
674 }
675
676
677 static void eap_aka_process_client_error(struct eap_sm *sm,
678                                          struct eap_aka_data *data,
679                                          u8 *respData, size_t respDataLen,
680                                          struct eap_sim_attrs *attr)
681 {
682         wpa_printf(MSG_DEBUG, "EAP-AKA: Client reported error %d",
683                    attr->client_error_code);
684         eap_aka_state(data, FAILURE);
685 }
686
687
688 static void eap_aka_process_authentication_reject(
689         struct eap_sm *sm, struct eap_aka_data *data,
690         u8 *respData, size_t respDataLen, struct eap_sim_attrs *attr)
691 {
692         wpa_printf(MSG_DEBUG, "EAP-AKA: Client rejected authentication");
693         eap_aka_state(data, FAILURE);
694 }
695
696
697 static void eap_aka_process_notification(struct eap_sm *sm,
698                                          struct eap_aka_data *data,
699                                          u8 *respData, size_t respDataLen,
700                                          struct eap_sim_attrs *attr)
701 {
702         wpa_printf(MSG_DEBUG, "EAP-AKA: Client replied to notification");
703         eap_aka_state(data, FAILURE);
704 }
705
706
707 static void eap_aka_process(struct eap_sm *sm, void *priv,
708                             u8 *respData, size_t respDataLen)
709 {
710         struct eap_aka_data *data = priv;
711         struct eap_hdr *resp;
712         u8 *pos, subtype;
713         size_t len;
714         struct eap_sim_attrs attr;
715
716         resp = (struct eap_hdr *) respData;
717         pos = (u8 *) (resp + 1);
718         subtype = pos[1];
719
720         if (eap_aka_subtype_ok(data, subtype)) {
721                 wpa_printf(MSG_DEBUG, "EAP-AKA: Unrecognized or unexpected "
722                            "EAP-AKA Subtype in EAP Response");
723                 data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
724                 eap_aka_state(data, NOTIFICATION);
725                 return;
726         }
727
728         len = be_to_host16(resp->length);
729         pos += 4;
730
731         if (eap_sim_parse_attr(pos, respData + len, &attr, 1, 0)) {
732                 wpa_printf(MSG_DEBUG, "EAP-AKA: Failed to parse attributes");
733                 data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
734                 eap_aka_state(data, NOTIFICATION);
735                 return;
736         }
737
738         if (subtype == EAP_AKA_SUBTYPE_CLIENT_ERROR) {
739                 eap_aka_process_client_error(sm, data, respData, len, &attr);
740                 return;
741         }
742
743         if (subtype == EAP_AKA_SUBTYPE_AUTHENTICATION_REJECT) {
744                 eap_aka_process_authentication_reject(sm, data, respData, len,
745                                                       &attr);
746                 return;
747         }
748
749         switch (data->state) {
750         case IDENTITY:
751                 eap_aka_process_identity(sm, data, respData, len, &attr);
752                 break;
753         case CHALLENGE:
754                 if (subtype == EAP_AKA_SUBTYPE_SYNCHRONIZATION_FAILURE) {
755                         eap_aka_process_sync_failure(sm, data, respData, len,
756                                                      &attr);
757                 } else {
758                         eap_aka_process_challenge(sm, data, respData, len,
759                                                   &attr);
760                 }
761                 break;
762         case REAUTH:
763                 eap_aka_process_reauth(sm, data, respData, len, &attr);
764                 break;
765         case NOTIFICATION:
766                 eap_aka_process_notification(sm, data, respData, len, &attr);
767                 break;
768         default:
769                 wpa_printf(MSG_DEBUG, "EAP-AKA: Unknown state %d in "
770                            "process", data->state);
771                 break;
772         }
773 }
774
775
776 static Boolean eap_aka_isDone(struct eap_sm *sm, void *priv)
777 {
778         struct eap_aka_data *data = priv;
779         return data->state == SUCCESS || data->state == FAILURE;
780 }
781
782
783 static u8 * eap_aka_getKey(struct eap_sm *sm, void *priv, size_t *len)
784 {
785         struct eap_aka_data *data = priv;
786         u8 *key;
787
788         if (data->state != SUCCESS)
789                 return NULL;
790
791         key = malloc(EAP_SIM_KEYING_DATA_LEN);
792         if (key == NULL)
793                 return NULL;
794         memcpy(key, data->msk, EAP_SIM_KEYING_DATA_LEN);
795         *len = EAP_SIM_KEYING_DATA_LEN;
796         return key;
797 }
798
799
800 static u8 * eap_aka_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
801 {
802         struct eap_aka_data *data = priv;
803         u8 *key;
804
805         if (data->state != SUCCESS)
806                 return NULL;
807
808         key = malloc(EAP_EMSK_LEN);
809         if (key == NULL)
810                 return NULL;
811         memcpy(key, data->emsk, EAP_EMSK_LEN);
812         *len = EAP_EMSK_LEN;
813         return key;
814 }
815
816
817 static Boolean eap_aka_isSuccess(struct eap_sm *sm, void *priv)
818 {
819         struct eap_aka_data *data = priv;
820         return data->state == SUCCESS;
821 }
822
823
824 int eap_server_aka_register(void)
825 {
826         struct eap_method *eap;
827         int ret;
828
829         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
830                                       EAP_VENDOR_IETF, EAP_TYPE_AKA, "AKA");
831         if (eap == NULL)
832                 return -1;
833
834         eap->init = eap_aka_init;
835         eap->reset = eap_aka_reset;
836         eap->buildReq = eap_aka_buildReq;
837         eap->check = eap_aka_check;
838         eap->process = eap_aka_process;
839         eap->isDone = eap_aka_isDone;
840         eap->getKey = eap_aka_getKey;
841         eap->isSuccess = eap_aka_isSuccess;
842         eap->get_emsk = eap_aka_get_emsk;
843
844         ret = eap_server_method_register(eap);
845         if (ret)
846                 eap_server_method_free(eap);
847         return ret;
848 }