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 1401264779 Lines: 635 827 76.8 %
Date: 2014-05-28 Functions: 42 44 95.5 %

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

Generated by: LCOV version 1.10