Merged EAP-AKA' into eap_aka.c and added it to defconfig/ChangeLog
[wpasupplicant] / src / eap_peer / eap_aka.c
1 /*
2  * EAP peer method: EAP-AKA (RFC 4187) and EAP-AKA' (draft-arkko-eap-aka-kdf)
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 "pcsc_funcs.h"
20 #include "eap_common/eap_sim_common.h"
21 #include "sha1.h"
22 #include "sha256.h"
23 #include "crypto.h"
24 #include "eap_peer/eap_config.h"
25 #ifdef CONFIG_USIM_SIMULATOR
26 #include "hlr_auc_gw/milenage.h"
27 #endif /* CONFIG_USIM_SIMULATOR */
28
29
30 struct eap_aka_data {
31         u8 ik[EAP_AKA_IK_LEN], ck[EAP_AKA_CK_LEN], res[EAP_AKA_RES_MAX_LEN];
32         size_t res_len;
33         u8 nonce_s[EAP_SIM_NONCE_S_LEN];
34         u8 mk[EAP_SIM_MK_LEN];
35         u8 k_aut[EAP_AKA_PRIME_K_AUT_LEN];
36         u8 k_encr[EAP_SIM_K_ENCR_LEN];
37         u8 k_re[EAP_AKA_PRIME_K_RE_LEN]; /* EAP-AKA' only */
38         u8 msk[EAP_SIM_KEYING_DATA_LEN];
39         u8 emsk[EAP_EMSK_LEN];
40         u8 rand[EAP_AKA_RAND_LEN], autn[EAP_AKA_AUTN_LEN];
41         u8 auts[EAP_AKA_AUTS_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
56         struct wpabuf *id_msgs;
57         int prev_id;
58         int result_ind, use_result_ind;
59         u8 eap_method;
60         u8 *network_name;
61         size_t network_name_len;
62         u16 kdf;
63 };
64
65
66 #ifndef CONFIG_NO_STDOUT_DEBUG
67 static const char * eap_aka_state_txt(int state)
68 {
69         switch (state) {
70         case CONTINUE:
71                 return "CONTINUE";
72         case RESULT_SUCCESS:
73                 return "RESULT_SUCCESS";
74         case RESULT_FAILURE:
75                 return "RESULT_FAILURE";
76         case SUCCESS:
77                 return "SUCCESS";
78         case FAILURE:
79                 return "FAILURE";
80         default:
81                 return "?";
82         }
83 }
84 #endif /* CONFIG_NO_STDOUT_DEBUG */
85
86
87 static void eap_aka_state(struct eap_aka_data *data, int state)
88 {
89         wpa_printf(MSG_DEBUG, "EAP-AKA: %s -> %s",
90                    eap_aka_state_txt(data->state),
91                    eap_aka_state_txt(state));
92         data->state = state;
93 }
94
95
96 static void * eap_aka_init(struct eap_sm *sm)
97 {
98         struct eap_aka_data *data;
99         const char *phase1 = eap_get_config_phase1(sm);
100
101         data = os_zalloc(sizeof(*data));
102         if (data == NULL)
103                 return NULL;
104
105         data->eap_method = EAP_TYPE_AKA;
106
107         eap_aka_state(data, CONTINUE);
108         data->prev_id = -1;
109
110         data->result_ind = phase1 && os_strstr(phase1, "result_ind=1") != NULL;
111
112         return data;
113 }
114
115
116 #ifdef EAP_AKA_PRIME
117 static void * eap_aka_prime_init(struct eap_sm *sm)
118 {
119         struct eap_aka_data *data = eap_aka_init(sm);
120         if (data == NULL)
121                 return NULL;
122         data->eap_method = EAP_TYPE_AKA_PRIME;
123         return data;
124 }
125 #endif /* EAP_AKA_PRIME */
126
127
128 static void eap_aka_deinit(struct eap_sm *sm, void *priv)
129 {
130         struct eap_aka_data *data = priv;
131         if (data) {
132                 os_free(data->pseudonym);
133                 os_free(data->reauth_id);
134                 os_free(data->last_eap_identity);
135                 wpabuf_free(data->id_msgs);
136                 os_free(data->network_name);
137                 os_free(data);
138         }
139 }
140
141
142 static int eap_aka_umts_auth(struct eap_sm *sm, struct eap_aka_data *data)
143 {
144         struct eap_peer_config *conf;
145
146         wpa_printf(MSG_DEBUG, "EAP-AKA: UMTS authentication algorithm");
147
148         conf = eap_get_config(sm);
149         if (conf == NULL)
150                 return -1;
151         if (conf->pcsc) {
152                 return scard_umts_auth(sm->scard_ctx, data->rand,
153                                        data->autn, data->res, &data->res_len,
154                                        data->ik, data->ck, data->auts);
155         }
156
157 #ifdef CONFIG_USIM_SIMULATOR
158         if (conf->password) {
159                 u8 opc[16], k[16], sqn[6];
160                 const char *pos;
161                 wpa_printf(MSG_DEBUG, "EAP-AKA: Use internal Milenage "
162                            "implementation for UMTS authentication");
163                 if (conf->password_len < 78) {
164                         wpa_printf(MSG_DEBUG, "EAP-AKA: invalid Milenage "
165                                    "password");
166                         return -1;
167                 }
168                 pos = (const char *) conf->password;
169                 if (hexstr2bin(pos, k, 16))
170                         return -1;
171                 pos += 32;
172                 if (*pos != ':')
173                         return -1;
174                 pos++;
175
176                 if (hexstr2bin(pos, opc, 16))
177                         return -1;
178                 pos += 32;
179                 if (*pos != ':')
180                         return -1;
181                 pos++;
182
183                 if (hexstr2bin(pos, sqn, 6))
184                         return -1;
185
186                 return milenage_check(opc, k, sqn, data->rand, data->autn,
187                                       data->ik, data->ck,
188                                       data->res, &data->res_len, data->auts);
189         }
190 #endif /* CONFIG_USIM_SIMULATOR */
191
192 #ifdef CONFIG_USIM_HARDCODED
193         wpa_printf(MSG_DEBUG, "EAP-AKA: Use hardcoded Kc and SRES values for "
194                    "testing");
195
196         /* These hardcoded Kc and SRES values are used for testing.
197          * Could consider making them configurable. */
198         os_memset(data->res, '2', EAP_AKA_RES_MAX_LEN);
199         data->res_len = EAP_AKA_RES_MAX_LEN;
200         os_memset(data->ik, '3', EAP_AKA_IK_LEN);
201         os_memset(data->ck, '4', EAP_AKA_CK_LEN);
202         {
203                 u8 autn[EAP_AKA_AUTN_LEN];
204                 os_memset(autn, '1', EAP_AKA_AUTN_LEN);
205                 if (os_memcmp(autn, data->autn, EAP_AKA_AUTN_LEN) != 0) {
206                         wpa_printf(MSG_WARNING, "EAP-AKA: AUTN did not match "
207                                    "with expected value");
208                         return -1;
209                 }
210         }
211 #if 0
212         {
213                 static int test_resync = 1;
214                 if (test_resync) {
215                         /* Test Resynchronization */
216                         test_resync = 0;
217                         return -2;
218                 }
219         }
220 #endif
221         return 0;
222
223 #else /* CONFIG_USIM_HARDCODED */
224
225         wpa_printf(MSG_DEBUG, "EAP-AKA: No UMTS authentication algorith "
226                    "enabled");
227         return -1;
228
229 #endif /* CONFIG_USIM_HARDCODED */
230 }
231
232
233 #define CLEAR_PSEUDONYM 0x01
234 #define CLEAR_REAUTH_ID 0x02
235 #define CLEAR_EAP_ID    0x04
236
237 static void eap_aka_clear_identities(struct eap_aka_data *data, int id)
238 {
239         wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old%s%s%s",
240                    id & CLEAR_PSEUDONYM ? " pseudonym" : "",
241                    id & CLEAR_REAUTH_ID ? " reauth_id" : "",
242                    id & CLEAR_EAP_ID ? " eap_id" : "");
243         if (id & CLEAR_PSEUDONYM) {
244                 os_free(data->pseudonym);
245                 data->pseudonym = NULL;
246                 data->pseudonym_len = 0;
247         }
248         if (id & CLEAR_REAUTH_ID) {
249                 os_free(data->reauth_id);
250                 data->reauth_id = NULL;
251                 data->reauth_id_len = 0;
252         }
253         if (id & CLEAR_EAP_ID) {
254                 os_free(data->last_eap_identity);
255                 data->last_eap_identity = NULL;
256                 data->last_eap_identity_len = 0;
257         }
258 }
259
260
261 static int eap_aka_learn_ids(struct eap_aka_data *data,
262                              struct eap_sim_attrs *attr)
263 {
264         if (attr->next_pseudonym) {
265                 os_free(data->pseudonym);
266                 data->pseudonym = os_malloc(attr->next_pseudonym_len);
267                 if (data->pseudonym == NULL) {
268                         wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for "
269                                    "next pseudonym");
270                         return -1;
271                 }
272                 os_memcpy(data->pseudonym, attr->next_pseudonym,
273                           attr->next_pseudonym_len);
274                 data->pseudonym_len = attr->next_pseudonym_len;
275                 wpa_hexdump_ascii(MSG_DEBUG,
276                                   "EAP-AKA: (encr) AT_NEXT_PSEUDONYM",
277                                   data->pseudonym,
278                                   data->pseudonym_len);
279         }
280
281         if (attr->next_reauth_id) {
282                 os_free(data->reauth_id);
283                 data->reauth_id = os_malloc(attr->next_reauth_id_len);
284                 if (data->reauth_id == NULL) {
285                         wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for "
286                                    "next reauth_id");
287                         return -1;
288                 }
289                 os_memcpy(data->reauth_id, attr->next_reauth_id,
290                           attr->next_reauth_id_len);
291                 data->reauth_id_len = attr->next_reauth_id_len;
292                 wpa_hexdump_ascii(MSG_DEBUG,
293                                   "EAP-AKA: (encr) AT_NEXT_REAUTH_ID",
294                                   data->reauth_id,
295                                   data->reauth_id_len);
296         }
297
298         return 0;
299 }
300
301
302 static int eap_aka_add_id_msg(struct eap_aka_data *data,
303                               const struct wpabuf *msg)
304 {
305         if (msg == NULL)
306                 return -1;
307
308         if (data->id_msgs == NULL) {
309                 data->id_msgs = wpabuf_dup(msg);
310                 return data->id_msgs == NULL ? -1 : 0;
311         }
312
313         if (wpabuf_resize(&data->id_msgs, wpabuf_len(msg)) < 0)
314                 return -1;
315         wpabuf_put_buf(data->id_msgs, msg);
316
317         return 0;
318 }
319
320
321 static void eap_aka_add_checkcode(struct eap_aka_data *data,
322                                   struct eap_sim_msg *msg)
323 {
324         const u8 *addr;
325         size_t len;
326         u8 hash[SHA256_MAC_LEN];
327
328         wpa_printf(MSG_DEBUG, "   AT_CHECKCODE");
329
330         if (data->id_msgs == NULL) {
331                 /*
332                  * No EAP-AKA/Identity packets were exchanged - send empty
333                  * checkcode.
334                  */
335                 eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, NULL, 0);
336                 return;
337         }
338
339         /* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */
340         addr = wpabuf_head(data->id_msgs);
341         len = wpabuf_len(data->id_msgs);
342         wpa_hexdump(MSG_MSGDUMP, "EAP-AKA: AT_CHECKCODE data", addr, len);
343         if (data->eap_method == EAP_TYPE_AKA_PRIME)
344                 sha256_vector(1, &addr, &len, hash);
345         else
346                 sha1_vector(1, &addr, &len, hash);
347
348         eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, hash,
349                         data->eap_method == EAP_TYPE_AKA_PRIME ?
350                         EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN);
351 }
352
353
354 static int eap_aka_verify_checkcode(struct eap_aka_data *data,
355                                     const u8 *checkcode, size_t checkcode_len)
356 {
357         const u8 *addr;
358         size_t len;
359         u8 hash[SHA256_MAC_LEN];
360         size_t hash_len;
361
362         if (checkcode == NULL)
363                 return -1;
364
365         if (data->id_msgs == NULL) {
366                 if (checkcode_len != 0) {
367                         wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server "
368                                    "indicates that AKA/Identity messages were "
369                                    "used, but they were not");
370                         return -1;
371                 }
372                 return 0;
373         }
374
375         hash_len = data->eap_method == EAP_TYPE_AKA_PRIME ?
376                 EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN;
377
378         if (checkcode_len != hash_len) {
379                 wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server "
380                            "indicates that AKA/Identity message were not "
381                            "used, but they were");
382                 return -1;
383         }
384
385         /* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */
386         addr = wpabuf_head(data->id_msgs);
387         len = wpabuf_len(data->id_msgs);
388         if (data->eap_method == EAP_TYPE_AKA_PRIME)
389                 sha256_vector(1, &addr, &len, hash);
390         else
391                 sha1_vector(1, &addr, &len, hash);
392
393         if (os_memcmp(hash, checkcode, hash_len) != 0) {
394                 wpa_printf(MSG_DEBUG, "EAP-AKA: Mismatch in AT_CHECKCODE");
395                 return -1;
396         }
397
398         return 0;
399 }
400
401
402 static struct wpabuf * eap_aka_client_error(struct eap_aka_data *data, u8 id,
403                                             int err)
404 {
405         struct eap_sim_msg *msg;
406
407         eap_aka_state(data, FAILURE);
408         data->num_id_req = 0;
409         data->num_notification = 0;
410
411         msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
412                                EAP_AKA_SUBTYPE_CLIENT_ERROR);
413         eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
414         return eap_sim_msg_finish(msg, NULL, NULL, 0);
415 }
416
417
418 static struct wpabuf * eap_aka_authentication_reject(struct eap_aka_data *data,
419                                                      u8 id)
420 {
421         struct eap_sim_msg *msg;
422
423         eap_aka_state(data, FAILURE);
424         data->num_id_req = 0;
425         data->num_notification = 0;
426
427         wpa_printf(MSG_DEBUG, "Generating EAP-AKA Authentication-Reject "
428                    "(id=%d)", id);
429         msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
430                                EAP_AKA_SUBTYPE_AUTHENTICATION_REJECT);
431         return eap_sim_msg_finish(msg, NULL, NULL, 0);
432 }
433
434
435 static struct wpabuf * eap_aka_synchronization_failure(
436         struct eap_aka_data *data, u8 id)
437 {
438         struct eap_sim_msg *msg;
439
440         data->num_id_req = 0;
441         data->num_notification = 0;
442
443         wpa_printf(MSG_DEBUG, "Generating EAP-AKA Synchronization-Failure "
444                    "(id=%d)", id);
445         msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
446                                EAP_AKA_SUBTYPE_SYNCHRONIZATION_FAILURE);
447         wpa_printf(MSG_DEBUG, "   AT_AUTS");
448         eap_sim_msg_add_full(msg, EAP_SIM_AT_AUTS, data->auts,
449                              EAP_AKA_AUTS_LEN);
450         return eap_sim_msg_finish(msg, NULL, NULL, 0);
451 }
452
453
454 static struct wpabuf * eap_aka_response_identity(struct eap_sm *sm,
455                                                  struct eap_aka_data *data,
456                                                  u8 id,
457                                                  enum eap_sim_id_req id_req)
458 {
459         const u8 *identity = NULL;
460         size_t identity_len = 0;
461         struct eap_sim_msg *msg;
462
463         data->reauth = 0;
464         if (id_req == ANY_ID && data->reauth_id) {
465                 identity = data->reauth_id;
466                 identity_len = data->reauth_id_len;
467                 data->reauth = 1;
468         } else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
469                    data->pseudonym) {
470                 identity = data->pseudonym;
471                 identity_len = data->pseudonym_len;
472                 eap_aka_clear_identities(data, CLEAR_REAUTH_ID);
473         } else if (id_req != NO_ID_REQ) {
474                 identity = eap_get_config_identity(sm, &identity_len);
475                 if (identity) {
476                         eap_aka_clear_identities(data, CLEAR_PSEUDONYM |
477                                                  CLEAR_REAUTH_ID);
478                 }
479         }
480         if (id_req != NO_ID_REQ)
481                 eap_aka_clear_identities(data, CLEAR_EAP_ID);
482
483         wpa_printf(MSG_DEBUG, "Generating EAP-AKA Identity (id=%d)", id);
484         msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
485                                EAP_AKA_SUBTYPE_IDENTITY);
486
487         if (identity) {
488                 wpa_hexdump_ascii(MSG_DEBUG, "   AT_IDENTITY",
489                                   identity, identity_len);
490                 eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
491                                 identity, identity_len);
492         }
493
494         return eap_sim_msg_finish(msg, NULL, NULL, 0);
495 }
496
497
498 static struct wpabuf * eap_aka_response_challenge(struct eap_aka_data *data,
499                                                   u8 id)
500 {
501         struct eap_sim_msg *msg;
502
503         wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d)", id);
504         msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
505                                EAP_AKA_SUBTYPE_CHALLENGE);
506         wpa_printf(MSG_DEBUG, "   AT_RES");
507         eap_sim_msg_add(msg, EAP_SIM_AT_RES, data->res_len * 8,
508                         data->res, data->res_len);
509         eap_aka_add_checkcode(data, msg);
510         if (data->use_result_ind) {
511                 wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
512                 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
513         }
514         wpa_printf(MSG_DEBUG, "   AT_MAC");
515         eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
516         return eap_sim_msg_finish(msg, data->k_aut, (u8 *) "", 0);
517 }
518
519
520 static struct wpabuf * eap_aka_response_reauth(struct eap_aka_data *data,
521                                                u8 id, int counter_too_small,
522                                                const u8 *nonce_s)
523 {
524         struct eap_sim_msg *msg;
525         unsigned int counter;
526
527         wpa_printf(MSG_DEBUG, "Generating EAP-AKA Reauthentication (id=%d)",
528                    id);
529         msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
530                                EAP_AKA_SUBTYPE_REAUTHENTICATION);
531         wpa_printf(MSG_DEBUG, "   AT_IV");
532         wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
533         eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
534
535         if (counter_too_small) {
536                 wpa_printf(MSG_DEBUG, "   *AT_COUNTER_TOO_SMALL");
537                 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
538                 counter = data->counter_too_small;
539         } else
540                 counter = data->counter;
541
542         wpa_printf(MSG_DEBUG, "   *AT_COUNTER %d", counter);
543         eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
544
545         if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
546                 wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt "
547                            "AT_ENCR_DATA");
548                 eap_sim_msg_free(msg);
549                 return NULL;
550         }
551         eap_aka_add_checkcode(data, msg);
552         if (data->use_result_ind) {
553                 wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
554                 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
555         }
556         wpa_printf(MSG_DEBUG, "   AT_MAC");
557         eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
558         return eap_sim_msg_finish(msg, data->k_aut, nonce_s,
559                                   EAP_SIM_NONCE_S_LEN);
560 }
561
562
563 static struct wpabuf * eap_aka_response_notification(struct eap_aka_data *data,
564                                                      u8 id, u16 notification)
565 {
566         struct eap_sim_msg *msg;
567         u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
568
569         wpa_printf(MSG_DEBUG, "Generating EAP-AKA Notification (id=%d)", id);
570         msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
571                                EAP_AKA_SUBTYPE_NOTIFICATION);
572         if (k_aut && data->reauth) {
573                 wpa_printf(MSG_DEBUG, "   AT_IV");
574                 wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
575                 eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
576                                            EAP_SIM_AT_ENCR_DATA);
577                 wpa_printf(MSG_DEBUG, "   *AT_COUNTER %d", data->counter);
578                 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
579                                 NULL, 0);
580                 if (eap_sim_msg_add_encr_end(msg, data->k_encr,
581                                              EAP_SIM_AT_PADDING)) {
582                         wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt "
583                                    "AT_ENCR_DATA");
584                         eap_sim_msg_free(msg);
585                         return NULL;
586                 }
587         }
588         if (k_aut) {
589                 wpa_printf(MSG_DEBUG, "   AT_MAC");
590                 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
591         }
592         return eap_sim_msg_finish(msg, k_aut, (u8 *) "", 0);
593 }
594
595
596 static struct wpabuf * eap_aka_process_identity(struct eap_sm *sm,
597                                                 struct eap_aka_data *data,
598                                                 u8 id,
599                                                 const struct wpabuf *reqData,
600                                                 struct eap_sim_attrs *attr)
601 {
602         int id_error;
603         struct wpabuf *buf;
604
605         wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Identity");
606
607         id_error = 0;
608         switch (attr->id_req) {
609         case NO_ID_REQ:
610                 break;
611         case ANY_ID:
612                 if (data->num_id_req > 0)
613                         id_error++;
614                 data->num_id_req++;
615                 break;
616         case FULLAUTH_ID:
617                 if (data->num_id_req > 1)
618                         id_error++;
619                 data->num_id_req++;
620                 break;
621         case PERMANENT_ID:
622                 if (data->num_id_req > 2)
623                         id_error++;
624                 data->num_id_req++;
625                 break;
626         }
627         if (id_error) {
628                 wpa_printf(MSG_INFO, "EAP-AKA: Too many ID requests "
629                            "used within one authentication");
630                 return eap_aka_client_error(data, id,
631                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
632         }
633
634         buf = eap_aka_response_identity(sm, data, id, attr->id_req);
635
636         if (data->prev_id != id) {
637                 eap_aka_add_id_msg(data, reqData);
638                 eap_aka_add_id_msg(data, buf);
639                 data->prev_id = id;
640         }
641
642         return buf;
643 }
644
645
646 static int eap_aka_verify_mac(struct eap_aka_data *data,
647                               const struct wpabuf *req,
648                               const u8 *mac, const u8 *extra,
649                               size_t extra_len)
650 {
651         if (data->eap_method == EAP_TYPE_AKA_PRIME)
652                 return eap_sim_verify_mac_sha256(data->k_aut, req, mac, extra,
653                                                  extra_len);
654         return eap_sim_verify_mac(data->k_aut, req, mac, extra, extra_len);
655 }
656
657
658 #ifdef EAP_AKA_PRIME
659 static struct wpabuf * eap_aka_prime_kdf_select(struct eap_aka_data *data,
660                                                 u8 id, u16 kdf)
661 {
662         struct eap_sim_msg *msg;
663
664         data->kdf = kdf;
665         wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d) (KDF "
666                    "select)", id);
667         msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
668                                EAP_AKA_SUBTYPE_CHALLENGE);
669         wpa_printf(MSG_DEBUG, "   AT_KDF");
670         eap_sim_msg_add(msg, EAP_SIM_AT_KDF, kdf, NULL, 0);
671         return eap_sim_msg_finish(msg, NULL, NULL, 0);
672 }
673
674
675 static struct wpabuf * eap_aka_prime_kdf_neg(struct eap_aka_data *data,
676                                              u8 id, struct eap_sim_attrs *attr)
677 {
678         size_t i;
679
680         for (i = 0; i < attr->kdf_count; i++) {
681                 if (attr->kdf[i] == EAP_AKA_PRIME_KDF)
682                         return eap_aka_prime_kdf_select(data, id,
683                                                         EAP_AKA_PRIME_KDF);
684         }
685
686         /* No matching KDF found - fail authentication as if AUTN had been
687          * incorrect */
688         return eap_aka_authentication_reject(data, id);
689 }
690
691
692 static int eap_aka_prime_kdf_valid(struct eap_aka_data *data,
693                                    struct eap_sim_attrs *attr)
694 {
695         size_t i, j;
696
697         if (attr->kdf_count == 0)
698                 return 0;
699
700         /* The only allowed (and required) duplication of a KDF is the addition
701          * of the selected KDF into the beginning of the list. */
702
703         if (data->kdf) {
704                 if (attr->kdf[0] != data->kdf) {
705                         wpa_printf(MSG_WARNING, "EAP-AKA': The server did not "
706                                    "accept the selected KDF");
707                         return 0;
708                 }
709
710                 for (i = 1; i < attr->kdf_count; i++) {
711                         if (attr->kdf[i] == data->kdf)
712                                 break;
713                 }
714                 if (i == attr->kdf_count &&
715                     attr->kdf_count < EAP_AKA_PRIME_KDF_MAX) {
716                         wpa_printf(MSG_WARNING, "EAP-AKA': The server did not "
717                                    "duplicate the selected KDF");
718                         return 0;
719                 }
720
721                 /* TODO: should check that the list is identical to the one
722                  * used in the previous Challenge message apart from the added
723                  * entry in the beginning. */
724         }
725
726         for (i = data->kdf ? 1 : 0; i < attr->kdf_count; i++) {
727                 for (j = i + 1; j < attr->kdf_count; j++) {
728                         if (attr->kdf[i] == attr->kdf[j]) {
729                                 wpa_printf(MSG_WARNING, "EAP-AKA': The server "
730                                            "included a duplicated KDF");
731                                 return 0;
732                         }
733                 }
734         }
735
736         return 1;
737 }
738 #endif /* EAP_AKA_PRIME */
739
740
741 static struct wpabuf * eap_aka_process_challenge(struct eap_sm *sm,
742                                                  struct eap_aka_data *data,
743                                                  u8 id,
744                                                  const struct wpabuf *reqData,
745                                                  struct eap_sim_attrs *attr)
746 {
747         const u8 *identity;
748         size_t identity_len;
749         int res;
750         struct eap_sim_attrs eattr;
751
752         wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Challenge");
753
754         if (attr->checkcode &&
755             eap_aka_verify_checkcode(data, attr->checkcode,
756                                      attr->checkcode_len)) {
757                 wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the "
758                            "message");
759                 return eap_aka_client_error(data, id,
760                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
761         }
762
763 #ifdef EAP_AKA_PRIME
764         if (data->eap_method == EAP_TYPE_AKA_PRIME) {
765                 if (!attr->kdf_input || attr->kdf_input_len == 0) {
766                         wpa_printf(MSG_WARNING, "EAP-AKA': Challenge message "
767                                    "did not include non-empty AT_KDF_INPUT");
768                         /* Fail authentication as if AUTN had been incorrect */
769                         return eap_aka_authentication_reject(data, id);
770                 }
771                 os_free(data->network_name);
772                 data->network_name = os_malloc(attr->kdf_input_len);
773                 if (data->network_name == NULL) {
774                         wpa_printf(MSG_WARNING, "EAP-AKA': No memory for "
775                                    "storing Network Name");
776                         return eap_aka_authentication_reject(data, id);
777                 }
778                 os_memcpy(data->network_name, attr->kdf_input,
779                           attr->kdf_input_len);
780                 data->network_name_len = attr->kdf_input_len;
781                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA': Network Name "
782                                   "(AT_KDF_INPUT)",
783                                   data->network_name, data->network_name_len);
784                 /* TODO: check Network Name per 3GPP.33.402 */
785
786                 if (!eap_aka_prime_kdf_valid(data, attr))
787                         return eap_aka_authentication_reject(data, id);
788
789                 if (attr->kdf[0] != EAP_AKA_PRIME_KDF)
790                         return eap_aka_prime_kdf_neg(data, id, attr);
791
792                 data->kdf = EAP_AKA_PRIME_KDF;
793                 wpa_printf(MSG_DEBUG, "EAP-AKA': KDF %d selected", data->kdf);
794         }
795
796         if (data->eap_method == EAP_TYPE_AKA && attr->bidding) {
797                 u16 flags = WPA_GET_BE16(attr->bidding);
798                 if ((flags & EAP_AKA_BIDDING_FLAG_D) &&
799                     eap_allowed_method(sm, EAP_VENDOR_IETF,
800                                        EAP_TYPE_AKA_PRIME)) {
801                         wpa_printf(MSG_WARNING, "EAP-AKA: Bidding down from "
802                                    "AKA' to AKA detected");
803                         /* Fail authentication as if AUTN had been incorrect */
804                         return eap_aka_authentication_reject(data, id);
805                 }
806         }
807 #endif /* EAP_AKA_PRIME */
808
809         data->reauth = 0;
810         if (!attr->mac || !attr->rand || !attr->autn) {
811                 wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message "
812                            "did not include%s%s%s",
813                            !attr->mac ? " AT_MAC" : "",
814                            !attr->rand ? " AT_RAND" : "",
815                            !attr->autn ? " AT_AUTN" : "");
816                 return eap_aka_client_error(data, id,
817                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
818         }
819         os_memcpy(data->rand, attr->rand, EAP_AKA_RAND_LEN);
820         os_memcpy(data->autn, attr->autn, EAP_AKA_AUTN_LEN);
821
822         res = eap_aka_umts_auth(sm, data);
823         if (res == -1) {
824                 wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication "
825                            "failed (AUTN)");
826                 return eap_aka_authentication_reject(data, id);
827         } else if (res == -2) {
828                 wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication "
829                            "failed (AUTN seq# -> AUTS)");
830                 return eap_aka_synchronization_failure(data, id);
831         } else if (res) {
832                 wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication failed");
833                 return eap_aka_client_error(data, id,
834                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
835         }
836 #ifdef EAP_AKA_PRIME
837         if (data->eap_method == EAP_TYPE_AKA_PRIME) {
838                 /* Note: AUTN = (SQN ^ AK) || AMF || MAC which gives us the
839                  * needed 6-octet SQN ^AK for CK',IK' derivation */
840                 eap_aka_prime_derive_ck_ik_prime(data->ck, data->ik,
841                                                  data->autn,
842                                                  data->network_name,
843                                                  data->network_name_len);
844         }
845 #endif /* EAP_AKA_PRIME */
846         if (data->last_eap_identity) {
847                 identity = data->last_eap_identity;
848                 identity_len = data->last_eap_identity_len;
849         } else if (data->pseudonym) {
850                 identity = data->pseudonym;
851                 identity_len = data->pseudonym_len;
852         } else
853                 identity = eap_get_config_identity(sm, &identity_len);
854         wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA: Selected identity for MK "
855                           "derivation", identity, identity_len);
856         if (data->eap_method == EAP_TYPE_AKA_PRIME) {
857                 eap_aka_prime_derive_keys(identity, identity_len, data->ik,
858                                           data->ck, data->k_encr, data->k_aut,
859                                           data->k_re, data->msk, data->emsk);
860         } else {
861                 eap_aka_derive_mk(identity, identity_len, data->ik, data->ck,
862                                   data->mk);
863                 eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut,
864                                     data->msk, data->emsk);
865         }
866         if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
867                 wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message "
868                            "used invalid AT_MAC");
869                 return eap_aka_client_error(data, id,
870                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
871         }
872
873         /* Old reauthentication and pseudonym identities must not be used
874          * anymore. In other words, if no new identities are received, full
875          * authentication will be used on next reauthentication. */
876         eap_aka_clear_identities(data, CLEAR_PSEUDONYM | CLEAR_REAUTH_ID |
877                                  CLEAR_EAP_ID);
878
879         if (attr->encr_data) {
880                 u8 *decrypted;
881                 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
882                                                attr->encr_data_len, attr->iv,
883                                                &eattr, 0);
884                 if (decrypted == NULL) {
885                         return eap_aka_client_error(
886                                 data, id, EAP_AKA_UNABLE_TO_PROCESS_PACKET);
887                 }
888                 eap_aka_learn_ids(data, &eattr);
889                 os_free(decrypted);
890         }
891
892         if (data->result_ind && attr->result_ind)
893                 data->use_result_ind = 1;
894
895         if (data->state != FAILURE && data->state != RESULT_FAILURE) {
896                 eap_aka_state(data, data->use_result_ind ?
897                               RESULT_SUCCESS : SUCCESS);
898         }
899
900         data->num_id_req = 0;
901         data->num_notification = 0;
902         /* RFC 4187 specifies that counter is initialized to one after
903          * fullauth, but initializing it to zero makes it easier to implement
904          * reauth verification. */
905         data->counter = 0;
906         return eap_aka_response_challenge(data, id);
907 }
908
909
910 static int eap_aka_process_notification_reauth(struct eap_aka_data *data,
911                                                struct eap_sim_attrs *attr)
912 {
913         struct eap_sim_attrs eattr;
914         u8 *decrypted;
915
916         if (attr->encr_data == NULL || attr->iv == NULL) {
917                 wpa_printf(MSG_WARNING, "EAP-AKA: Notification message after "
918                            "reauth did not include encrypted data");
919                 return -1;
920         }
921
922         decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
923                                        attr->encr_data_len, attr->iv, &eattr,
924                                        0);
925         if (decrypted == NULL) {
926                 wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted "
927                            "data from notification message");
928                 return -1;
929         }
930
931         if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
932                 wpa_printf(MSG_WARNING, "EAP-AKA: Counter in notification "
933                            "message does not match with counter in reauth "
934                            "message");
935                 os_free(decrypted);
936                 return -1;
937         }
938
939         os_free(decrypted);
940         return 0;
941 }
942
943
944 static int eap_aka_process_notification_auth(struct eap_aka_data *data,
945                                              const struct wpabuf *reqData,
946                                              struct eap_sim_attrs *attr)
947 {
948         if (attr->mac == NULL) {
949                 wpa_printf(MSG_INFO, "EAP-AKA: no AT_MAC in after_auth "
950                            "Notification message");
951                 return -1;
952         }
953
954         if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
955                 wpa_printf(MSG_WARNING, "EAP-AKA: Notification message "
956                            "used invalid AT_MAC");
957                 return -1;
958         }
959
960         if (data->reauth &&
961             eap_aka_process_notification_reauth(data, attr)) {
962                 wpa_printf(MSG_WARNING, "EAP-AKA: Invalid notification "
963                            "message after reauth");
964                 return -1;
965         }
966
967         return 0;
968 }
969
970
971 static struct wpabuf * eap_aka_process_notification(
972         struct eap_sm *sm, struct eap_aka_data *data, u8 id,
973         const struct wpabuf *reqData, struct eap_sim_attrs *attr)
974 {
975         wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Notification");
976         if (data->num_notification > 0) {
977                 wpa_printf(MSG_INFO, "EAP-AKA: too many notification "
978                            "rounds (only one allowed)");
979                 return eap_aka_client_error(data, id,
980                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
981         }
982         data->num_notification++;
983         if (attr->notification == -1) {
984                 wpa_printf(MSG_INFO, "EAP-AKA: no AT_NOTIFICATION in "
985                            "Notification message");
986                 return eap_aka_client_error(data, id,
987                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
988         }
989
990         if ((attr->notification & 0x4000) == 0 &&
991             eap_aka_process_notification_auth(data, reqData, attr)) {
992                 return eap_aka_client_error(data, id,
993                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
994         }
995
996         eap_sim_report_notification(sm->msg_ctx, attr->notification, 1);
997         if (attr->notification >= 0 && attr->notification < 32768) {
998                 eap_aka_state(data, FAILURE);
999         } else if (attr->notification == EAP_SIM_SUCCESS &&
1000                    data->state == RESULT_SUCCESS)
1001                 eap_aka_state(data, SUCCESS);
1002         return eap_aka_response_notification(data, id, attr->notification);
1003 }
1004
1005
1006 static struct wpabuf * eap_aka_process_reauthentication(
1007         struct eap_sm *sm, struct eap_aka_data *data, u8 id,
1008         const struct wpabuf *reqData, struct eap_sim_attrs *attr)
1009 {
1010         struct eap_sim_attrs eattr;
1011         u8 *decrypted;
1012
1013         wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Reauthentication");
1014
1015         if (attr->checkcode &&
1016             eap_aka_verify_checkcode(data, attr->checkcode,
1017                                      attr->checkcode_len)) {
1018                 wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the "
1019                            "message");
1020                 return eap_aka_client_error(data, id,
1021                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1022         }
1023
1024         if (data->reauth_id == NULL) {
1025                 wpa_printf(MSG_WARNING, "EAP-AKA: Server is trying "
1026                            "reauthentication, but no reauth_id available");
1027                 return eap_aka_client_error(data, id,
1028                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1029         }
1030
1031         data->reauth = 1;
1032         if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
1033                 wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication "
1034                            "did not have valid AT_MAC");
1035                 return eap_aka_client_error(data, id,
1036                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1037         }
1038
1039         if (attr->encr_data == NULL || attr->iv == NULL) {
1040                 wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication "
1041                            "message did not include encrypted data");
1042                 return eap_aka_client_error(data, id,
1043                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1044         }
1045
1046         decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
1047                                        attr->encr_data_len, attr->iv, &eattr,
1048                                        0);
1049         if (decrypted == NULL) {
1050                 wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted "
1051                            "data from reauthentication message");
1052                 return eap_aka_client_error(data, id,
1053                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1054         }
1055
1056         if (eattr.nonce_s == NULL || eattr.counter < 0) {
1057                 wpa_printf(MSG_INFO, "EAP-AKA: (encr) No%s%s in reauth packet",
1058                            !eattr.nonce_s ? " AT_NONCE_S" : "",
1059                            eattr.counter < 0 ? " AT_COUNTER" : "");
1060                 os_free(decrypted);
1061                 return eap_aka_client_error(data, id,
1062                                             EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1063         }
1064
1065         if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
1066                 struct wpabuf *res;
1067                 wpa_printf(MSG_INFO, "EAP-AKA: (encr) Invalid counter "
1068                            "(%d <= %d)", eattr.counter, data->counter);
1069                 data->counter_too_small = eattr.counter;
1070
1071                 /* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
1072                  * reauth_id must not be used to start a new reauthentication.
1073                  * However, since it was used in the last EAP-Response-Identity
1074                  * packet, it has to saved for the following fullauth to be
1075                  * used in MK derivation. */
1076                 os_free(data->last_eap_identity);
1077                 data->last_eap_identity = data->reauth_id;
1078                 data->last_eap_identity_len = data->reauth_id_len;
1079                 data->reauth_id = NULL;
1080                 data->reauth_id_len = 0;
1081
1082                 res = eap_aka_response_reauth(data, id, 1, eattr.nonce_s);
1083                 os_free(decrypted);
1084
1085                 return res;
1086         }
1087         data->counter = eattr.counter;
1088
1089         os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
1090         wpa_hexdump(MSG_DEBUG, "EAP-AKA: (encr) AT_NONCE_S",
1091                     data->nonce_s, EAP_SIM_NONCE_S_LEN);
1092
1093         if (data->eap_method == EAP_TYPE_AKA_PRIME) {
1094                 eap_aka_prime_derive_keys_reauth(data->k_re, data->counter,
1095                                                  data->reauth_id,
1096                                                  data->reauth_id_len,
1097                                                  data->nonce_s,
1098                                                  data->msk, data->emsk);
1099         } else {
1100                 eap_sim_derive_keys_reauth(data->counter, data->reauth_id,
1101                                            data->reauth_id_len,
1102                                            data->nonce_s, data->mk,
1103                                            data->msk, data->emsk);
1104         }
1105         eap_aka_clear_identities(data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1106         eap_aka_learn_ids(data, &eattr);
1107
1108         if (data->result_ind && attr->result_ind)
1109                 data->use_result_ind = 1;
1110
1111         if (data->state != FAILURE && data->state != RESULT_FAILURE) {
1112                 eap_aka_state(data, data->use_result_ind ?
1113                               RESULT_SUCCESS : SUCCESS);
1114         }
1115
1116         data->num_id_req = 0;
1117         data->num_notification = 0;
1118         if (data->counter > EAP_AKA_MAX_FAST_REAUTHS) {
1119                 wpa_printf(MSG_DEBUG, "EAP-AKA: Maximum number of "
1120                            "fast reauths performed - force fullauth");
1121                 eap_aka_clear_identities(data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1122         }
1123         os_free(decrypted);
1124         return eap_aka_response_reauth(data, id, 0, data->nonce_s);
1125 }
1126
1127
1128 static struct wpabuf * eap_aka_process(struct eap_sm *sm, void *priv,
1129                                        struct eap_method_ret *ret,
1130                                        const struct wpabuf *reqData)
1131 {
1132         struct eap_aka_data *data = priv;
1133         const struct eap_hdr *req;
1134         u8 subtype, id;
1135         struct wpabuf *res;
1136         const u8 *pos;
1137         struct eap_sim_attrs attr;
1138         size_t len;
1139
1140         wpa_hexdump_buf(MSG_DEBUG, "EAP-AKA: EAP data", reqData);
1141         if (eap_get_config_identity(sm, &len) == NULL) {
1142                 wpa_printf(MSG_INFO, "EAP-AKA: Identity not configured");
1143                 eap_sm_request_identity(sm);
1144                 ret->ignore = TRUE;
1145                 return NULL;
1146         }
1147
1148         pos = eap_hdr_validate(EAP_VENDOR_IETF, data->eap_method, reqData,
1149                                &len);
1150         if (pos == NULL || len < 1) {
1151                 ret->ignore = TRUE;
1152                 return NULL;
1153         }
1154         req = wpabuf_head(reqData);
1155         id = req->identifier;
1156         len = be_to_host16(req->length);
1157
1158         ret->ignore = FALSE;
1159         ret->methodState = METHOD_MAY_CONT;
1160         ret->decision = DECISION_FAIL;
1161         ret->allowNotifications = TRUE;
1162
1163         subtype = *pos++;
1164         wpa_printf(MSG_DEBUG, "EAP-AKA: Subtype=%d", subtype);
1165         pos += 2; /* Reserved */
1166
1167         if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr,
1168                                data->eap_method == EAP_TYPE_AKA_PRIME ? 2 : 1,
1169                                0)) {
1170                 res = eap_aka_client_error(data, id,
1171                                            EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1172                 goto done;
1173         }
1174
1175         switch (subtype) {
1176         case EAP_AKA_SUBTYPE_IDENTITY:
1177                 res = eap_aka_process_identity(sm, data, id, reqData, &attr);
1178                 break;
1179         case EAP_AKA_SUBTYPE_CHALLENGE:
1180                 res = eap_aka_process_challenge(sm, data, id, reqData, &attr);
1181                 break;
1182         case EAP_AKA_SUBTYPE_NOTIFICATION:
1183                 res = eap_aka_process_notification(sm, data, id, reqData,
1184                                                    &attr);
1185                 break;
1186         case EAP_AKA_SUBTYPE_REAUTHENTICATION:
1187                 res = eap_aka_process_reauthentication(sm, data, id, reqData,
1188                                                        &attr);
1189                 break;
1190         case EAP_AKA_SUBTYPE_CLIENT_ERROR:
1191                 wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Client-Error");
1192                 res = eap_aka_client_error(data, id,
1193                                            EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1194                 break;
1195         default:
1196                 wpa_printf(MSG_DEBUG, "EAP-AKA: Unknown subtype=%d", subtype);
1197                 res = eap_aka_client_error(data, id,
1198                                            EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1199                 break;
1200         }
1201
1202 done:
1203         if (data->state == FAILURE) {
1204                 ret->decision = DECISION_FAIL;
1205                 ret->methodState = METHOD_DONE;
1206         } else if (data->state == SUCCESS) {
1207                 ret->decision = data->use_result_ind ?
1208                         DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
1209                 /*
1210                  * It is possible for the server to reply with AKA
1211                  * Notification, so we must allow the method to continue and
1212                  * not only accept EAP-Success at this point.
1213                  */
1214                 ret->methodState = data->use_result_ind ?
1215                         METHOD_DONE : METHOD_MAY_CONT;
1216         } else if (data->state == RESULT_FAILURE)
1217                 ret->methodState = METHOD_CONT;
1218         else if (data->state == RESULT_SUCCESS)
1219                 ret->methodState = METHOD_CONT;
1220
1221         if (ret->methodState == METHOD_DONE) {
1222                 ret->allowNotifications = FALSE;
1223         }
1224
1225         return res;
1226 }
1227
1228
1229 static Boolean eap_aka_has_reauth_data(struct eap_sm *sm, void *priv)
1230 {
1231         struct eap_aka_data *data = priv;
1232         return data->pseudonym || data->reauth_id;
1233 }
1234
1235
1236 static void eap_aka_deinit_for_reauth(struct eap_sm *sm, void *priv)
1237 {
1238         struct eap_aka_data *data = priv;
1239         eap_aka_clear_identities(data, CLEAR_EAP_ID);
1240         data->prev_id = -1;
1241         wpabuf_free(data->id_msgs);
1242         data->id_msgs = NULL;
1243         data->use_result_ind = 0;
1244 }
1245
1246
1247 static void * eap_aka_init_for_reauth(struct eap_sm *sm, void *priv)
1248 {
1249         struct eap_aka_data *data = priv;
1250         data->num_id_req = 0;
1251         data->num_notification = 0;
1252         eap_aka_state(data, CONTINUE);
1253         return priv;
1254 }
1255
1256
1257 static const u8 * eap_aka_get_identity(struct eap_sm *sm, void *priv,
1258                                        size_t *len)
1259 {
1260         struct eap_aka_data *data = priv;
1261
1262         if (data->reauth_id) {
1263                 *len = data->reauth_id_len;
1264                 return data->reauth_id;
1265         }
1266
1267         if (data->pseudonym) {
1268                 *len = data->pseudonym_len;
1269                 return data->pseudonym;
1270         }
1271
1272         return NULL;
1273 }
1274
1275
1276 static Boolean eap_aka_isKeyAvailable(struct eap_sm *sm, void *priv)
1277 {
1278         struct eap_aka_data *data = priv;
1279         return data->state == SUCCESS;
1280 }
1281
1282
1283 static u8 * eap_aka_getKey(struct eap_sm *sm, void *priv, size_t *len)
1284 {
1285         struct eap_aka_data *data = priv;
1286         u8 *key;
1287
1288         if (data->state != SUCCESS)
1289                 return NULL;
1290
1291         key = os_malloc(EAP_SIM_KEYING_DATA_LEN);
1292         if (key == NULL)
1293                 return NULL;
1294
1295         *len = EAP_SIM_KEYING_DATA_LEN;
1296         os_memcpy(key, data->msk, EAP_SIM_KEYING_DATA_LEN);
1297
1298         return key;
1299 }
1300
1301
1302 static u8 * eap_aka_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1303 {
1304         struct eap_aka_data *data = priv;
1305         u8 *key;
1306
1307         if (data->state != SUCCESS)
1308                 return NULL;
1309
1310         key = os_malloc(EAP_EMSK_LEN);
1311         if (key == NULL)
1312                 return NULL;
1313
1314         *len = EAP_EMSK_LEN;
1315         os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1316
1317         return key;
1318 }
1319
1320
1321 int eap_peer_aka_register(void)
1322 {
1323         struct eap_method *eap;
1324         int ret;
1325
1326         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1327                                     EAP_VENDOR_IETF, EAP_TYPE_AKA, "AKA");
1328         if (eap == NULL)
1329                 return -1;
1330
1331         eap->init = eap_aka_init;
1332         eap->deinit = eap_aka_deinit;
1333         eap->process = eap_aka_process;
1334         eap->isKeyAvailable = eap_aka_isKeyAvailable;
1335         eap->getKey = eap_aka_getKey;
1336         eap->has_reauth_data = eap_aka_has_reauth_data;
1337         eap->deinit_for_reauth = eap_aka_deinit_for_reauth;
1338         eap->init_for_reauth = eap_aka_init_for_reauth;
1339         eap->get_identity = eap_aka_get_identity;
1340         eap->get_emsk = eap_aka_get_emsk;
1341
1342         ret = eap_peer_method_register(eap);
1343         if (ret)
1344                 eap_peer_method_free(eap);
1345         return ret;
1346 }
1347
1348
1349 #ifdef EAP_AKA_PRIME
1350 int eap_peer_aka_prime_register(void)
1351 {
1352         struct eap_method *eap;
1353         int ret;
1354
1355         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1356                                     EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME,
1357                                     "AKA'");
1358         if (eap == NULL)
1359                 return -1;
1360
1361         eap->init = eap_aka_prime_init;
1362         eap->deinit = eap_aka_deinit;
1363         eap->process = eap_aka_process;
1364         eap->isKeyAvailable = eap_aka_isKeyAvailable;
1365         eap->getKey = eap_aka_getKey;
1366         eap->has_reauth_data = eap_aka_has_reauth_data;
1367         eap->deinit_for_reauth = eap_aka_deinit_for_reauth;
1368         eap->init_for_reauth = eap_aka_init_for_reauth;
1369         eap->get_identity = eap_aka_get_identity;
1370         eap->get_emsk = eap_aka_get_emsk;
1371
1372         ret = eap_peer_method_register(eap);
1373         if (ret)
1374                 eap_peer_method_free(eap);
1375
1376         return ret;
1377 }
1378 #endif /* EAP_AKA_PRIME */