LCOV - code coverage report
Current view: top level - src/eap_peer - eap_fast.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 652 836 78.0 %
Date: 2015-09-27 Functions: 43 44 97.7 %

          Line data    Source code
       1             : /*
       2             :  * EAP peer method: EAP-FAST (RFC 4851)
       3             :  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : 
      11             : #include "common.h"
      12             : #include "crypto/tls.h"
      13             : #include "crypto/sha1.h"
      14             : #include "eap_common/eap_tlv_common.h"
      15             : #include "eap_i.h"
      16             : #include "eap_tls_common.h"
      17             : #include "eap_config.h"
      18             : #include "eap_fast_pac.h"
      19             : 
      20             : #ifdef EAP_FAST_DYNAMIC
      21             : #include "eap_fast_pac.c"
      22             : #endif /* EAP_FAST_DYNAMIC */
      23             : 
      24             : /* TODO:
      25             :  * - test session resumption and enable it if it interoperates
      26             :  * - password change (pending mschapv2 packet; replay decrypted packet)
      27             :  */
      28             : 
      29             : 
      30             : static void eap_fast_deinit(struct eap_sm *sm, void *priv);
      31             : 
      32             : 
      33             : struct eap_fast_data {
      34             :         struct eap_ssl_data ssl;
      35             : 
      36             :         int fast_version;
      37             : 
      38             :         const struct eap_method *phase2_method;
      39             :         void *phase2_priv;
      40             :         int phase2_success;
      41             : 
      42             :         struct eap_method_type phase2_type;
      43             :         struct eap_method_type *phase2_types;
      44             :         size_t num_phase2_types;
      45             :         int resuming; /* starting a resumed session */
      46             :         struct eap_fast_key_block_provisioning *key_block_p;
      47             : #define EAP_FAST_PROV_UNAUTH 1
      48             : #define EAP_FAST_PROV_AUTH 2
      49             :         int provisioning_allowed; /* Allowed PAC provisioning modes */
      50             :         int provisioning; /* doing PAC provisioning (not the normal auth) */
      51             :         int anon_provisioning; /* doing anonymous (unauthenticated)
      52             :                                 * provisioning */
      53             :         int session_ticket_used;
      54             : 
      55             :         u8 key_data[EAP_FAST_KEY_LEN];
      56             :         u8 *session_id;
      57             :         size_t id_len;
      58             :         u8 emsk[EAP_EMSK_LEN];
      59             :         int success;
      60             : 
      61             :         struct eap_fast_pac *pac;
      62             :         struct eap_fast_pac *current_pac;
      63             :         size_t max_pac_list_len;
      64             :         int use_pac_binary_format;
      65             : 
      66             :         u8 simck[EAP_FAST_SIMCK_LEN];
      67             :         int simck_idx;
      68             : 
      69             :         struct wpabuf *pending_phase2_req;
      70             : };
      71             : 
      72             : 
      73          23 : static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
      74             :                                       const u8 *client_random,
      75             :                                       const u8 *server_random,
      76             :                                       u8 *master_secret)
      77             : {
      78          23 :         struct eap_fast_data *data = ctx;
      79             : 
      80          23 :         wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
      81             : 
      82          23 :         if (client_random == NULL || server_random == NULL ||
      83             :             master_secret == NULL) {
      84           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall "
      85             :                            "back to full TLS handshake");
      86           0 :                 data->session_ticket_used = 0;
      87           0 :                 if (data->provisioning_allowed) {
      88           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a "
      89             :                                    "new PAC-Key");
      90           0 :                         data->provisioning = 1;
      91           0 :                         data->current_pac = NULL;
      92             :                 }
      93           0 :                 return 0;
      94             :         }
      95             : 
      96          23 :         wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len);
      97             : 
      98          23 :         if (data->current_pac == NULL) {
      99          13 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for "
     100             :                            "using SessionTicket");
     101          13 :                 data->session_ticket_used = 0;
     102          13 :                 return 0;
     103             :         }
     104             : 
     105          10 :         eap_fast_derive_master_secret(data->current_pac->pac_key,
     106             :                                       server_random, client_random,
     107             :                                       master_secret);
     108             : 
     109          10 :         data->session_ticket_used = 1;
     110             : 
     111          10 :         return 1;
     112             : }
     113             : 
     114             : 
     115          23 : static int eap_fast_parse_phase1(struct eap_fast_data *data,
     116             :                                  const char *phase1)
     117             : {
     118             :         const char *pos;
     119             : 
     120          23 :         pos = os_strstr(phase1, "fast_provisioning=");
     121          23 :         if (pos) {
     122          22 :                 data->provisioning_allowed = atoi(pos + 18);
     123          22 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC provisioning "
     124             :                            "mode: %d", data->provisioning_allowed);
     125             :         }
     126             : 
     127          23 :         pos = os_strstr(phase1, "fast_max_pac_list_len=");
     128          23 :         if (pos) {
     129           3 :                 data->max_pac_list_len = atoi(pos + 22);
     130           3 :                 if (data->max_pac_list_len == 0)
     131           0 :                         data->max_pac_list_len = 1;
     132           3 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Maximum PAC list length: %lu",
     133             :                            (unsigned long) data->max_pac_list_len);
     134             :         }
     135             : 
     136          23 :         pos = os_strstr(phase1, "fast_pac_format=binary");
     137          23 :         if (pos) {
     138           6 :                 data->use_pac_binary_format = 1;
     139           6 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Using binary format for PAC "
     140             :                            "list");
     141             :         }
     142             : 
     143          23 :         return 0;
     144             : }
     145             : 
     146             : 
     147          26 : static void * eap_fast_init(struct eap_sm *sm)
     148             : {
     149             :         struct eap_fast_data *data;
     150          26 :         struct eap_peer_config *config = eap_get_config(sm);
     151             : 
     152          26 :         if (config == NULL)
     153           0 :                 return NULL;
     154             : 
     155          26 :         data = os_zalloc(sizeof(*data));
     156          26 :         if (data == NULL)
     157           0 :                 return NULL;
     158          26 :         data->fast_version = EAP_FAST_VERSION;
     159          26 :         data->max_pac_list_len = 10;
     160             : 
     161          26 :         if (config->phase1 && eap_fast_parse_phase1(data, config->phase1) < 0) {
     162           0 :                 eap_fast_deinit(sm, data);
     163           0 :                 return NULL;
     164             :         }
     165             : 
     166          26 :         if (eap_peer_select_phase2_methods(config, "auth=",
     167             :                                            &data->phase2_types,
     168             :                                            &data->num_phase2_types) < 0) {
     169           0 :                 eap_fast_deinit(sm, data);
     170           0 :                 return NULL;
     171             :         }
     172             : 
     173          26 :         data->phase2_type.vendor = EAP_VENDOR_IETF;
     174          26 :         data->phase2_type.method = EAP_TYPE_NONE;
     175             : 
     176          26 :         if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_FAST)) {
     177           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
     178           0 :                 eap_fast_deinit(sm, data);
     179           0 :                 return NULL;
     180             :         }
     181             : 
     182          26 :         if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
     183             :                                                  eap_fast_session_ticket_cb,
     184             :                                                  data) < 0) {
     185           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
     186             :                            "callback");
     187           0 :                 eap_fast_deinit(sm, data);
     188           0 :                 return NULL;
     189             :         }
     190             : 
     191             :         /*
     192             :          * The local RADIUS server in a Cisco AP does not seem to like empty
     193             :          * fragments before data, so disable that workaround for CBC.
     194             :          * TODO: consider making this configurable
     195             :          */
     196          26 :         if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
     197           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
     198             :                            "workarounds");
     199             :         }
     200             : 
     201          26 :         if (!config->pac_file) {
     202           1 :                 wpa_printf(MSG_INFO, "EAP-FAST: No PAC file configured");
     203           1 :                 eap_fast_deinit(sm, data);
     204           1 :                 return NULL;
     205             :         }
     206             : 
     207          31 :         if (data->use_pac_binary_format &&
     208           6 :             eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
     209           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
     210           0 :                 eap_fast_deinit(sm, data);
     211           0 :                 return NULL;
     212             :         }
     213             : 
     214          44 :         if (!data->use_pac_binary_format &&
     215          19 :             eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
     216           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
     217           0 :                 eap_fast_deinit(sm, data);
     218           0 :                 return NULL;
     219             :         }
     220          25 :         eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
     221             : 
     222          25 :         if (data->pac == NULL && !data->provisioning_allowed) {
     223           1 :                 wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
     224             :                            "provisioning disabled");
     225           1 :                 eap_fast_deinit(sm, data);
     226           1 :                 return NULL;
     227             :         }
     228             : 
     229          24 :         return data;
     230             : }
     231             : 
     232             : 
     233          26 : static void eap_fast_deinit(struct eap_sm *sm, void *priv)
     234             : {
     235          26 :         struct eap_fast_data *data = priv;
     236             :         struct eap_fast_pac *pac, *prev;
     237             : 
     238          26 :         if (data == NULL)
     239          26 :                 return;
     240          26 :         if (data->phase2_priv && data->phase2_method)
     241          22 :                 data->phase2_method->deinit(sm, data->phase2_priv);
     242          26 :         os_free(data->phase2_types);
     243          26 :         os_free(data->key_block_p);
     244          26 :         eap_peer_tls_ssl_deinit(sm, &data->ssl);
     245             : 
     246          26 :         pac = data->pac;
     247          26 :         prev = NULL;
     248          75 :         while (pac) {
     249          23 :                 prev = pac;
     250          23 :                 pac = pac->next;
     251          23 :                 eap_fast_free_pac(prev);
     252             :         }
     253          26 :         os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
     254          26 :         os_memset(data->emsk, 0, EAP_EMSK_LEN);
     255          26 :         os_free(data->session_id);
     256          26 :         wpabuf_free(data->pending_phase2_req);
     257          26 :         os_free(data);
     258             : }
     259             : 
     260             : 
     261          17 : static int eap_fast_derive_msk(struct eap_fast_data *data)
     262             : {
     263          17 :         eap_fast_derive_eap_msk(data->simck, data->key_data);
     264          17 :         eap_fast_derive_eap_emsk(data->simck, data->emsk);
     265          17 :         data->success = 1;
     266          17 :         return 0;
     267             : }
     268             : 
     269             : 
     270          18 : static int eap_fast_derive_key_auth(struct eap_sm *sm,
     271             :                                     struct eap_fast_data *data)
     272             : {
     273             :         u8 *sks;
     274             : 
     275             :         /* RFC 4851, Section 5.1:
     276             :          * Extra key material after TLS key_block: session_key_seed[40]
     277             :          */
     278             : 
     279          18 :         sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
     280             :                                   EAP_FAST_SKS_LEN);
     281          18 :         if (sks == NULL) {
     282           1 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
     283             :                            "session_key_seed");
     284           1 :                 return -1;
     285             :         }
     286             : 
     287             :         /*
     288             :          * RFC 4851, Section 5.2:
     289             :          * S-IMCK[0] = session_key_seed
     290             :          */
     291          17 :         wpa_hexdump_key(MSG_DEBUG,
     292             :                         "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
     293             :                         sks, EAP_FAST_SKS_LEN);
     294          17 :         data->simck_idx = 0;
     295          17 :         os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
     296          17 :         os_free(sks);
     297          17 :         return 0;
     298             : }
     299             : 
     300             : 
     301           5 : static int eap_fast_derive_key_provisioning(struct eap_sm *sm,
     302             :                                             struct eap_fast_data *data)
     303             : {
     304           5 :         os_free(data->key_block_p);
     305           5 :         data->key_block_p = (struct eap_fast_key_block_provisioning *)
     306           5 :                 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
     307             :                                     "key expansion",
     308             :                                     sizeof(*data->key_block_p));
     309           5 :         if (data->key_block_p == NULL) {
     310           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
     311           0 :                 return -1;
     312             :         }
     313             :         /*
     314             :          * RFC 4851, Section 5.2:
     315             :          * S-IMCK[0] = session_key_seed
     316             :          */
     317           5 :         wpa_hexdump_key(MSG_DEBUG,
     318             :                         "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
     319           5 :                         data->key_block_p->session_key_seed,
     320             :                         sizeof(data->key_block_p->session_key_seed));
     321           5 :         data->simck_idx = 0;
     322           5 :         os_memcpy(data->simck, data->key_block_p->session_key_seed,
     323             :                   EAP_FAST_SIMCK_LEN);
     324           5 :         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
     325           5 :                         data->key_block_p->server_challenge,
     326             :                         sizeof(data->key_block_p->server_challenge));
     327           5 :         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
     328           5 :                         data->key_block_p->client_challenge,
     329             :                         sizeof(data->key_block_p->client_challenge));
     330           5 :         return 0;
     331             : }
     332             : 
     333             : 
     334          23 : static int eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
     335             : {
     336             :         int res;
     337             : 
     338          23 :         if (data->anon_provisioning)
     339           5 :                 res = eap_fast_derive_key_provisioning(sm, data);
     340             :         else
     341          18 :                 res = eap_fast_derive_key_auth(sm, data);
     342          23 :         return res;
     343             : }
     344             : 
     345             : 
     346          23 : static int eap_fast_init_phase2_method(struct eap_sm *sm,
     347             :                                        struct eap_fast_data *data)
     348             : {
     349          23 :         data->phase2_method =
     350          23 :                 eap_peer_get_eap_method(data->phase2_type.vendor,
     351          23 :                                         data->phase2_type.method);
     352          23 :         if (data->phase2_method == NULL)
     353           0 :                 return -1;
     354             : 
     355          23 :         if (data->key_block_p) {
     356           5 :                 sm->auth_challenge = data->key_block_p->server_challenge;
     357           5 :                 sm->peer_challenge = data->key_block_p->client_challenge;
     358             :         }
     359          23 :         sm->init_phase2 = 1;
     360          23 :         data->phase2_priv = data->phase2_method->init(sm);
     361          23 :         sm->init_phase2 = 0;
     362          23 :         sm->auth_challenge = NULL;
     363          23 :         sm->peer_challenge = NULL;
     364             : 
     365          23 :         return data->phase2_priv == NULL ? -1 : 0;
     366             : }
     367             : 
     368             : 
     369          29 : static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
     370             : {
     371             :         size_t i;
     372             : 
     373             :         /* TODO: TNC with anonymous provisioning; need to require both
     374             :          * completed MSCHAPv2 and TNC */
     375             : 
     376          29 :         if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
     377           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
     378             :                            "during unauthenticated provisioning; reject phase2"
     379             :                            " type %d", type);
     380           0 :                 return -1;
     381             :         }
     382             : 
     383             : #ifdef EAP_TNC
     384          29 :         if (type == EAP_TYPE_TNC) {
     385           1 :                 data->phase2_type.vendor = EAP_VENDOR_IETF;
     386           1 :                 data->phase2_type.method = EAP_TYPE_TNC;
     387           1 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
     388             :                            "vendor %d method %d for TNC",
     389             :                            data->phase2_type.vendor,
     390             :                            data->phase2_type.method);
     391           1 :                 return 0;
     392             :         }
     393             : #endif /* EAP_TNC */
     394             : 
     395          68 :         for (i = 0; i < data->num_phase2_types; i++) {
     396          56 :                 if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
     397          28 :                     data->phase2_types[i].method != type)
     398           6 :                         continue;
     399             : 
     400          22 :                 data->phase2_type.vendor = data->phase2_types[i].vendor;
     401          22 :                 data->phase2_type.method = data->phase2_types[i].method;
     402          22 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
     403             :                            "vendor %d method %d",
     404             :                            data->phase2_type.vendor,
     405             :                            data->phase2_type.method);
     406          22 :                 break;
     407             :         }
     408             : 
     409          28 :         if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
     410           6 :                 return -1;
     411             : 
     412          22 :         return 0;
     413             : }
     414             : 
     415             : 
     416          58 : static int eap_fast_phase2_request(struct eap_sm *sm,
     417             :                                    struct eap_fast_data *data,
     418             :                                    struct eap_method_ret *ret,
     419             :                                    struct eap_hdr *hdr,
     420             :                                    struct wpabuf **resp)
     421             : {
     422          58 :         size_t len = be_to_host16(hdr->length);
     423             :         u8 *pos;
     424             :         struct eap_method_ret iret;
     425          58 :         struct eap_peer_config *config = eap_get_config(sm);
     426             :         struct wpabuf msg;
     427             : 
     428          58 :         if (len <= sizeof(struct eap_hdr)) {
     429           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: too short "
     430             :                            "Phase 2 request (len=%lu)", (unsigned long) len);
     431           0 :                 return -1;
     432             :         }
     433          58 :         pos = (u8 *) (hdr + 1);
     434          58 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
     435          58 :         if (*pos == EAP_TYPE_IDENTITY) {
     436          12 :                 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
     437          12 :                 return 0;
     438             :         }
     439             : 
     440          64 :         if (data->phase2_priv && data->phase2_method &&
     441          18 :             *pos != data->phase2_type.method) {
     442           1 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
     443             :                            "deinitialize previous method");
     444           1 :                 data->phase2_method->deinit(sm, data->phase2_priv);
     445           1 :                 data->phase2_method = NULL;
     446           1 :                 data->phase2_priv = NULL;
     447           1 :                 data->phase2_type.vendor = EAP_VENDOR_IETF;
     448           1 :                 data->phase2_type.method = EAP_TYPE_NONE;
     449             :         }
     450             : 
     451          92 :         if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
     452          75 :             data->phase2_type.method == EAP_TYPE_NONE &&
     453          29 :             eap_fast_select_phase2_method(data, *pos) < 0) {
     454           6 :                 if (eap_peer_tls_phase2_nak(data->phase2_types,
     455             :                                             data->num_phase2_types,
     456             :                                             hdr, resp))
     457           0 :                         return -1;
     458           6 :                 return 0;
     459             :         }
     460             : 
     461          63 :         if ((data->phase2_priv == NULL &&
     462          63 :              eap_fast_init_phase2_method(sm, data) < 0) ||
     463          40 :             data->phase2_method == NULL) {
     464           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
     465           0 :                            "Phase 2 EAP method %d", *pos);
     466           0 :                 ret->methodState = METHOD_DONE;
     467           0 :                 ret->decision = DECISION_FAIL;
     468           0 :                 return -1;
     469             :         }
     470             : 
     471          40 :         os_memset(&iret, 0, sizeof(iret));
     472          40 :         wpabuf_set(&msg, hdr, len);
     473          40 :         *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
     474             :                                              &msg);
     475          80 :         if (*resp == NULL ||
     476          54 :             (iret.methodState == METHOD_DONE &&
     477          14 :              iret.decision == DECISION_FAIL)) {
     478           0 :                 ret->methodState = METHOD_DONE;
     479           0 :                 ret->decision = DECISION_FAIL;
     480          66 :         } else if ((iret.methodState == METHOD_DONE ||
     481          66 :                     iret.methodState == METHOD_MAY_CONT) &&
     482          65 :                    (iret.decision == DECISION_UNCOND_SUCC ||
     483          25 :                     iret.decision == DECISION_COND_SUCC)) {
     484          23 :                 data->phase2_success = 1;
     485             :         }
     486             : 
     487          40 :         if (*resp == NULL && config &&
     488           0 :             (config->pending_req_identity || config->pending_req_password ||
     489           0 :              config->pending_req_otp || config->pending_req_new_password)) {
     490           0 :                 wpabuf_free(data->pending_phase2_req);
     491           0 :                 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
     492          40 :         } else if (*resp == NULL)
     493           0 :                 return -1;
     494             : 
     495          40 :         return 0;
     496             : }
     497             : 
     498             : 
     499           0 : static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
     500             : {
     501             :         struct wpabuf *buf;
     502             :         struct eap_tlv_nak_tlv *nak;
     503           0 :         buf = wpabuf_alloc(sizeof(*nak));
     504           0 :         if (buf == NULL)
     505           0 :                 return NULL;
     506           0 :         nak = wpabuf_put(buf, sizeof(*nak));
     507           0 :         nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
     508           0 :         nak->length = host_to_be16(6);
     509           0 :         nak->vendor_id = host_to_be32(vendor_id);
     510           0 :         nak->nak_type = host_to_be16(tlv_type);
     511           0 :         return buf;
     512             : }
     513             : 
     514             : 
     515          35 : static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
     516             : {
     517             :         struct wpabuf *buf;
     518             :         struct eap_tlv_intermediate_result_tlv *result;
     519          35 :         buf = wpabuf_alloc(sizeof(*result));
     520          35 :         if (buf == NULL)
     521           0 :                 return NULL;
     522          35 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
     523             :                    intermediate ? "Intermediate " : "", status);
     524          35 :         result = wpabuf_put(buf, sizeof(*result));
     525          35 :         result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
     526             :                                         (intermediate ?
     527             :                                          EAP_TLV_INTERMEDIATE_RESULT_TLV :
     528             :                                          EAP_TLV_RESULT_TLV));
     529          35 :         result->length = host_to_be16(2);
     530          35 :         result->status = host_to_be16(status);
     531          35 :         return buf;
     532             : }
     533             : 
     534             : 
     535          12 : static struct wpabuf * eap_fast_tlv_pac_ack(void)
     536             : {
     537             :         struct wpabuf *buf;
     538             :         struct eap_tlv_result_tlv *res;
     539             :         struct eap_tlv_pac_ack_tlv *ack;
     540             : 
     541          12 :         buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
     542          12 :         if (buf == NULL)
     543           0 :                 return NULL;
     544             : 
     545          12 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
     546          12 :         ack = wpabuf_put(buf, sizeof(*ack));
     547          12 :         ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
     548             :                                      EAP_TLV_TYPE_MANDATORY);
     549          12 :         ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
     550          12 :         ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
     551          12 :         ack->pac_len = host_to_be16(2);
     552          12 :         ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
     553             : 
     554          12 :         return buf;
     555             : }
     556             : 
     557             : 
     558          58 : static struct wpabuf * eap_fast_process_eap_payload_tlv(
     559             :         struct eap_sm *sm, struct eap_fast_data *data,
     560             :         struct eap_method_ret *ret,
     561             :         u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
     562             : {
     563             :         struct eap_hdr *hdr;
     564          58 :         struct wpabuf *resp = NULL;
     565             : 
     566          58 :         if (eap_payload_tlv_len < sizeof(*hdr)) {
     567           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
     568             :                            "Payload TLV (len=%lu)",
     569             :                            (unsigned long) eap_payload_tlv_len);
     570           0 :                 return NULL;
     571             :         }
     572             : 
     573          58 :         hdr = (struct eap_hdr *) eap_payload_tlv;
     574          58 :         if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
     575           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
     576             :                            "EAP Payload TLV");
     577           0 :                 return NULL;
     578             :         }
     579             : 
     580          58 :         if (hdr->code != EAP_CODE_REQUEST) {
     581           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
     582           0 :                            "Phase 2 EAP header", hdr->code);
     583           0 :                 return NULL;
     584             :         }
     585             : 
     586          58 :         if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
     587           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
     588             :                            "failed");
     589           0 :                 return NULL;
     590             :         }
     591             : 
     592          58 :         return eap_fast_tlv_eap_payload(resp);
     593             : }
     594             : 
     595             : 
     596          22 : static int eap_fast_validate_crypto_binding(
     597             :         struct eap_tlv_crypto_binding_tlv *_bind)
     598             : {
     599          66 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
     600             :                    "Received Version %d SubType %d",
     601          66 :                    _bind->version, _bind->received_version, _bind->subtype);
     602          22 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
     603          22 :                     _bind->nonce, sizeof(_bind->nonce));
     604          22 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
     605          22 :                     _bind->compound_mac, sizeof(_bind->compound_mac));
     606             : 
     607          44 :         if (_bind->version != EAP_FAST_VERSION ||
     608          44 :             _bind->received_version != EAP_FAST_VERSION ||
     609          22 :             _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
     610           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
     611             :                            "Crypto-Binding TLV: Version %d "
     612             :                            "Received Version %d SubType %d",
     613           0 :                            _bind->version, _bind->received_version,
     614           0 :                            _bind->subtype);
     615           0 :                 return -1;
     616             :         }
     617             : 
     618          22 :         return 0;
     619             : }
     620             : 
     621             : 
     622          22 : static void eap_fast_write_crypto_binding(
     623             :         struct eap_tlv_crypto_binding_tlv *rbind,
     624             :         struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
     625             : {
     626          22 :         rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
     627             :                                        EAP_TLV_CRYPTO_BINDING_TLV);
     628          22 :         rbind->length = host_to_be16(sizeof(*rbind) -
     629             :                                      sizeof(struct eap_tlv_hdr));
     630          22 :         rbind->version = EAP_FAST_VERSION;
     631          22 :         rbind->received_version = _bind->version;
     632          22 :         rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
     633          22 :         os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
     634          22 :         inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
     635          22 :         hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
     636          22 :                   rbind->compound_mac);
     637             : 
     638          66 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
     639             :                    "Received Version %d SubType %d",
     640          66 :                    rbind->version, rbind->received_version, rbind->subtype);
     641          22 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
     642          22 :                     rbind->nonce, sizeof(rbind->nonce));
     643          22 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
     644          22 :                     rbind->compound_mac, sizeof(rbind->compound_mac));
     645          22 : }
     646             : 
     647             : 
     648          22 : static int eap_fast_get_phase2_key(struct eap_sm *sm,
     649             :                                    struct eap_fast_data *data,
     650             :                                    u8 *isk, size_t isk_len)
     651             : {
     652             :         u8 *key;
     653             :         size_t key_len;
     654             : 
     655          22 :         os_memset(isk, 0, isk_len);
     656             : 
     657          22 :         if (data->phase2_method == NULL || data->phase2_priv == NULL) {
     658           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
     659             :                            "available");
     660           0 :                 return -1;
     661             :         }
     662             : 
     663          37 :         if (data->phase2_method->isKeyAvailable == NULL ||
     664          15 :             data->phase2_method->getKey == NULL)
     665           7 :                 return 0;
     666             : 
     667          30 :         if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
     668          15 :             (key = data->phase2_method->getKey(sm, data->phase2_priv,
     669             :                                                &key_len)) == NULL) {
     670           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
     671             :                            "from Phase 2");
     672           0 :                 return -1;
     673             :         }
     674             : 
     675          15 :         if (key_len > isk_len)
     676           1 :                 key_len = isk_len;
     677          30 :         if (key_len == 32 &&
     678          30 :             data->phase2_method->vendor == EAP_VENDOR_IETF &&
     679          15 :             data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
     680             :                 /*
     681             :                  * EAP-FAST uses reverse order for MS-MPPE keys when deriving
     682             :                  * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
     683             :                  * ISK for EAP-FAST cryptobinding.
     684             :                  */
     685          14 :                 os_memcpy(isk, key + 16, 16);
     686          14 :                 os_memcpy(isk + 16, key, 16);
     687             :         } else
     688           1 :                 os_memcpy(isk, key, key_len);
     689          15 :         os_free(key);
     690             : 
     691          15 :         return 0;
     692             : }
     693             : 
     694             : 
     695          22 : static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
     696             :                             u8 *cmk)
     697             : {
     698             :         u8 isk[32], imck[60];
     699             : 
     700          22 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
     701          22 :                    "calculation", data->simck_idx + 1);
     702             : 
     703             :         /*
     704             :          * RFC 4851, Section 5.2:
     705             :          * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
     706             :          *                 MSK[j], 60)
     707             :          * S-IMCK[j] = first 40 octets of IMCK[j]
     708             :          * CMK[j] = last 20 octets of IMCK[j]
     709             :          */
     710             : 
     711          22 :         if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
     712           0 :                 return -1;
     713          22 :         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
     714          22 :         sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
     715             :                    "Inner Methods Compound Keys",
     716             :                    isk, sizeof(isk), imck, sizeof(imck));
     717          22 :         data->simck_idx++;
     718          22 :         os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
     719          22 :         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
     720          22 :                         data->simck, EAP_FAST_SIMCK_LEN);
     721          22 :         os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
     722          22 :         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
     723             :                         cmk, EAP_FAST_CMK_LEN);
     724             : 
     725          22 :         return 0;
     726             : }
     727             : 
     728             : 
     729           8 : static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
     730             : {
     731             :         struct eap_tlv_hdr *pac;
     732             :         struct eap_tlv_request_action_tlv *act;
     733             :         struct eap_tlv_pac_type_tlv *type;
     734             : 
     735           8 :         act = (struct eap_tlv_request_action_tlv *) pos;
     736           8 :         act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
     737           8 :         act->length = host_to_be16(2);
     738           8 :         act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
     739             : 
     740           8 :         pac = (struct eap_tlv_hdr *) (act + 1);
     741           8 :         pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
     742           8 :         pac->length = host_to_be16(sizeof(*type));
     743             : 
     744           8 :         type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
     745           8 :         type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
     746           8 :         type->length = host_to_be16(2);
     747           8 :         type->pac_type = host_to_be16(pac_type);
     748             : 
     749           8 :         return (u8 *) (type + 1);
     750             : }
     751             : 
     752             : 
     753          22 : static struct wpabuf * eap_fast_process_crypto_binding(
     754             :         struct eap_sm *sm, struct eap_fast_data *data,
     755             :         struct eap_method_ret *ret,
     756             :         struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
     757             : {
     758             :         struct wpabuf *resp;
     759             :         u8 *pos;
     760             :         u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
     761             :         int res;
     762             :         size_t len;
     763             : 
     764          22 :         if (eap_fast_validate_crypto_binding(_bind) < 0)
     765           0 :                 return NULL;
     766             : 
     767          22 :         if (eap_fast_get_cmk(sm, data, cmk) < 0)
     768           0 :                 return NULL;
     769             : 
     770             :         /* Validate received Compound MAC */
     771          22 :         os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
     772          22 :         os_memset(_bind->compound_mac, 0, sizeof(cmac));
     773          22 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
     774             :                     "MAC calculation", (u8 *) _bind, bind_len);
     775          22 :         hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
     776          22 :                   _bind->compound_mac);
     777          22 :         res = os_memcmp_const(cmac, _bind->compound_mac, sizeof(cmac));
     778          22 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
     779             :                     cmac, sizeof(cmac));
     780          22 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
     781          22 :                     _bind->compound_mac, sizeof(cmac));
     782          22 :         if (res != 0) {
     783           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
     784           0 :                 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
     785           0 :                 return NULL;
     786             :         }
     787             : 
     788             :         /*
     789             :          * Compound MAC was valid, so authentication succeeded. Reply with
     790             :          * crypto binding to allow server to complete authentication.
     791             :          */
     792             : 
     793          22 :         len = sizeof(struct eap_tlv_crypto_binding_tlv);
     794          22 :         resp = wpabuf_alloc(len);
     795          22 :         if (resp == NULL)
     796           0 :                 return NULL;
     797             : 
     798          39 :         if (!data->anon_provisioning && data->phase2_success &&
     799          17 :             eap_fast_derive_msk(data) < 0) {
     800           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
     801           0 :                 ret->methodState = METHOD_DONE;
     802           0 :                 ret->decision = DECISION_FAIL;
     803           0 :                 data->phase2_success = 0;
     804           0 :                 wpabuf_free(resp);
     805           0 :                 return NULL;
     806             :         }
     807             : 
     808          22 :         if (!data->anon_provisioning && data->phase2_success) {
     809          17 :                 os_free(data->session_id);
     810          17 :                 data->session_id = eap_peer_tls_derive_session_id(
     811             :                         sm, &data->ssl, EAP_TYPE_FAST, &data->id_len);
     812          17 :                 if (data->session_id) {
     813          34 :                         wpa_hexdump(MSG_DEBUG, "EAP-FAST: Derived Session-Id",
     814          17 :                                     data->session_id, data->id_len);
     815             :                 } else {
     816           0 :                         wpa_printf(MSG_ERROR, "EAP-FAST: Failed to derive "
     817             :                                    "Session-Id");
     818           0 :                         wpabuf_free(resp);
     819           0 :                         return NULL;
     820             :                 }
     821             :         }
     822             : 
     823          22 :         pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
     824          22 :         eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
     825             :                                       pos, _bind, cmk);
     826             : 
     827          22 :         return resp;
     828             : }
     829             : 
     830             : 
     831          36 : static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
     832             :                                    u8 *pos, size_t len, int *pac_key_found)
     833             : {
     834          36 :         switch (type & 0x7fff) {
     835             :         case PAC_TYPE_PAC_KEY:
     836          12 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
     837          12 :                 if (len != EAP_FAST_PAC_KEY_LEN) {
     838           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
     839             :                                    "length %lu", (unsigned long) len);
     840           0 :                         break;
     841             :                 }
     842          12 :                 *pac_key_found = 1;
     843          12 :                 os_memcpy(entry->pac_key, pos, len);
     844          12 :                 break;
     845             :         case PAC_TYPE_PAC_OPAQUE:
     846          12 :                 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
     847          12 :                 entry->pac_opaque = pos;
     848          12 :                 entry->pac_opaque_len = len;
     849          12 :                 break;
     850             :         case PAC_TYPE_PAC_INFO:
     851          12 :                 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
     852          12 :                 entry->pac_info = pos;
     853          12 :                 entry->pac_info_len = len;
     854          12 :                 break;
     855             :         default:
     856           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
     857             :                            type);
     858           0 :                 break;
     859             :         }
     860          36 : }
     861             : 
     862             : 
     863          12 : static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
     864             :                                     u8 *pac, size_t pac_len)
     865             : {
     866             :         struct pac_tlv_hdr *hdr;
     867             :         u8 *pos;
     868             :         size_t left, len;
     869          12 :         int type, pac_key_found = 0;
     870             : 
     871          12 :         pos = pac;
     872          12 :         left = pac_len;
     873             : 
     874          60 :         while (left > sizeof(*hdr)) {
     875          36 :                 hdr = (struct pac_tlv_hdr *) pos;
     876          36 :                 type = be_to_host16(hdr->type);
     877          36 :                 len = be_to_host16(hdr->len);
     878          36 :                 pos += sizeof(*hdr);
     879          36 :                 left -= sizeof(*hdr);
     880          36 :                 if (len > left) {
     881           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
     882             :                                    "(type=%d len=%lu left=%lu)",
     883             :                                    type, (unsigned long) len,
     884             :                                    (unsigned long) left);
     885           0 :                         return -1;
     886             :                 }
     887             : 
     888          36 :                 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
     889             : 
     890          36 :                 pos += len;
     891          36 :                 left -= len;
     892             :         }
     893             : 
     894          12 :         if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
     895           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
     896             :                            "all the required fields");
     897           0 :                 return -1;
     898             :         }
     899             : 
     900          12 :         return 0;
     901             : }
     902             : 
     903             : 
     904          60 : static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
     905             :                                    u8 *pos, size_t len)
     906             : {
     907             :         u16 pac_type;
     908             :         u32 lifetime;
     909             :         struct os_time now;
     910             : 
     911          60 :         switch (type & 0x7fff) {
     912             :         case PAC_TYPE_CRED_LIFETIME:
     913          12 :                 if (len != 4) {
     914           0 :                         wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
     915             :                                     "Invalid CRED_LIFETIME length - ignored",
     916             :                                     pos, len);
     917           0 :                         return 0;
     918             :                 }
     919             : 
     920             :                 /*
     921             :                  * This is not currently saved separately in PAC files since
     922             :                  * the server can automatically initiate PAC update when
     923             :                  * needed. Anyway, the information is available from PAC-Info
     924             :                  * dump if it is needed for something in the future.
     925             :                  */
     926          12 :                 lifetime = WPA_GET_BE32(pos);
     927          12 :                 os_get_time(&now);
     928          12 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
     929             :                            "(%d days)",
     930          12 :                            lifetime, (lifetime - (u32) now.sec) / 86400);
     931          12 :                 break;
     932             :         case PAC_TYPE_A_ID:
     933          12 :                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
     934             :                                   pos, len);
     935          12 :                 entry->a_id = pos;
     936          12 :                 entry->a_id_len = len;
     937          12 :                 break;
     938             :         case PAC_TYPE_I_ID:
     939          12 :                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
     940             :                                   pos, len);
     941          12 :                 entry->i_id = pos;
     942          12 :                 entry->i_id_len = len;
     943          12 :                 break;
     944             :         case PAC_TYPE_A_ID_INFO:
     945          12 :                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
     946             :                                   pos, len);
     947          12 :                 entry->a_id_info = pos;
     948          12 :                 entry->a_id_info_len = len;
     949          12 :                 break;
     950             :         case PAC_TYPE_PAC_TYPE:
     951             :                 /* RFC 5422, Section 4.2.6 - PAC-Type TLV */
     952          12 :                 if (len != 2) {
     953           0 :                         wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
     954             :                                    "length %lu (expected 2)",
     955             :                                    (unsigned long) len);
     956           0 :                         wpa_hexdump_ascii(MSG_DEBUG,
     957             :                                           "EAP-FAST: PAC-Info - PAC-Type",
     958             :                                           pos, len);
     959           0 :                         return -1;
     960             :                 }
     961          12 :                 pac_type = WPA_GET_BE16(pos);
     962          12 :                 if (pac_type != PAC_TYPE_TUNNEL_PAC &&
     963           0 :                     pac_type != PAC_TYPE_USER_AUTHORIZATION &&
     964             :                     pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
     965           0 :                         wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
     966             :                                    "%d", pac_type);
     967           0 :                         return -1;
     968             :                 }
     969             : 
     970          12 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
     971             :                            pac_type);
     972          12 :                 entry->pac_type = pac_type;
     973          12 :                 break;
     974             :         default:
     975           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
     976             :                            "type %d", type);
     977           0 :                 break;
     978             :         }
     979             : 
     980          60 :         return 0;
     981             : }
     982             : 
     983             : 
     984          12 : static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
     985             : {
     986             :         struct pac_tlv_hdr *hdr;
     987             :         u8 *pos;
     988             :         size_t left, len;
     989             :         int type;
     990             : 
     991             :         /* RFC 5422, Section 4.2.4 */
     992             : 
     993             :         /* PAC-Type defaults to Tunnel PAC (Type 1) */
     994          12 :         entry->pac_type = PAC_TYPE_TUNNEL_PAC;
     995             : 
     996          12 :         pos = entry->pac_info;
     997          12 :         left = entry->pac_info_len;
     998          84 :         while (left > sizeof(*hdr)) {
     999          60 :                 hdr = (struct pac_tlv_hdr *) pos;
    1000          60 :                 type = be_to_host16(hdr->type);
    1001          60 :                 len = be_to_host16(hdr->len);
    1002          60 :                 pos += sizeof(*hdr);
    1003          60 :                 left -= sizeof(*hdr);
    1004          60 :                 if (len > left) {
    1005           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
    1006             :                                    "(type=%d len=%lu left=%lu)",
    1007             :                                    type, (unsigned long) len,
    1008             :                                    (unsigned long) left);
    1009           0 :                         return -1;
    1010             :                 }
    1011             : 
    1012          60 :                 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
    1013           0 :                         return -1;
    1014             : 
    1015          60 :                 pos += len;
    1016          60 :                 left -= len;
    1017             :         }
    1018             : 
    1019          12 :         if (entry->a_id == NULL || entry->a_id_info == NULL) {
    1020           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
    1021             :                            "all the required fields");
    1022           0 :                 return -1;
    1023             :         }
    1024             : 
    1025          12 :         return 0;
    1026             : }
    1027             : 
    1028             : 
    1029          12 : static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
    1030             :                                             struct eap_fast_data *data,
    1031             :                                             struct eap_method_ret *ret,
    1032             :                                             u8 *pac, size_t pac_len)
    1033             : {
    1034          12 :         struct eap_peer_config *config = eap_get_config(sm);
    1035             :         struct eap_fast_pac entry;
    1036             : 
    1037          12 :         os_memset(&entry, 0, sizeof(entry));
    1038          24 :         if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
    1039          12 :             eap_fast_process_pac_info(&entry))
    1040           0 :                 return NULL;
    1041             : 
    1042          12 :         eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
    1043          12 :         eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
    1044          12 :         if (data->use_pac_binary_format)
    1045           2 :                 eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
    1046             :         else
    1047          10 :                 eap_fast_save_pac(sm, data->pac, config->pac_file);
    1048             : 
    1049          12 :         if (data->provisioning) {
    1050          12 :                 if (data->anon_provisioning) {
    1051             :                         /*
    1052             :                          * Unauthenticated provisioning does not provide keying
    1053             :                          * material and must end with an EAP-Failure.
    1054             :                          * Authentication will be done separately after this.
    1055             :                          */
    1056           5 :                         data->success = 0;
    1057           5 :                         ret->decision = DECISION_FAIL;
    1058             :                 } else {
    1059             :                         /*
    1060             :                          * Server may or may not allow authenticated
    1061             :                          * provisioning also for key generation.
    1062             :                          */
    1063           7 :                         ret->decision = DECISION_COND_SUCC;
    1064             :                 }
    1065          12 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
    1066             :                            "- Provisioning completed successfully");
    1067          12 :                 sm->expected_failure = 1;
    1068             :         } else {
    1069             :                 /*
    1070             :                  * This is PAC refreshing, i.e., normal authentication that is
    1071             :                  * expected to be completed with an EAP-Success. However,
    1072             :                  * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
    1073             :                  * after protected success exchange in case of EAP-Fast
    1074             :                  * provisioning, so we better use DECISION_COND_SUCC here
    1075             :                  * instead of DECISION_UNCOND_SUCC.
    1076             :                  */
    1077           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
    1078             :                            "- PAC refreshing completed successfully");
    1079           0 :                 ret->decision = DECISION_COND_SUCC;
    1080             :         }
    1081          12 :         ret->methodState = METHOD_DONE;
    1082          12 :         return eap_fast_tlv_pac_ack();
    1083             : }
    1084             : 
    1085             : 
    1086          91 : static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
    1087             :                                     struct eap_fast_tlv_parse *tlv,
    1088             :                                     struct wpabuf **resp)
    1089             : {
    1090             :         int mandatory, tlv_type, res;
    1091             :         size_t len;
    1092             :         u8 *pos, *end;
    1093             : 
    1094          91 :         os_memset(tlv, 0, sizeof(*tlv));
    1095             : 
    1096             :         /* Parse TLVs from the decrypted Phase 2 data */
    1097          91 :         pos = wpabuf_mhead(decrypted);
    1098          91 :         end = pos + wpabuf_len(decrypted);
    1099         309 :         while (pos + 4 < end) {
    1100         127 :                 mandatory = pos[0] & 0x80;
    1101         127 :                 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
    1102         127 :                 pos += 2;
    1103         127 :                 len = WPA_GET_BE16(pos);
    1104         127 :                 pos += 2;
    1105         127 :                 if (len > (size_t) (end - pos)) {
    1106           0 :                         wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
    1107           0 :                         return -1;
    1108             :                 }
    1109         127 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
    1110             :                            "TLV type %d length %u%s",
    1111             :                            tlv_type, (unsigned int) len,
    1112             :                            mandatory ? " (mandatory)" : "");
    1113             : 
    1114         127 :                 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
    1115         127 :                 if (res == -2)
    1116           0 :                         break;
    1117         127 :                 if (res < 0) {
    1118           0 :                         if (mandatory) {
    1119           0 :                                 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
    1120             :                                            "mandatory TLV type %d", tlv_type);
    1121           0 :                                 *resp = eap_fast_tlv_nak(0, tlv_type);
    1122           0 :                                 break;
    1123             :                         } else {
    1124           0 :                                 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
    1125             :                                            "unknown optional TLV type %d",
    1126             :                                            tlv_type);
    1127             :                         }
    1128             :                 }
    1129             : 
    1130         127 :                 pos += len;
    1131             :         }
    1132             : 
    1133          91 :         return 0;
    1134             : }
    1135             : 
    1136             : 
    1137          91 : static int eap_fast_encrypt_response(struct eap_sm *sm,
    1138             :                                      struct eap_fast_data *data,
    1139             :                                      struct wpabuf *resp,
    1140             :                                      u8 identifier, struct wpabuf **out_data)
    1141             : {
    1142          91 :         if (resp == NULL)
    1143           0 :                 return 0;
    1144             : 
    1145          91 :         wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
    1146             :                         resp);
    1147          91 :         if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
    1148             :                                  data->fast_version, identifier,
    1149             :                                  resp, out_data)) {
    1150           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
    1151             :                            "frame");
    1152             :         }
    1153          91 :         wpabuf_free(resp);
    1154             : 
    1155          91 :         return 0;
    1156             : }
    1157             : 
    1158             : 
    1159           8 : static struct wpabuf * eap_fast_pac_request(void)
    1160             : {
    1161             :         struct wpabuf *tmp;
    1162             :         u8 *pos, *pos2;
    1163             : 
    1164           8 :         tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
    1165             :                            sizeof(struct eap_tlv_request_action_tlv) +
    1166             :                            sizeof(struct eap_tlv_pac_type_tlv));
    1167           8 :         if (tmp == NULL)
    1168           0 :                 return NULL;
    1169             : 
    1170           8 :         pos = wpabuf_put(tmp, 0);
    1171           8 :         pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
    1172           8 :         wpabuf_put(tmp, pos2 - pos);
    1173           8 :         return tmp;
    1174             : }
    1175             : 
    1176             : 
    1177          91 : static int eap_fast_process_decrypted(struct eap_sm *sm,
    1178             :                                       struct eap_fast_data *data,
    1179             :                                       struct eap_method_ret *ret,
    1180             :                                       u8 identifier,
    1181             :                                       struct wpabuf *decrypted,
    1182             :                                       struct wpabuf **out_data)
    1183             : {
    1184          91 :         struct wpabuf *resp = NULL, *tmp;
    1185             :         struct eap_fast_tlv_parse tlv;
    1186          91 :         int failed = 0;
    1187             : 
    1188          91 :         if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
    1189           0 :                 return 0;
    1190          91 :         if (resp)
    1191           0 :                 return eap_fast_encrypt_response(sm, data, resp,
    1192             :                                                  identifier, out_data);
    1193             : 
    1194          91 :         if (tlv.result == EAP_TLV_RESULT_FAILURE) {
    1195           0 :                 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
    1196           0 :                 return eap_fast_encrypt_response(sm, data, resp,
    1197             :                                                  identifier, out_data);
    1198             :         }
    1199             : 
    1200          91 :         if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
    1201           0 :                 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
    1202           0 :                 return eap_fast_encrypt_response(sm, data, resp,
    1203             :                                                  identifier, out_data);
    1204             :         }
    1205             : 
    1206          91 :         if (tlv.crypto_binding) {
    1207          22 :                 tmp = eap_fast_process_crypto_binding(sm, data, ret,
    1208             :                                                       tlv.crypto_binding,
    1209             :                                                       tlv.crypto_binding_len);
    1210          22 :                 if (tmp == NULL)
    1211           0 :                         failed = 1;
    1212             :                 else
    1213          22 :                         resp = wpabuf_concat(resp, tmp);
    1214             :         }
    1215             : 
    1216          91 :         if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
    1217           7 :                 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
    1218             :                                           EAP_TLV_RESULT_SUCCESS, 1);
    1219           7 :                 resp = wpabuf_concat(resp, tmp);
    1220             :         }
    1221             : 
    1222          91 :         if (tlv.eap_payload_tlv) {
    1223          58 :                 tmp = eap_fast_process_eap_payload_tlv(
    1224             :                         sm, data, ret, tlv.eap_payload_tlv,
    1225             :                         tlv.eap_payload_tlv_len);
    1226          58 :                 resp = wpabuf_concat(resp, tmp);
    1227             :         }
    1228             : 
    1229          91 :         if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
    1230           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
    1231             :                            "acknowledging success");
    1232           0 :                 failed = 1;
    1233          91 :         } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
    1234          12 :                 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
    1235             :                                            tlv.pac_len);
    1236          12 :                 resp = wpabuf_concat(resp, tmp);
    1237             :         }
    1238             : 
    1239         153 :         if (data->current_pac == NULL && data->provisioning &&
    1240         129 :             !data->anon_provisioning && !tlv.pac &&
    1241          58 :             (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
    1242          28 :              tlv.result == EAP_TLV_RESULT_SUCCESS)) {
    1243             :                 /*
    1244             :                  * Need to request Tunnel PAC when using authenticated
    1245             :                  * provisioning.
    1246             :                  */
    1247           8 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
    1248           8 :                 tmp = eap_fast_pac_request();
    1249           8 :                 resp = wpabuf_concat(resp, tmp);
    1250             :         }
    1251             : 
    1252          91 :         if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
    1253          28 :                 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
    1254          28 :                 resp = wpabuf_concat(tmp, resp);
    1255          63 :         } else if (failed) {
    1256           0 :                 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
    1257           0 :                 resp = wpabuf_concat(tmp, resp);
    1258             :         }
    1259             : 
    1260         119 :         if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
    1261          44 :             tlv.crypto_binding && data->phase2_success) {
    1262          16 :                 if (data->anon_provisioning) {
    1263           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
    1264             :                                    "provisioning completed successfully.");
    1265           0 :                         ret->methodState = METHOD_DONE;
    1266           0 :                         ret->decision = DECISION_FAIL;
    1267           0 :                         sm->expected_failure = 1;
    1268             :                 } else {
    1269          16 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
    1270             :                                    "completed successfully.");
    1271          16 :                         if (data->provisioning)
    1272           7 :                                 ret->methodState = METHOD_MAY_CONT;
    1273             :                         else
    1274           9 :                                 ret->methodState = METHOD_DONE;
    1275          16 :                         ret->decision = DECISION_UNCOND_SUCC;
    1276             :                 }
    1277             :         }
    1278             : 
    1279          91 :         if (resp == NULL) {
    1280           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
    1281             :                            "empty response packet");
    1282           0 :                 resp = wpabuf_alloc(1);
    1283             :         }
    1284             : 
    1285          91 :         return eap_fast_encrypt_response(sm, data, resp, identifier,
    1286             :                                          out_data);
    1287             : }
    1288             : 
    1289             : 
    1290          91 : static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
    1291             :                             struct eap_method_ret *ret, u8 identifier,
    1292             :                             const struct wpabuf *in_data,
    1293             :                             struct wpabuf **out_data)
    1294             : {
    1295             :         struct wpabuf *in_decrypted;
    1296             :         int res;
    1297             : 
    1298          91 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
    1299             :                    " Phase 2", (unsigned long) wpabuf_len(in_data));
    1300             : 
    1301          91 :         if (data->pending_phase2_req) {
    1302          12 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
    1303             :                            "skip decryption and use old data");
    1304             :                 /* Clear TLS reassembly state. */
    1305          12 :                 eap_peer_tls_reset_input(&data->ssl);
    1306             : 
    1307          12 :                 in_decrypted = data->pending_phase2_req;
    1308          12 :                 data->pending_phase2_req = NULL;
    1309          12 :                 goto continue_req;
    1310             :         }
    1311             : 
    1312          79 :         if (wpabuf_len(in_data) == 0) {
    1313             :                 /* Received TLS ACK - requesting more fragments */
    1314           0 :                 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
    1315             :                                             data->fast_version,
    1316             :                                             identifier, NULL, out_data);
    1317             :         }
    1318             : 
    1319          79 :         res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
    1320          79 :         if (res)
    1321           0 :                 return res;
    1322             : 
    1323             : continue_req:
    1324          91 :         wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
    1325             :                         in_decrypted);
    1326             : 
    1327          91 :         if (wpabuf_len(in_decrypted) < 4) {
    1328           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
    1329             :                            "TLV frame (len=%lu)",
    1330             :                            (unsigned long) wpabuf_len(in_decrypted));
    1331           0 :                 wpabuf_free(in_decrypted);
    1332           0 :                 return -1;
    1333             :         }
    1334             : 
    1335          91 :         res = eap_fast_process_decrypted(sm, data, ret, identifier,
    1336             :                                          in_decrypted, out_data);
    1337             : 
    1338          91 :         wpabuf_free(in_decrypted);
    1339             : 
    1340          91 :         return res;
    1341             : }
    1342             : 
    1343             : 
    1344          24 : static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
    1345             : {
    1346             :         const u8 *a_id;
    1347             :         const struct pac_tlv_hdr *hdr;
    1348             : 
    1349             :         /*
    1350             :          * Parse authority identity (A-ID) from the EAP-FAST/Start. This
    1351             :          * supports both raw A-ID and one inside an A-ID TLV.
    1352             :          */
    1353          24 :         a_id = buf;
    1354          24 :         *id_len = len;
    1355          24 :         if (len > sizeof(*hdr)) {
    1356             :                 int tlen;
    1357          24 :                 hdr = (const struct pac_tlv_hdr *) buf;
    1358          24 :                 tlen = be_to_host16(hdr->len);
    1359          48 :                 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
    1360          24 :                     sizeof(*hdr) + tlen <= len) {
    1361          24 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
    1362             :                                    "(Start)");
    1363          24 :                         a_id = (const u8 *) (hdr + 1);
    1364          24 :                         *id_len = tlen;
    1365             :                 }
    1366             :         }
    1367          24 :         wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
    1368             : 
    1369          24 :         return a_id;
    1370             : }
    1371             : 
    1372             : 
    1373          24 : static void eap_fast_select_pac(struct eap_fast_data *data,
    1374             :                                 const u8 *a_id, size_t a_id_len)
    1375             : {
    1376          24 :         data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
    1377             :                                              PAC_TYPE_TUNNEL_PAC);
    1378          24 :         if (data->current_pac == NULL) {
    1379             :                 /*
    1380             :                  * Tunnel PAC was not available for this A-ID. Try to use
    1381             :                  * Machine Authentication PAC, if one is available.
    1382             :                  */
    1383          13 :                 data->current_pac = eap_fast_get_pac(
    1384             :                         data->pac, a_id, a_id_len,
    1385             :                         PAC_TYPE_MACHINE_AUTHENTICATION);
    1386             :         }
    1387             : 
    1388          24 :         if (data->current_pac) {
    1389          11 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
    1390          11 :                            "(PAC-Type %d)", data->current_pac->pac_type);
    1391          22 :                 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
    1392          11 :                                   data->current_pac->a_id_info,
    1393          11 :                                   data->current_pac->a_id_info_len);
    1394             :         }
    1395          24 : }
    1396             : 
    1397             : 
    1398          11 : static int eap_fast_use_pac_opaque(struct eap_sm *sm,
    1399             :                                    struct eap_fast_data *data,
    1400             :                                    struct eap_fast_pac *pac)
    1401             : {
    1402             :         u8 *tlv;
    1403             :         size_t tlv_len, olen;
    1404             :         struct eap_tlv_hdr *ehdr;
    1405             : 
    1406          11 :         olen = pac->pac_opaque_len;
    1407          11 :         tlv_len = sizeof(*ehdr) + olen;
    1408          11 :         tlv = os_malloc(tlv_len);
    1409          11 :         if (tlv) {
    1410          11 :                 ehdr = (struct eap_tlv_hdr *) tlv;
    1411          11 :                 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
    1412          11 :                 ehdr->length = host_to_be16(olen);
    1413          11 :                 os_memcpy(ehdr + 1, pac->pac_opaque, olen);
    1414             :         }
    1415          22 :         if (tlv == NULL ||
    1416          11 :             tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
    1417             :                                             TLS_EXT_PAC_OPAQUE,
    1418             :                                             tlv, tlv_len) < 0) {
    1419           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
    1420             :                            "extension");
    1421           0 :                 os_free(tlv);
    1422           0 :                 return -1;
    1423             :         }
    1424          11 :         os_free(tlv);
    1425             : 
    1426          11 :         return 0;
    1427             : }
    1428             : 
    1429             : 
    1430          13 : static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
    1431             :                                          struct eap_fast_data *data)
    1432             : {
    1433          13 :         if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
    1434             :                                             TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
    1435           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
    1436             :                            "TLS extension");
    1437           0 :                 return -1;
    1438             :         }
    1439          13 :         return 0;
    1440             : }
    1441             : 
    1442             : 
    1443          13 : static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
    1444             :                                              struct eap_fast_data *data)
    1445             : {
    1446             :         u8 ciphers[5];
    1447          13 :         int count = 0;
    1448             : 
    1449          13 :         if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
    1450           5 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
    1451             :                            "provisioning TLS cipher suites");
    1452           5 :                 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
    1453             :         }
    1454             : 
    1455          13 :         if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
    1456           8 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
    1457             :                            "provisioning TLS cipher suites");
    1458           8 :                 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
    1459           8 :                 ciphers[count++] = TLS_CIPHER_AES128_SHA;
    1460           8 :                 ciphers[count++] = TLS_CIPHER_RC4_SHA;
    1461             :         }
    1462             : 
    1463          13 :         ciphers[count++] = TLS_CIPHER_NONE;
    1464             : 
    1465          13 :         if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
    1466             :                                            ciphers)) {
    1467           0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
    1468             :                            "cipher suites for provisioning");
    1469           0 :                 return -1;
    1470             :         }
    1471             : 
    1472          13 :         return 0;
    1473             : }
    1474             : 
    1475             : 
    1476          24 : static int eap_fast_process_start(struct eap_sm *sm,
    1477             :                                   struct eap_fast_data *data, u8 flags,
    1478             :                                   const u8 *pos, size_t left)
    1479             : {
    1480             :         const u8 *a_id;
    1481             :         size_t a_id_len;
    1482             : 
    1483             :         /* EAP-FAST Version negotiation (section 3.1) */
    1484          24 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
    1485             :                    flags & EAP_TLS_VERSION_MASK, data->fast_version);
    1486          24 :         if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
    1487           0 :                 data->fast_version = flags & EAP_TLS_VERSION_MASK;
    1488          24 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
    1489             :                    data->fast_version);
    1490             : 
    1491          24 :         a_id = eap_fast_get_a_id(pos, left, &a_id_len);
    1492          24 :         eap_fast_select_pac(data, a_id, a_id_len);
    1493             : 
    1494          24 :         if (data->resuming && data->current_pac) {
    1495           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
    1496             :                            "do not add PAC-Opaque to TLS ClientHello");
    1497           0 :                 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
    1498           0 :                         return -1;
    1499          24 :         } else if (data->current_pac) {
    1500             :                 /*
    1501             :                  * PAC found for the A-ID and we are not resuming an old
    1502             :                  * session, so add PAC-Opaque extension to ClientHello.
    1503             :                  */
    1504          11 :                 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
    1505           0 :                         return -1;
    1506             :         } else {
    1507             :                 /* No PAC found, so we must provision one. */
    1508          13 :                 if (!data->provisioning_allowed) {
    1509           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
    1510             :                                    "provisioning disabled");
    1511           0 :                         return -1;
    1512             :                 }
    1513          13 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
    1514             :                            "starting provisioning");
    1515          26 :                 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
    1516          13 :                     eap_fast_clear_pac_opaque_ext(sm, data) < 0)
    1517           0 :                         return -1;
    1518          13 :                 data->provisioning = 1;
    1519             :         }
    1520             : 
    1521          24 :         return 0;
    1522             : }
    1523             : 
    1524             : 
    1525         146 : static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
    1526             :                                         struct eap_method_ret *ret,
    1527             :                                         const struct wpabuf *reqData)
    1528             : {
    1529             :         const struct eap_hdr *req;
    1530             :         size_t left;
    1531             :         int res;
    1532             :         u8 flags, id;
    1533             :         struct wpabuf *resp;
    1534             :         const u8 *pos;
    1535         146 :         struct eap_fast_data *data = priv;
    1536             :         struct wpabuf msg;
    1537             : 
    1538         146 :         pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
    1539             :                                         reqData, &left, &flags);
    1540         146 :         if (pos == NULL)
    1541           0 :                 return NULL;
    1542             : 
    1543         146 :         req = wpabuf_head(reqData);
    1544         146 :         id = req->identifier;
    1545             : 
    1546         146 :         if (flags & EAP_TLS_FLAGS_START) {
    1547          24 :                 if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
    1548           0 :                         return NULL;
    1549             : 
    1550          24 :                 left = 0; /* A-ID is not used in further packet processing */
    1551             :         }
    1552             : 
    1553         146 :         wpabuf_set(&msg, pos, left);
    1554             : 
    1555         146 :         resp = NULL;
    1556         225 :         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
    1557          79 :             !data->resuming) {
    1558             :                 /* Process tunneled (encrypted) phase 2 data. */
    1559          79 :                 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
    1560         158 :                 if (res < 0) {
    1561           0 :                         ret->methodState = METHOD_DONE;
    1562           0 :                         ret->decision = DECISION_FAIL;
    1563             :                         /*
    1564             :                          * Ack possible Alert that may have caused failure in
    1565             :                          * decryption.
    1566             :                          */
    1567           0 :                         res = 1;
    1568             :                 }
    1569             :         } else {
    1570             :                 /* Continue processing TLS handshake (phase 1). */
    1571          67 :                 res = eap_peer_tls_process_helper(sm, &data->ssl,
    1572             :                                                   EAP_TYPE_FAST,
    1573             :                                                   data->fast_version, id, &msg,
    1574             :                                                   &resp);
    1575          67 :                 if (res < 0) {
    1576           0 :                         wpa_printf(MSG_DEBUG,
    1577             :                                    "EAP-FAST: TLS processing failed");
    1578           0 :                         ret->methodState = METHOD_DONE;
    1579           0 :                         ret->decision = DECISION_FAIL;
    1580           0 :                         return resp;
    1581             :                 }
    1582             : 
    1583          67 :                 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
    1584             :                         char cipher[80];
    1585          23 :                         wpa_printf(MSG_DEBUG,
    1586             :                                    "EAP-FAST: TLS done, proceed to Phase 2");
    1587          36 :                         if (data->provisioning &&
    1588          13 :                             (!(data->provisioning_allowed &
    1589           8 :                                EAP_FAST_PROV_AUTH) ||
    1590           8 :                              tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
    1591           8 :                                             cipher, sizeof(cipher)) < 0 ||
    1592          16 :                              os_strstr(cipher, "ADH-") ||
    1593           8 :                              os_strstr(cipher, "anon"))) {
    1594           5 :                                 wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
    1595             :                                            "anonymous (unauthenticated) "
    1596             :                                            "provisioning");
    1597           5 :                                 data->anon_provisioning = 1;
    1598             :                         } else
    1599          18 :                                 data->anon_provisioning = 0;
    1600          23 :                         data->resuming = 0;
    1601          23 :                         if (eap_fast_derive_keys(sm, data) < 0) {
    1602           1 :                                 wpa_printf(MSG_DEBUG,
    1603             :                                            "EAP-FAST: Could not derive keys");
    1604           1 :                                 ret->methodState = METHOD_DONE;
    1605           1 :                                 ret->decision = DECISION_FAIL;
    1606           1 :                                 wpabuf_free(resp);
    1607           1 :                                 return NULL;
    1608             :                         }
    1609             :                 }
    1610             : 
    1611          66 :                 if (res == 2) {
    1612             :                         /*
    1613             :                          * Application data included in the handshake message.
    1614             :                          */
    1615          12 :                         wpabuf_free(data->pending_phase2_req);
    1616          12 :                         data->pending_phase2_req = resp;
    1617          12 :                         resp = NULL;
    1618          12 :                         res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
    1619             :                 }
    1620             :         }
    1621             : 
    1622         145 :         if (res == 1) {
    1623           7 :                 wpabuf_free(resp);
    1624           7 :                 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
    1625             :                                               data->fast_version);
    1626             :         }
    1627             : 
    1628         138 :         return resp;
    1629             : }
    1630             : 
    1631             : 
    1632             : #if 0 /* FIX */
    1633             : static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
    1634             : {
    1635             :         struct eap_fast_data *data = priv;
    1636             :         return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
    1637             : }
    1638             : 
    1639             : 
    1640             : static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
    1641             : {
    1642             :         struct eap_fast_data *data = priv;
    1643             :         os_free(data->key_block_p);
    1644             :         data->key_block_p = NULL;
    1645             :         wpabuf_free(data->pending_phase2_req);
    1646             :         data->pending_phase2_req = NULL;
    1647             : }
    1648             : 
    1649             : 
    1650             : static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
    1651             : {
    1652             :         struct eap_fast_data *data = priv;
    1653             :         if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
    1654             :                 os_free(data);
    1655             :                 return NULL;
    1656             :         }
    1657             :         os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
    1658             :         os_memset(data->emsk, 0, EAP_EMSK_LEN);
    1659             :         os_free(data->session_id);
    1660             :         data->session_id = NULL;
    1661             :         if (data->phase2_priv && data->phase2_method &&
    1662             :             data->phase2_method->init_for_reauth)
    1663             :                 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
    1664             :         data->phase2_success = 0;
    1665             :         data->resuming = 1;
    1666             :         data->provisioning = 0;
    1667             :         data->anon_provisioning = 0;
    1668             :         data->simck_idx = 0;
    1669             :         return priv;
    1670             : }
    1671             : #endif
    1672             : 
    1673             : 
    1674          14 : static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
    1675             :                                size_t buflen, int verbose)
    1676             : {
    1677          14 :         struct eap_fast_data *data = priv;
    1678             :         int len, ret;
    1679             : 
    1680          14 :         len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
    1681          14 :         if (data->phase2_method) {
    1682          14 :                 ret = os_snprintf(buf + len, buflen - len,
    1683             :                                   "EAP-FAST Phase2 method=%s\n",
    1684          14 :                                   data->phase2_method->name);
    1685          14 :                 if (os_snprintf_error(buflen - len, ret))
    1686           0 :                         return len;
    1687          14 :                 len += ret;
    1688             :         }
    1689          14 :         return len;
    1690             : }
    1691             : 
    1692             : 
    1693         146 : static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
    1694             : {
    1695         146 :         struct eap_fast_data *data = priv;
    1696         146 :         return data->success;
    1697             : }
    1698             : 
    1699             : 
    1700          26 : static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
    1701             : {
    1702          26 :         struct eap_fast_data *data = priv;
    1703             :         u8 *key;
    1704             : 
    1705          26 :         if (!data->success)
    1706           0 :                 return NULL;
    1707             : 
    1708          26 :         key = os_malloc(EAP_FAST_KEY_LEN);
    1709          26 :         if (key == NULL)
    1710           0 :                 return NULL;
    1711             : 
    1712          26 :         *len = EAP_FAST_KEY_LEN;
    1713          26 :         os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
    1714             : 
    1715          26 :         return key;
    1716             : }
    1717             : 
    1718             : 
    1719          26 : static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
    1720             : {
    1721          26 :         struct eap_fast_data *data = priv;
    1722             :         u8 *id;
    1723             : 
    1724          26 :         if (!data->success)
    1725           0 :                 return NULL;
    1726             : 
    1727          26 :         id = os_malloc(data->id_len);
    1728          26 :         if (id == NULL)
    1729           0 :                 return NULL;
    1730             : 
    1731          26 :         *len = data->id_len;
    1732          26 :         os_memcpy(id, data->session_id, data->id_len);
    1733             : 
    1734          26 :         return id;
    1735             : }
    1736             : 
    1737             : 
    1738           2 : static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
    1739             : {
    1740           2 :         struct eap_fast_data *data = priv;
    1741             :         u8 *key;
    1742             : 
    1743           2 :         if (!data->success)
    1744           0 :                 return NULL;
    1745             : 
    1746           2 :         key = os_malloc(EAP_EMSK_LEN);
    1747           2 :         if (key == NULL)
    1748           0 :                 return NULL;
    1749             : 
    1750           2 :         *len = EAP_EMSK_LEN;
    1751           2 :         os_memcpy(key, data->emsk, EAP_EMSK_LEN);
    1752             : 
    1753           2 :         return key;
    1754             : }
    1755             : 
    1756             : 
    1757          49 : int eap_peer_fast_register(void)
    1758             : {
    1759             :         struct eap_method *eap;
    1760             :         int ret;
    1761             : 
    1762          49 :         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
    1763             :                                     EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
    1764          49 :         if (eap == NULL)
    1765           0 :                 return -1;
    1766             : 
    1767          49 :         eap->init = eap_fast_init;
    1768          49 :         eap->deinit = eap_fast_deinit;
    1769          49 :         eap->process = eap_fast_process;
    1770          49 :         eap->isKeyAvailable = eap_fast_isKeyAvailable;
    1771          49 :         eap->getKey = eap_fast_getKey;
    1772          49 :         eap->getSessionId = eap_fast_get_session_id;
    1773          49 :         eap->get_status = eap_fast_get_status;
    1774             : #if 0
    1775             :         eap->has_reauth_data = eap_fast_has_reauth_data;
    1776             :         eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
    1777             :         eap->init_for_reauth = eap_fast_init_for_reauth;
    1778             : #endif
    1779          49 :         eap->get_emsk = eap_fast_get_emsk;
    1780             : 
    1781          49 :         ret = eap_peer_method_register(eap);
    1782          49 :         if (ret)
    1783           0 :                 eap_peer_method_free(eap);
    1784          49 :         return ret;
    1785             : }

Generated by: LCOV version 1.10