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 1475438200 Lines: 809 858 94.3 %
Date: 2016-10-02 Functions: 44 44 100.0 %

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

Generated by: LCOV version 1.10