LCOV - code coverage report
Current view: top level - src/eap_peer - eap_peap.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401264779 Lines: 490 629 77.9 %
Date: 2014-05-28 Functions: 20 21 95.2 %

          Line data    Source code
       1             : /*
       2             :  * EAP peer method: EAP-PEAP (draft-josefsson-pppext-eap-tls-eap-10.txt)
       3             :  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : 
      11             : #include "common.h"
      12             : #include "crypto/sha1.h"
      13             : #include "crypto/tls.h"
      14             : #include "eap_common/eap_tlv_common.h"
      15             : #include "eap_common/eap_peap_common.h"
      16             : #include "eap_i.h"
      17             : #include "eap_tls_common.h"
      18             : #include "eap_config.h"
      19             : #include "tncc.h"
      20             : 
      21             : 
      22             : /* Maximum supported PEAP version
      23             :  * 0 = Microsoft's PEAP version 0; draft-kamath-pppext-peapv0-00.txt
      24             :  * 1 = draft-josefsson-ppext-eap-tls-eap-05.txt
      25             :  */
      26             : #define EAP_PEAP_VERSION 1
      27             : 
      28             : 
      29             : static void eap_peap_deinit(struct eap_sm *sm, void *priv);
      30             : 
      31             : 
      32             : struct eap_peap_data {
      33             :         struct eap_ssl_data ssl;
      34             : 
      35             :         int peap_version, force_peap_version, force_new_label;
      36             : 
      37             :         const struct eap_method *phase2_method;
      38             :         void *phase2_priv;
      39             :         int phase2_success;
      40             :         int phase2_eap_success;
      41             :         int phase2_eap_started;
      42             : 
      43             :         struct eap_method_type phase2_type;
      44             :         struct eap_method_type *phase2_types;
      45             :         size_t num_phase2_types;
      46             : 
      47             :         int peap_outer_success; /* 0 = PEAP terminated on Phase 2 inner
      48             :                                  * EAP-Success
      49             :                                  * 1 = reply with tunneled EAP-Success to inner
      50             :                                  * EAP-Success and expect AS to send outer
      51             :                                  * (unencrypted) EAP-Success after this
      52             :                                  * 2 = reply with PEAP/TLS ACK to inner
      53             :                                  * EAP-Success and expect AS to send outer
      54             :                                  * (unencrypted) EAP-Success after this */
      55             :         int resuming; /* starting a resumed session */
      56             :         int reauth; /* reauthentication */
      57             :         u8 *key_data;
      58             :         u8 *session_id;
      59             :         size_t id_len;
      60             : 
      61             :         struct wpabuf *pending_phase2_req;
      62             :         enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
      63             :         int crypto_binding_used;
      64             :         u8 binding_nonce[32];
      65             :         u8 ipmk[40];
      66             :         u8 cmk[20];
      67             :         int soh; /* Whether IF-TNCCS-SOH (Statement of Health; Microsoft NAP)
      68             :                   * is enabled. */
      69             : };
      70             : 
      71             : 
      72          13 : static int eap_peap_parse_phase1(struct eap_peap_data *data,
      73             :                                  const char *phase1)
      74             : {
      75             :         const char *pos;
      76             : 
      77          13 :         pos = os_strstr(phase1, "peapver=");
      78          13 :         if (pos) {
      79           8 :                 data->force_peap_version = atoi(pos + 8);
      80           8 :                 data->peap_version = data->force_peap_version;
      81           8 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Forced PEAP version %d",
      82             :                            data->force_peap_version);
      83             :         }
      84             : 
      85          13 :         if (os_strstr(phase1, "peaplabel=1")) {
      86           2 :                 data->force_new_label = 1;
      87           2 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Force new label for key "
      88             :                            "derivation");
      89             :         }
      90             : 
      91          13 :         if (os_strstr(phase1, "peap_outer_success=0")) {
      92           0 :                 data->peap_outer_success = 0;
      93           0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: terminate authentication on "
      94             :                            "tunneled EAP-Success");
      95          13 :         } else if (os_strstr(phase1, "peap_outer_success=1")) {
      96           1 :                 data->peap_outer_success = 1;
      97           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: send tunneled EAP-Success "
      98             :                            "after receiving tunneled EAP-Success");
      99          12 :         } else if (os_strstr(phase1, "peap_outer_success=2")) {
     100           1 :                 data->peap_outer_success = 2;
     101           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: send PEAP/TLS ACK after "
     102             :                            "receiving tunneled EAP-Success");
     103             :         }
     104             : 
     105          13 :         if (os_strstr(phase1, "crypto_binding=0")) {
     106           1 :                 data->crypto_binding = NO_BINDING;
     107           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Do not use cryptobinding");
     108          12 :         } else if (os_strstr(phase1, "crypto_binding=1")) {
     109           1 :                 data->crypto_binding = OPTIONAL_BINDING;
     110           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Optional cryptobinding");
     111          11 :         } else if (os_strstr(phase1, "crypto_binding=2")) {
     112           1 :                 data->crypto_binding = REQUIRE_BINDING;
     113           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Require cryptobinding");
     114             :         }
     115             : 
     116             : #ifdef EAP_TNC
     117          13 :         if (os_strstr(phase1, "tnc=soh2")) {
     118           1 :                 data->soh = 2;
     119           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
     120          12 :         } else if (os_strstr(phase1, "tnc=soh1")) {
     121           1 :                 data->soh = 1;
     122           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 1 enabled");
     123          11 :         } else if (os_strstr(phase1, "tnc=soh")) {
     124           1 :                 data->soh = 2;
     125           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
     126             :         }
     127             : #endif /* EAP_TNC */
     128             : 
     129          13 :         return 0;
     130             : }
     131             : 
     132             : 
     133          27 : static void * eap_peap_init(struct eap_sm *sm)
     134             : {
     135             :         struct eap_peap_data *data;
     136          27 :         struct eap_peer_config *config = eap_get_config(sm);
     137             : 
     138          27 :         data = os_zalloc(sizeof(*data));
     139          27 :         if (data == NULL)
     140           0 :                 return NULL;
     141          27 :         sm->peap_done = FALSE;
     142          27 :         data->peap_version = EAP_PEAP_VERSION;
     143          27 :         data->force_peap_version = -1;
     144          27 :         data->peap_outer_success = 2;
     145          27 :         data->crypto_binding = OPTIONAL_BINDING;
     146             : 
     147          40 :         if (config && config->phase1 &&
     148          13 :             eap_peap_parse_phase1(data, config->phase1) < 0) {
     149           0 :                 eap_peap_deinit(sm, data);
     150           0 :                 return NULL;
     151             :         }
     152             : 
     153          27 :         if (eap_peer_select_phase2_methods(config, "auth=",
     154             :                                            &data->phase2_types,
     155             :                                            &data->num_phase2_types) < 0) {
     156           0 :                 eap_peap_deinit(sm, data);
     157           0 :                 return NULL;
     158             :         }
     159             : 
     160          27 :         data->phase2_type.vendor = EAP_VENDOR_IETF;
     161          27 :         data->phase2_type.method = EAP_TYPE_NONE;
     162             : 
     163          27 :         if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_PEAP)) {
     164           0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
     165           0 :                 eap_peap_deinit(sm, data);
     166           0 :                 return NULL;
     167             :         }
     168             : 
     169          27 :         return data;
     170             : }
     171             : 
     172             : 
     173          27 : static void eap_peap_deinit(struct eap_sm *sm, void *priv)
     174             : {
     175          27 :         struct eap_peap_data *data = priv;
     176          27 :         if (data == NULL)
     177          27 :                 return;
     178          27 :         if (data->phase2_priv && data->phase2_method)
     179          27 :                 data->phase2_method->deinit(sm, data->phase2_priv);
     180          27 :         os_free(data->phase2_types);
     181          27 :         eap_peer_tls_ssl_deinit(sm, &data->ssl);
     182          27 :         os_free(data->key_data);
     183          27 :         os_free(data->session_id);
     184          27 :         wpabuf_free(data->pending_phase2_req);
     185          27 :         os_free(data);
     186             : }
     187             : 
     188             : 
     189             : /**
     190             :  * eap_tlv_build_nak - Build EAP-TLV NAK message
     191             :  * @id: EAP identifier for the header
     192             :  * @nak_type: TLV type (EAP_TLV_*)
     193             :  * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
     194             :  *
     195             :  * This function builds an EAP-TLV NAK message. The caller is responsible for
     196             :  * freeing the returned buffer.
     197             :  */
     198           0 : static struct wpabuf * eap_tlv_build_nak(int id, u16 nak_type)
     199             : {
     200             :         struct wpabuf *msg;
     201             : 
     202           0 :         msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, 10,
     203             :                             EAP_CODE_RESPONSE, id);
     204           0 :         if (msg == NULL)
     205           0 :                 return NULL;
     206             : 
     207           0 :         wpabuf_put_u8(msg, 0x80); /* Mandatory */
     208           0 :         wpabuf_put_u8(msg, EAP_TLV_NAK_TLV);
     209           0 :         wpabuf_put_be16(msg, 6); /* Length */
     210           0 :         wpabuf_put_be32(msg, 0); /* Vendor-Id */
     211           0 :         wpabuf_put_be16(msg, nak_type); /* NAK-Type */
     212             : 
     213           0 :         return msg;
     214             : }
     215             : 
     216             : 
     217           7 : static int eap_peap_get_isk(struct eap_sm *sm, struct eap_peap_data *data,
     218             :                             u8 *isk, size_t isk_len)
     219             : {
     220             :         u8 *key;
     221             :         size_t key_len;
     222             : 
     223           7 :         os_memset(isk, 0, isk_len);
     224          14 :         if (data->phase2_method == NULL || data->phase2_priv == NULL ||
     225          14 :             data->phase2_method->isKeyAvailable == NULL ||
     226           7 :             data->phase2_method->getKey == NULL)
     227           0 :                 return 0;
     228             : 
     229          14 :         if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
     230           7 :             (key = data->phase2_method->getKey(sm, data->phase2_priv,
     231             :                                                &key_len)) == NULL) {
     232           0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not get key material "
     233             :                            "from Phase 2");
     234           0 :                 return -1;
     235             :         }
     236             : 
     237           7 :         if (key_len > isk_len)
     238           0 :                 key_len = isk_len;
     239           7 :         os_memcpy(isk, key, key_len);
     240           7 :         os_free(key);
     241             : 
     242           7 :         return 0;
     243             : }
     244             : 
     245             : 
     246           7 : static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
     247             : {
     248             :         u8 *tk;
     249             :         u8 isk[32], imck[60];
     250             : 
     251             :         /*
     252             :          * Tunnel key (TK) is the first 60 octets of the key generated by
     253             :          * phase 1 of PEAP (based on TLS).
     254             :          */
     255           7 :         tk = data->key_data;
     256           7 :         if (tk == NULL)
     257           0 :                 return -1;
     258           7 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
     259             : 
     260           8 :         if (data->reauth &&
     261           1 :             tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
     262             :                 /* Fast-connect: IPMK|CMK = TK */
     263           0 :                 os_memcpy(data->ipmk, tk, 40);
     264           0 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK from TK",
     265           0 :                                 data->ipmk, 40);
     266           0 :                 os_memcpy(data->cmk, tk + 40, 20);
     267           0 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK from TK",
     268           0 :                                 data->cmk, 20);
     269           0 :                 return 0;
     270             :         }
     271             : 
     272           7 :         if (eap_peap_get_isk(sm, data, isk, sizeof(isk)) < 0)
     273           0 :                 return -1;
     274           7 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
     275             : 
     276             :         /*
     277             :          * IPMK Seed = "Inner Methods Compound Keys" | ISK
     278             :          * TempKey = First 40 octets of TK
     279             :          * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
     280             :          * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
     281             :          * in the end of the label just before ISK; is that just a typo?)
     282             :          */
     283           7 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
     284           7 :         if (peap_prfplus(data->peap_version, tk, 40,
     285             :                          "Inner Methods Compound Keys",
     286             :                          isk, sizeof(isk), imck, sizeof(imck)) < 0)
     287           0 :                 return -1;
     288           7 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
     289             :                         imck, sizeof(imck));
     290             : 
     291           7 :         os_memcpy(data->ipmk, imck, 40);
     292           7 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
     293           7 :         os_memcpy(data->cmk, imck + 40, 20);
     294           7 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
     295             : 
     296           7 :         return 0;
     297             : }
     298             : 
     299             : 
     300           6 : static int eap_tlv_add_cryptobinding(struct eap_sm *sm,
     301             :                                      struct eap_peap_data *data,
     302             :                                      struct wpabuf *buf)
     303             : {
     304             :         u8 *mac;
     305           6 :         u8 eap_type = EAP_TYPE_PEAP;
     306             :         const u8 *addr[2];
     307             :         size_t len[2];
     308             :         u16 tlv_type;
     309             : 
     310             :         /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
     311           6 :         addr[0] = wpabuf_put(buf, 0);
     312           6 :         len[0] = 60;
     313           6 :         addr[1] = &eap_type;
     314           6 :         len[1] = 1;
     315             : 
     316           6 :         tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
     317           6 :         wpabuf_put_be16(buf, tlv_type);
     318           6 :         wpabuf_put_be16(buf, 56);
     319             : 
     320           6 :         wpabuf_put_u8(buf, 0); /* Reserved */
     321           6 :         wpabuf_put_u8(buf, data->peap_version); /* Version */
     322           6 :         wpabuf_put_u8(buf, data->peap_version); /* RecvVersion */
     323           6 :         wpabuf_put_u8(buf, 1); /* SubType: 0 = Request, 1 = Response */
     324           6 :         wpabuf_put_data(buf, data->binding_nonce, 32); /* Nonce */
     325           6 :         mac = wpabuf_put(buf, 20); /* Compound_MAC */
     326           6 :         wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK", data->cmk, 20);
     327          12 :         wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
     328           6 :                     addr[0], len[0]);
     329          12 :         wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
     330           6 :                     addr[1], len[1]);
     331           6 :         hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac);
     332           6 :         wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC", mac, SHA1_MAC_LEN);
     333           6 :         data->crypto_binding_used = 1;
     334             : 
     335           6 :         return 0;
     336             : }
     337             : 
     338             : 
     339             : /**
     340             :  * eap_tlv_build_result - Build EAP-TLV Result message
     341             :  * @id: EAP identifier for the header
     342             :  * @status: Status (EAP_TLV_RESULT_SUCCESS or EAP_TLV_RESULT_FAILURE)
     343             :  * Returns: Buffer to the allocated EAP-TLV Result message or %NULL on failure
     344             :  *
     345             :  * This function builds an EAP-TLV Result message. The caller is responsible
     346             :  * for freeing the returned buffer.
     347             :  */
     348           8 : static struct wpabuf * eap_tlv_build_result(struct eap_sm *sm,
     349             :                                             struct eap_peap_data *data,
     350             :                                             int crypto_tlv_used,
     351             :                                             int id, u16 status)
     352             : {
     353             :         struct wpabuf *msg;
     354             :         size_t len;
     355             : 
     356           8 :         if (data->crypto_binding == NO_BINDING)
     357           1 :                 crypto_tlv_used = 0;
     358             : 
     359           8 :         len = 6;
     360           8 :         if (crypto_tlv_used)
     361           6 :                 len += 60; /* Cryptobinding TLV */
     362           8 :         msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, len,
     363             :                             EAP_CODE_RESPONSE, id);
     364           8 :         if (msg == NULL)
     365           0 :                 return NULL;
     366             : 
     367           8 :         wpabuf_put_u8(msg, 0x80); /* Mandatory */
     368           8 :         wpabuf_put_u8(msg, EAP_TLV_RESULT_TLV);
     369           8 :         wpabuf_put_be16(msg, 2); /* Length */
     370           8 :         wpabuf_put_be16(msg, status); /* Status */
     371             : 
     372           8 :         if (crypto_tlv_used && eap_tlv_add_cryptobinding(sm, data, msg)) {
     373           0 :                 wpabuf_free(msg);
     374           0 :                 return NULL;
     375             :         }
     376             : 
     377           8 :         return msg;
     378             : }
     379             : 
     380             : 
     381           7 : static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
     382             :                                           struct eap_peap_data *data,
     383             :                                           const u8 *crypto_tlv,
     384             :                                           size_t crypto_tlv_len)
     385             : {
     386             :         u8 buf[61], mac[SHA1_MAC_LEN];
     387             :         const u8 *pos;
     388             : 
     389           7 :         if (eap_peap_derive_cmk(sm, data) < 0) {
     390           0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not derive CMK");
     391           0 :                 return -1;
     392             :         }
     393             : 
     394           7 :         if (crypto_tlv_len != 4 + 56) {
     395           0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
     396             :                            "length %d", (int) crypto_tlv_len);
     397           0 :                 return -1;
     398             :         }
     399             : 
     400           7 :         pos = crypto_tlv;
     401           7 :         pos += 4; /* TLV header */
     402           7 :         if (pos[1] != data->peap_version) {
     403           0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
     404             :                            "mismatch (was %d; expected %d)",
     405           0 :                            pos[1], data->peap_version);
     406           0 :                 return -1;
     407             :         }
     408             : 
     409           7 :         if (pos[3] != 0) {
     410           0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
     411           0 :                            "SubType %d", pos[3]);
     412           0 :                 return -1;
     413             :         }
     414           7 :         pos += 4;
     415           7 :         os_memcpy(data->binding_nonce, pos, 32);
     416           7 :         pos += 32; /* Nonce */
     417             : 
     418             :         /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
     419           7 :         os_memcpy(buf, crypto_tlv, 60);
     420           7 :         os_memset(buf + 4 + 4 + 32, 0, 20); /* Compound_MAC */
     421           7 :         buf[60] = EAP_TYPE_PEAP;
     422           7 :         wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Compound_MAC data",
     423             :                     buf, sizeof(buf));
     424           7 :         hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
     425             : 
     426           7 :         if (os_memcmp(mac, pos, SHA1_MAC_LEN) != 0) {
     427           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
     428             :                            "cryptobinding TLV");
     429           1 :                 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received MAC",
     430             :                             pos, SHA1_MAC_LEN);
     431           1 :                 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Expected MAC",
     432             :                             mac, SHA1_MAC_LEN);
     433           1 :                 return -1;
     434             :         }
     435             : 
     436           6 :         wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
     437             : 
     438           6 :         return 0;
     439             : }
     440             : 
     441             : 
     442             : /**
     443             :  * eap_tlv_process - Process a received EAP-TLV message and generate a response
     444             :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     445             :  * @ret: Return values from EAP request validation and processing
     446             :  * @req: EAP-TLV request to be processed. The caller must have validated that
     447             :  * the buffer is large enough to contain full request (hdr->length bytes) and
     448             :  * that the EAP type is EAP_TYPE_TLV.
     449             :  * @resp: Buffer to return a pointer to the allocated response message. This
     450             :  * field should be initialized to %NULL before the call. The value will be
     451             :  * updated if a response message is generated. The caller is responsible for
     452             :  * freeing the allocated message.
     453             :  * @force_failure: Force negotiation to fail
     454             :  * Returns: 0 on success, -1 on failure
     455             :  */
     456           8 : static int eap_tlv_process(struct eap_sm *sm, struct eap_peap_data *data,
     457             :                            struct eap_method_ret *ret,
     458             :                            const struct wpabuf *req, struct wpabuf **resp,
     459             :                            int force_failure)
     460             : {
     461             :         size_t left, tlv_len;
     462             :         const u8 *pos;
     463           8 :         const u8 *result_tlv = NULL, *crypto_tlv = NULL;
     464           8 :         size_t result_tlv_len = 0, crypto_tlv_len = 0;
     465             :         int tlv_type, mandatory;
     466             : 
     467             :         /* Parse TLVs */
     468           8 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, req, &left);
     469           8 :         if (pos == NULL)
     470           0 :                 return -1;
     471           8 :         wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
     472          32 :         while (left >= 4) {
     473          16 :                 mandatory = !!(pos[0] & 0x80);
     474          16 :                 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
     475          16 :                 pos += 2;
     476          16 :                 tlv_len = WPA_GET_BE16(pos);
     477          16 :                 pos += 2;
     478          16 :                 left -= 4;
     479          16 :                 if (tlv_len > left) {
     480           0 :                         wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
     481             :                                    "(tlv_len=%lu left=%lu)",
     482             :                                    (unsigned long) tlv_len,
     483             :                                    (unsigned long) left);
     484           0 :                         return -1;
     485             :                 }
     486          16 :                 switch (tlv_type) {
     487             :                 case EAP_TLV_RESULT_TLV:
     488           8 :                         result_tlv = pos;
     489           8 :                         result_tlv_len = tlv_len;
     490           8 :                         break;
     491             :                 case EAP_TLV_CRYPTO_BINDING_TLV:
     492           8 :                         crypto_tlv = pos;
     493           8 :                         crypto_tlv_len = tlv_len;
     494           8 :                         break;
     495             :                 default:
     496           0 :                         wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
     497             :                                    "%d%s", tlv_type,
     498             :                                    mandatory ? " (mandatory)" : "");
     499           0 :                         if (mandatory) {
     500             :                                 /* NAK TLV and ignore all TLVs in this packet.
     501             :                                  */
     502           0 :                                 *resp = eap_tlv_build_nak(eap_get_id(req),
     503             :                                                           tlv_type);
     504           0 :                                 return *resp == NULL ? -1 : 0;
     505             :                         }
     506             :                         /* Ignore this TLV, but process other TLVs */
     507           0 :                         break;
     508             :                 }
     509             : 
     510          16 :                 pos += tlv_len;
     511          16 :                 left -= tlv_len;
     512             :         }
     513           8 :         if (left) {
     514           0 :                 wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
     515             :                            "Request (left=%lu)", (unsigned long) left);
     516           0 :                 return -1;
     517             :         }
     518             : 
     519             :         /* Process supported TLVs */
     520           8 :         if (crypto_tlv && data->crypto_binding != NO_BINDING) {
     521           7 :                 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
     522             :                             crypto_tlv, crypto_tlv_len);
     523          14 :                 if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
     524             :                                                    crypto_tlv_len + 4) < 0) {
     525           1 :                         if (result_tlv == NULL)
     526           0 :                                 return -1;
     527           1 :                         force_failure = 1;
     528           1 :                         crypto_tlv = NULL; /* do not include Cryptobinding TLV
     529             :                                             * in response, if the received
     530             :                                             * cryptobinding was invalid. */
     531             :                 }
     532           1 :         } else if (!crypto_tlv && data->crypto_binding == REQUIRE_BINDING) {
     533           0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
     534           0 :                 return -1;
     535             :         }
     536             : 
     537           8 :         if (result_tlv) {
     538             :                 int status, resp_status;
     539           8 :                 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
     540             :                             result_tlv, result_tlv_len);
     541           8 :                 if (result_tlv_len < 2) {
     542           0 :                         wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
     543             :                                    "(len=%lu)",
     544             :                                    (unsigned long) result_tlv_len);
     545           0 :                         return -1;
     546             :                 }
     547           8 :                 status = WPA_GET_BE16(result_tlv);
     548           8 :                 if (status == EAP_TLV_RESULT_SUCCESS) {
     549           8 :                         wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
     550             :                                    "- EAP-TLV/Phase2 Completed");
     551           8 :                         if (force_failure) {
     552           1 :                                 wpa_printf(MSG_INFO, "EAP-TLV: Earlier failure"
     553             :                                            " - force failed Phase 2");
     554           1 :                                 resp_status = EAP_TLV_RESULT_FAILURE;
     555           1 :                                 ret->decision = DECISION_FAIL;
     556             :                         } else {
     557           7 :                                 resp_status = EAP_TLV_RESULT_SUCCESS;
     558           7 :                                 ret->decision = DECISION_UNCOND_SUCC;
     559             :                         }
     560           0 :                 } else if (status == EAP_TLV_RESULT_FAILURE) {
     561           0 :                         wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
     562           0 :                         resp_status = EAP_TLV_RESULT_FAILURE;
     563           0 :                         ret->decision = DECISION_FAIL;
     564             :                 } else {
     565           0 :                         wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
     566             :                                    "Status %d", status);
     567           0 :                         resp_status = EAP_TLV_RESULT_FAILURE;
     568           0 :                         ret->decision = DECISION_FAIL;
     569             :                 }
     570           8 :                 ret->methodState = METHOD_DONE;
     571             : 
     572          16 :                 *resp = eap_tlv_build_result(sm, data, crypto_tlv != NULL,
     573           8 :                                              eap_get_id(req), resp_status);
     574             :         }
     575             : 
     576           8 :         return 0;
     577             : }
     578             : 
     579             : 
     580         112 : static int eap_peap_phase2_request(struct eap_sm *sm,
     581             :                                    struct eap_peap_data *data,
     582             :                                    struct eap_method_ret *ret,
     583             :                                    struct wpabuf *req,
     584             :                                    struct wpabuf **resp)
     585             : {
     586         112 :         struct eap_hdr *hdr = wpabuf_mhead(req);
     587         112 :         size_t len = be_to_host16(hdr->length);
     588             :         u8 *pos;
     589             :         struct eap_method_ret iret;
     590         112 :         struct eap_peer_config *config = eap_get_config(sm);
     591             : 
     592         112 :         if (len <= sizeof(struct eap_hdr)) {
     593           0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: too short "
     594             :                            "Phase 2 request (len=%lu)", (unsigned long) len);
     595           0 :                 return -1;
     596             :         }
     597         112 :         pos = (u8 *) (hdr + 1);
     598         112 :         wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
     599         112 :         switch (*pos) {
     600             :         case EAP_TYPE_IDENTITY:
     601          31 :                 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
     602          31 :                 break;
     603             :         case EAP_TYPE_TLV:
     604           8 :                 os_memset(&iret, 0, sizeof(iret));
     605          16 :                 if (eap_tlv_process(sm, data, &iret, req, resp,
     606           8 :                                     data->phase2_eap_started &&
     607           8 :                                     !data->phase2_eap_success)) {
     608           0 :                         ret->methodState = METHOD_DONE;
     609           0 :                         ret->decision = DECISION_FAIL;
     610           0 :                         return -1;
     611             :                 }
     612           8 :                 if (iret.methodState == METHOD_DONE ||
     613           0 :                     iret.methodState == METHOD_MAY_CONT) {
     614           8 :                         ret->methodState = iret.methodState;
     615           8 :                         ret->decision = iret.decision;
     616           8 :                         data->phase2_success = 1;
     617             :                 }
     618           8 :                 break;
     619             :         case EAP_TYPE_EXPANDED:
     620             : #ifdef EAP_TNC
     621           3 :                 if (data->soh) {
     622             :                         const u8 *epos;
     623             :                         size_t eleft;
     624             : 
     625           3 :                         epos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21,
     626             :                                                 req, &eleft);
     627           3 :                         if (epos) {
     628             :                                 struct wpabuf *buf;
     629           3 :                                 wpa_printf(MSG_DEBUG,
     630             :                                            "EAP-PEAP: SoH EAP Extensions");
     631           3 :                                 buf = tncc_process_soh_request(data->soh,
     632             :                                                                epos, eleft);
     633           3 :                                 if (buf) {
     634           3 :                                         *resp = eap_msg_alloc(
     635             :                                                 EAP_VENDOR_MICROSOFT, 0x21,
     636             :                                                 wpabuf_len(buf),
     637             :                                                 EAP_CODE_RESPONSE,
     638           3 :                                                 hdr->identifier);
     639           3 :                                         if (*resp == NULL) {
     640           0 :                                                 ret->methodState = METHOD_DONE;
     641           0 :                                                 ret->decision = DECISION_FAIL;
     642           0 :                                                 return -1;
     643             :                                         }
     644           3 :                                         wpabuf_put_buf(*resp, buf);
     645           3 :                                         wpabuf_free(buf);
     646           3 :                                         break;
     647             :                                 }
     648             :                         }
     649             :                 }
     650             : #endif /* EAP_TNC */
     651             :                 /* fall through */
     652             :         default:
     653         140 :                 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
     654          70 :                     data->phase2_type.method == EAP_TYPE_NONE) {
     655             :                         size_t i;
     656          70 :                         for (i = 0; i < data->num_phase2_types; i++) {
     657          33 :                                 if (data->phase2_types[i].vendor !=
     658          33 :                                     EAP_VENDOR_IETF ||
     659          33 :                                     data->phase2_types[i].method != *pos)
     660           6 :                                         continue;
     661             : 
     662          27 :                                 data->phase2_type.vendor =
     663          27 :                                         data->phase2_types[i].vendor;
     664          27 :                                 data->phase2_type.method =
     665          27 :                                         data->phase2_types[i].method;
     666          27 :                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Selected "
     667             :                                            "Phase 2 EAP vendor %d method %d",
     668             :                                            data->phase2_type.vendor,
     669             :                                            data->phase2_type.method);
     670          27 :                                 break;
     671             :                         }
     672             :                 }
     673         138 :                 if (*pos != data->phase2_type.method ||
     674          68 :                     *pos == EAP_TYPE_NONE) {
     675           2 :                         if (eap_peer_tls_phase2_nak(data->phase2_types,
     676             :                                                     data->num_phase2_types,
     677             :                                                     hdr, resp))
     678           0 :                                 return -1;
     679           2 :                         return 0;
     680             :                 }
     681             : 
     682          68 :                 if (data->phase2_priv == NULL) {
     683          27 :                         data->phase2_method = eap_peer_get_eap_method(
     684             :                                 data->phase2_type.vendor,
     685          27 :                                 data->phase2_type.method);
     686          27 :                         if (data->phase2_method) {
     687          27 :                                 sm->init_phase2 = 1;
     688          27 :                                 data->phase2_priv =
     689          27 :                                         data->phase2_method->init(sm);
     690          27 :                                 sm->init_phase2 = 0;
     691             :                         }
     692             :                 }
     693          68 :                 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
     694           0 :                         wpa_printf(MSG_INFO, "EAP-PEAP: failed to initialize "
     695           0 :                                    "Phase 2 EAP method %d", *pos);
     696           0 :                         ret->methodState = METHOD_DONE;
     697           0 :                         ret->decision = DECISION_FAIL;
     698           0 :                         return -1;
     699             :                 }
     700          68 :                 data->phase2_eap_started = 1;
     701          68 :                 os_memset(&iret, 0, sizeof(iret));
     702          68 :                 *resp = data->phase2_method->process(sm, data->phase2_priv,
     703             :                                                      &iret, req);
     704         106 :                 if ((iret.methodState == METHOD_DONE ||
     705         104 :                      iret.methodState == METHOD_MAY_CONT) &&
     706         105 :                     (iret.decision == DECISION_UNCOND_SUCC ||
     707          39 :                      iret.decision == DECISION_COND_SUCC)) {
     708          30 :                         data->phase2_eap_success = 1;
     709          30 :                         data->phase2_success = 1;
     710             :                 }
     711          68 :                 break;
     712             :         }
     713             : 
     714         112 :         if (*resp == NULL &&
     715           5 :             (config->pending_req_identity || config->pending_req_password ||
     716           1 :              config->pending_req_otp || config->pending_req_new_password)) {
     717           2 :                 wpabuf_free(data->pending_phase2_req);
     718           2 :                 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
     719             :         }
     720             : 
     721         110 :         return 0;
     722             : }
     723             : 
     724             : 
     725         135 : static int eap_peap_decrypt(struct eap_sm *sm, struct eap_peap_data *data,
     726             :                             struct eap_method_ret *ret,
     727             :                             const struct eap_hdr *req,
     728             :                             const struct wpabuf *in_data,
     729             :                             struct wpabuf **out_data)
     730             : {
     731         135 :         struct wpabuf *in_decrypted = NULL;
     732         135 :         int res, skip_change = 0;
     733             :         struct eap_hdr *hdr, *rhdr;
     734         135 :         struct wpabuf *resp = NULL;
     735             :         size_t len;
     736             : 
     737         135 :         wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
     738             :                    " Phase 2", (unsigned long) wpabuf_len(in_data));
     739             : 
     740         135 :         if (data->pending_phase2_req) {
     741           2 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 request - "
     742             :                            "skip decryption and use old data");
     743             :                 /* Clear TLS reassembly state. */
     744           2 :                 eap_peer_tls_reset_input(&data->ssl);
     745           2 :                 in_decrypted = data->pending_phase2_req;
     746           2 :                 data->pending_phase2_req = NULL;
     747           2 :                 skip_change = 1;
     748           2 :                 goto continue_req;
     749             :         }
     750             : 
     751         133 :         if (wpabuf_len(in_data) == 0 && sm->workaround &&
     752           0 :             data->phase2_success) {
     753             :                 /*
     754             :                  * Cisco ACS seems to be using TLS ACK to terminate
     755             :                  * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
     756             :                  */
     757           0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Received TLS ACK, but "
     758             :                            "expected data - acknowledge with TLS ACK since "
     759             :                            "Phase 2 has been completed");
     760           0 :                 ret->decision = DECISION_COND_SUCC;
     761           0 :                 ret->methodState = METHOD_DONE;
     762           0 :                 return 1;
     763         133 :         } else if (wpabuf_len(in_data) == 0) {
     764             :                 /* Received TLS ACK - requesting more fragments */
     765           0 :                 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
     766             :                                             data->peap_version,
     767           0 :                                             req->identifier, NULL, out_data);
     768             :         }
     769             : 
     770         133 :         res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
     771         133 :         if (res)
     772           0 :                 return res;
     773             : 
     774             : continue_req:
     775         135 :         wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
     776             :                         in_decrypted);
     777             : 
     778         135 :         hdr = wpabuf_mhead(in_decrypted);
     779         158 :         if (wpabuf_len(in_decrypted) == 5 && hdr->code == EAP_CODE_REQUEST &&
     780          46 :             be_to_host16(hdr->length) == 5 &&
     781          23 :             eap_get_type(in_decrypted) == EAP_TYPE_IDENTITY) {
     782             :                 /* At least FreeRADIUS seems to send full EAP header with
     783             :                  * EAP Request Identity */
     784          23 :                 skip_change = 1;
     785             :         }
     786         220 :         if (wpabuf_len(in_decrypted) >= 5 && hdr->code == EAP_CODE_REQUEST &&
     787          85 :             eap_get_type(in_decrypted) == EAP_TYPE_TLV) {
     788           8 :                 skip_change = 1;
     789             :         }
     790             : 
     791         135 :         if (data->peap_version == 0 && !skip_change) {
     792             :                 struct eap_hdr *nhdr;
     793          27 :                 struct wpabuf *nmsg = wpabuf_alloc(sizeof(struct eap_hdr) +
     794          27 :                                                    wpabuf_len(in_decrypted));
     795          27 :                 if (nmsg == NULL) {
     796           0 :                         wpabuf_free(in_decrypted);
     797           0 :                         return 0;
     798             :                 }
     799          27 :                 nhdr = wpabuf_put(nmsg, sizeof(*nhdr));
     800          27 :                 wpabuf_put_buf(nmsg, in_decrypted);
     801          27 :                 nhdr->code = req->code;
     802          27 :                 nhdr->identifier = req->identifier;
     803          27 :                 nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
     804             :                                             wpabuf_len(in_decrypted));
     805             : 
     806          27 :                 wpabuf_free(in_decrypted);
     807          27 :                 in_decrypted = nmsg;
     808             :         }
     809             : 
     810         135 :         hdr = wpabuf_mhead(in_decrypted);
     811         135 :         if (wpabuf_len(in_decrypted) < sizeof(*hdr)) {
     812           0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
     813             :                            "EAP frame (len=%lu)",
     814             :                            (unsigned long) wpabuf_len(in_decrypted));
     815           0 :                 wpabuf_free(in_decrypted);
     816           0 :                 return 0;
     817             :         }
     818         135 :         len = be_to_host16(hdr->length);
     819         135 :         if (len > wpabuf_len(in_decrypted)) {
     820           0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
     821             :                            "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
     822             :                            (unsigned long) wpabuf_len(in_decrypted),
     823             :                            (unsigned long) len);
     824           0 :                 wpabuf_free(in_decrypted);
     825           0 :                 return 0;
     826             :         }
     827         135 :         if (len < wpabuf_len(in_decrypted)) {
     828           0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Odd.. Phase 2 EAP header has "
     829             :                            "shorter length than full decrypted data "
     830             :                            "(%lu < %lu)",
     831             :                            (unsigned long) len,
     832             :                            (unsigned long) wpabuf_len(in_decrypted));
     833             :         }
     834         270 :         wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
     835         270 :                    "identifier=%d length=%lu", hdr->code, hdr->identifier,
     836             :                    (unsigned long) len);
     837         135 :         switch (hdr->code) {
     838             :         case EAP_CODE_REQUEST:
     839         112 :                 if (eap_peap_phase2_request(sm, data, ret, in_decrypted,
     840             :                                             &resp)) {
     841           0 :                         wpabuf_free(in_decrypted);
     842           0 :                         wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
     843             :                                    "processing failed");
     844           0 :                         return 0;
     845             :                 }
     846         112 :                 break;
     847             :         case EAP_CODE_SUCCESS:
     848          22 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
     849          22 :                 if (data->peap_version == 1) {
     850             :                         /* EAP-Success within TLS tunnel is used to indicate
     851             :                          * shutdown of the TLS channel. The authentication has
     852             :                          * been completed. */
     853          44 :                         if (data->phase2_eap_started &&
     854          22 :                             !data->phase2_eap_success) {
     855           0 :                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 "
     856             :                                            "Success used to indicate success, "
     857             :                                            "but Phase 2 EAP was not yet "
     858             :                                            "completed successfully");
     859           0 :                                 ret->methodState = METHOD_DONE;
     860           0 :                                 ret->decision = DECISION_FAIL;
     861           0 :                                 wpabuf_free(in_decrypted);
     862           0 :                                 return 0;
     863             :                         }
     864          22 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Version 1 - "
     865             :                                    "EAP-Success within TLS tunnel - "
     866             :                                    "authentication completed");
     867          22 :                         ret->decision = DECISION_UNCOND_SUCC;
     868          22 :                         ret->methodState = METHOD_DONE;
     869          22 :                         data->phase2_success = 1;
     870          22 :                         if (data->peap_outer_success == 2) {
     871          21 :                                 wpabuf_free(in_decrypted);
     872          21 :                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Use TLS ACK "
     873             :                                            "to finish authentication");
     874          21 :                                 return 1;
     875           1 :                         } else if (data->peap_outer_success == 1) {
     876             :                                 /* Reply with EAP-Success within the TLS
     877             :                                  * channel to complete the authentication. */
     878           1 :                                 resp = wpabuf_alloc(sizeof(struct eap_hdr));
     879           1 :                                 if (resp) {
     880           1 :                                         rhdr = wpabuf_put(resp, sizeof(*rhdr));
     881           1 :                                         rhdr->code = EAP_CODE_SUCCESS;
     882           1 :                                         rhdr->identifier = hdr->identifier;
     883           1 :                                         rhdr->length =
     884           1 :                                                 host_to_be16(sizeof(*rhdr));
     885             :                                 }
     886             :                         } else {
     887             :                                 /* No EAP-Success expected for Phase 1 (outer,
     888             :                                  * unencrypted auth), so force EAP state
     889             :                                  * machine to SUCCESS state. */
     890           0 :                                 sm->peap_done = TRUE;
     891             :                         }
     892             :                 } else {
     893             :                         /* FIX: ? */
     894             :                 }
     895           1 :                 break;
     896             :         case EAP_CODE_FAILURE:
     897           1 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
     898           1 :                 ret->decision = DECISION_FAIL;
     899           1 :                 ret->methodState = METHOD_MAY_CONT;
     900           1 :                 ret->allowNotifications = FALSE;
     901             :                 /* Reply with EAP-Failure within the TLS channel to complete
     902             :                  * failure reporting. */
     903           1 :                 resp = wpabuf_alloc(sizeof(struct eap_hdr));
     904           1 :                 if (resp) {
     905           1 :                         rhdr = wpabuf_put(resp, sizeof(*rhdr));
     906           1 :                         rhdr->code = EAP_CODE_FAILURE;
     907           1 :                         rhdr->identifier = hdr->identifier;
     908           1 :                         rhdr->length = host_to_be16(sizeof(*rhdr));
     909             :                 }
     910           1 :                 break;
     911             :         default:
     912           0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
     913           0 :                            "Phase 2 EAP header", hdr->code);
     914           0 :                 break;
     915             :         }
     916             : 
     917         114 :         wpabuf_free(in_decrypted);
     918             : 
     919         114 :         if (resp) {
     920         112 :                 int skip_change2 = 0;
     921             :                 struct wpabuf *rmsg, buf;
     922             : 
     923         112 :                 wpa_hexdump_buf_key(MSG_DEBUG,
     924             :                                     "EAP-PEAP: Encrypting Phase 2 data", resp);
     925             :                 /* PEAP version changes */
     926         222 :                 if (wpabuf_len(resp) >= 5 &&
     927         220 :                     wpabuf_head_u8(resp)[0] == EAP_CODE_RESPONSE &&
     928         110 :                     eap_get_type(resp) == EAP_TYPE_TLV)
     929           8 :                         skip_change2 = 1;
     930         112 :                 rmsg = resp;
     931         112 :                 if (data->peap_version == 0 && !skip_change2) {
     932          27 :                         wpabuf_set(&buf, wpabuf_head_u8(resp) +
     933             :                                    sizeof(struct eap_hdr),
     934          27 :                                    wpabuf_len(resp) - sizeof(struct eap_hdr));
     935          27 :                         rmsg = &buf;
     936             :                 }
     937             : 
     938         112 :                 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
     939         112 :                                          data->peap_version, req->identifier,
     940             :                                          rmsg, out_data)) {
     941           0 :                         wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
     942             :                                    "a Phase 2 frame");
     943             :                 }
     944         112 :                 wpabuf_free(resp);
     945             :         }
     946             : 
     947         114 :         return 0;
     948             : }
     949             : 
     950             : 
     951         257 : static struct wpabuf * eap_peap_process(struct eap_sm *sm, void *priv,
     952             :                                         struct eap_method_ret *ret,
     953             :                                         const struct wpabuf *reqData)
     954             : {
     955             :         const struct eap_hdr *req;
     956             :         size_t left;
     957             :         int res;
     958             :         u8 flags, id;
     959             :         struct wpabuf *resp;
     960             :         const u8 *pos;
     961         257 :         struct eap_peap_data *data = priv;
     962             : 
     963         257 :         pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
     964             :                                         reqData, &left, &flags);
     965         257 :         if (pos == NULL)
     966           0 :                 return NULL;
     967         257 :         req = wpabuf_head(reqData);
     968         257 :         id = req->identifier;
     969             : 
     970         257 :         if (flags & EAP_TLS_FLAGS_START) {
     971          31 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
     972             :                            "ver=%d)", flags & EAP_TLS_VERSION_MASK,
     973             :                         data->peap_version);
     974          31 :                 if ((flags & EAP_TLS_VERSION_MASK) < data->peap_version)
     975           0 :                         data->peap_version = flags & EAP_TLS_VERSION_MASK;
     976          40 :                 if (data->force_peap_version >= 0 &&
     977           9 :                     data->force_peap_version != data->peap_version) {
     978           0 :                         wpa_printf(MSG_WARNING, "EAP-PEAP: Failed to select "
     979             :                                    "forced PEAP version %d",
     980             :                                    data->force_peap_version);
     981           0 :                         ret->methodState = METHOD_DONE;
     982           0 :                         ret->decision = DECISION_FAIL;
     983           0 :                         ret->allowNotifications = FALSE;
     984           0 :                         return NULL;
     985             :                 }
     986          31 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Using PEAP version %d",
     987             :                            data->peap_version);
     988          31 :                 left = 0; /* make sure that this frame is empty, even though it
     989             :                            * should always be, anyway */
     990             :         }
     991             : 
     992         257 :         resp = NULL;
     993         396 :         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
     994         274 :             !data->resuming) {
     995             :                 struct wpabuf msg;
     996         135 :                 wpabuf_set(&msg, pos, left);
     997         135 :                 res = eap_peap_decrypt(sm, data, ret, req, &msg, &resp);
     998             :         } else {
     999         122 :                 res = eap_peer_tls_process_helper(sm, &data->ssl,
    1000             :                                                   EAP_TYPE_PEAP,
    1001             :                                                   data->peap_version, id, pos,
    1002             :                                                   left, &resp);
    1003             : 
    1004         122 :                 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
    1005             :                         char *label;
    1006          31 :                         wpa_printf(MSG_DEBUG,
    1007             :                                    "EAP-PEAP: TLS done, proceed to Phase 2");
    1008          31 :                         os_free(data->key_data);
    1009             :                         /* draft-josefsson-ppext-eap-tls-eap-05.txt
    1010             :                          * specifies that PEAPv1 would use "client PEAP
    1011             :                          * encryption" as the label. However, most existing
    1012             :                          * PEAPv1 implementations seem to be using the old
    1013             :                          * label, "client EAP encryption", instead. Use the old
    1014             :                          * label by default, but allow it to be configured with
    1015             :                          * phase1 parameter peaplabel=1. */
    1016          31 :                         if (data->force_new_label)
    1017           2 :                                 label = "client PEAP encryption";
    1018             :                         else
    1019          29 :                                 label = "client EAP encryption";
    1020          31 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
    1021             :                                    "key derivation", label);
    1022          31 :                         data->key_data =
    1023          31 :                                 eap_peer_tls_derive_key(sm, &data->ssl, label,
    1024             :                                                         EAP_TLS_KEY_LEN);
    1025          31 :                         if (data->key_data) {
    1026          31 :                                 wpa_hexdump_key(MSG_DEBUG, 
    1027             :                                                 "EAP-PEAP: Derived key",
    1028          31 :                                                 data->key_data,
    1029             :                                                 EAP_TLS_KEY_LEN);
    1030             :                         } else {
    1031           0 :                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
    1032             :                                            "derive key");
    1033             :                         }
    1034             : 
    1035          31 :                         os_free(data->session_id);
    1036          31 :                         data->session_id =
    1037          31 :                                 eap_peer_tls_derive_session_id(sm, &data->ssl,
    1038             :                                                                EAP_TYPE_PEAP,
    1039             :                                                                &data->id_len);
    1040          31 :                         if (data->session_id) {
    1041          62 :                                 wpa_hexdump(MSG_DEBUG,
    1042             :                                             "EAP-PEAP: Derived Session-Id",
    1043          31 :                                             data->session_id, data->id_len);
    1044             :                         } else {
    1045           0 :                                 wpa_printf(MSG_ERROR, "EAP-PEAP: Failed to "
    1046             :                                            "derive Session-Id");
    1047             :                         }
    1048             : 
    1049          31 :                         if (sm->workaround && data->resuming) {
    1050             :                                 /*
    1051             :                                  * At least few RADIUS servers (Aegis v1.1.6;
    1052             :                                  * but not v1.1.4; and Cisco ACS) seem to be
    1053             :                                  * terminating PEAPv1 (Aegis) or PEAPv0 (Cisco
    1054             :                                  * ACS) session resumption with outer
    1055             :                                  * EAP-Success. This does not seem to follow
    1056             :                                  * draft-josefsson-pppext-eap-tls-eap-05.txt
    1057             :                                  * section 4.2, so only allow this if EAP
    1058             :                                  * workarounds are enabled.
    1059             :                                  */
    1060           4 :                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Workaround - "
    1061             :                                            "allow outer EAP-Success to "
    1062             :                                            "terminate PEAP resumption");
    1063           4 :                                 ret->decision = DECISION_COND_SUCC;
    1064           4 :                                 data->phase2_success = 1;
    1065             :                         }
    1066             : 
    1067          31 :                         data->resuming = 0;
    1068             :                 }
    1069             : 
    1070         122 :                 if (res == 2) {
    1071             :                         struct wpabuf msg;
    1072             :                         /*
    1073             :                          * Application data included in the handshake message.
    1074             :                          */
    1075           0 :                         wpabuf_free(data->pending_phase2_req);
    1076           0 :                         data->pending_phase2_req = resp;
    1077           0 :                         resp = NULL;
    1078           0 :                         wpabuf_set(&msg, pos, left);
    1079           0 :                         res = eap_peap_decrypt(sm, data, ret, req, &msg,
    1080             :                                                &resp);
    1081             :                 }
    1082             :         }
    1083             : 
    1084         257 :         if (ret->methodState == METHOD_DONE) {
    1085          30 :                 ret->allowNotifications = FALSE;
    1086             :         }
    1087             : 
    1088         257 :         if (res == 1) {
    1089          80 :                 wpabuf_free(resp);
    1090          80 :                 return eap_peer_tls_build_ack(id, EAP_TYPE_PEAP,
    1091             :                                               data->peap_version);
    1092             :         }
    1093             : 
    1094         177 :         return resp;
    1095             : }
    1096             : 
    1097             : 
    1098           8 : static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
    1099             : {
    1100           8 :         struct eap_peap_data *data = priv;
    1101          16 :         return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
    1102           8 :                 data->phase2_success;
    1103             : }
    1104             : 
    1105             : 
    1106           4 : static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
    1107             : {
    1108           4 :         struct eap_peap_data *data = priv;
    1109           4 :         wpabuf_free(data->pending_phase2_req);
    1110           4 :         data->pending_phase2_req = NULL;
    1111           4 :         data->crypto_binding_used = 0;
    1112           4 : }
    1113             : 
    1114             : 
    1115           4 : static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
    1116             : {
    1117           4 :         struct eap_peap_data *data = priv;
    1118           4 :         os_free(data->key_data);
    1119           4 :         data->key_data = NULL;
    1120           4 :         os_free(data->session_id);
    1121           4 :         data->session_id = NULL;
    1122           4 :         if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
    1123           0 :                 os_free(data);
    1124           0 :                 return NULL;
    1125             :         }
    1126           8 :         if (data->phase2_priv && data->phase2_method &&
    1127           4 :             data->phase2_method->init_for_reauth)
    1128           1 :                 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
    1129           4 :         data->phase2_success = 0;
    1130           4 :         data->phase2_eap_success = 0;
    1131           4 :         data->phase2_eap_started = 0;
    1132           4 :         data->resuming = 1;
    1133           4 :         data->reauth = 1;
    1134           4 :         sm->peap_done = FALSE;
    1135           4 :         return priv;
    1136             : }
    1137             : 
    1138             : 
    1139          19 : static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
    1140             :                                size_t buflen, int verbose)
    1141             : {
    1142          19 :         struct eap_peap_data *data = priv;
    1143             :         int len, ret;
    1144             : 
    1145          19 :         len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
    1146          19 :         if (data->phase2_method) {
    1147          19 :                 ret = os_snprintf(buf + len, buflen - len,
    1148             :                                   "EAP-PEAPv%d Phase2 method=%s\n",
    1149             :                                   data->peap_version,
    1150          19 :                                   data->phase2_method->name);
    1151          19 :                 if (ret < 0 || (size_t) ret >= buflen - len)
    1152           0 :                         return len;
    1153          19 :                 len += ret;
    1154             :         }
    1155          19 :         return len;
    1156             : }
    1157             : 
    1158             : 
    1159         257 : static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
    1160             : {
    1161         257 :         struct eap_peap_data *data = priv;
    1162         257 :         return data->key_data != NULL && data->phase2_success;
    1163             : }
    1164             : 
    1165             : 
    1166          75 : static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
    1167             : {
    1168          75 :         struct eap_peap_data *data = priv;
    1169             :         u8 *key;
    1170             : 
    1171          75 :         if (data->key_data == NULL || !data->phase2_success)
    1172           0 :                 return NULL;
    1173             : 
    1174          75 :         key = os_malloc(EAP_TLS_KEY_LEN);
    1175          75 :         if (key == NULL)
    1176           0 :                 return NULL;
    1177             : 
    1178          75 :         *len = EAP_TLS_KEY_LEN;
    1179             : 
    1180          75 :         if (data->crypto_binding_used) {
    1181             :                 u8 csk[128];
    1182             :                 /*
    1183             :                  * Note: It looks like Microsoft implementation requires null
    1184             :                  * termination for this label while the one used for deriving
    1185             :                  * IPMK|CMK did not use null termination.
    1186             :                  */
    1187           6 :                 if (peap_prfplus(data->peap_version, data->ipmk, 40,
    1188             :                                  "Session Key Generating Function",
    1189             :                                  (u8 *) "\00", 1, csk, sizeof(csk)) < 0) {
    1190           0 :                         os_free(key);
    1191           0 :                         return NULL;
    1192             :                 }
    1193           6 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
    1194           6 :                 os_memcpy(key, csk, EAP_TLS_KEY_LEN);
    1195           6 :                 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
    1196             :                             key, EAP_TLS_KEY_LEN);
    1197             :         } else
    1198          69 :                 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
    1199             : 
    1200          75 :         return key;
    1201             : }
    1202             : 
    1203             : 
    1204          75 : static u8 * eap_peap_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
    1205             : {
    1206          75 :         struct eap_peap_data *data = priv;
    1207             :         u8 *id;
    1208             : 
    1209          75 :         if (data->session_id == NULL || !data->phase2_success)
    1210           0 :                 return NULL;
    1211             : 
    1212          75 :         id = os_malloc(data->id_len);
    1213          75 :         if (id == NULL)
    1214           0 :                 return NULL;
    1215             : 
    1216          75 :         *len = data->id_len;
    1217          75 :         os_memcpy(id, data->session_id, data->id_len);
    1218             : 
    1219          75 :         return id;
    1220             : }
    1221             : 
    1222             : 
    1223           4 : int eap_peer_peap_register(void)
    1224             : {
    1225             :         struct eap_method *eap;
    1226             :         int ret;
    1227             : 
    1228           4 :         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
    1229             :                                     EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
    1230           4 :         if (eap == NULL)
    1231           0 :                 return -1;
    1232             : 
    1233           4 :         eap->init = eap_peap_init;
    1234           4 :         eap->deinit = eap_peap_deinit;
    1235           4 :         eap->process = eap_peap_process;
    1236           4 :         eap->isKeyAvailable = eap_peap_isKeyAvailable;
    1237           4 :         eap->getKey = eap_peap_getKey;
    1238           4 :         eap->get_status = eap_peap_get_status;
    1239           4 :         eap->has_reauth_data = eap_peap_has_reauth_data;
    1240           4 :         eap->deinit_for_reauth = eap_peap_deinit_for_reauth;
    1241           4 :         eap->init_for_reauth = eap_peap_init_for_reauth;
    1242           4 :         eap->getSessionId = eap_peap_get_session_id;
    1243             : 
    1244           4 :         ret = eap_peer_method_register(eap);
    1245           4 :         if (ret)
    1246           0 :                 eap_peer_method_free(eap);
    1247           4 :         return ret;
    1248             : }

Generated by: LCOV version 1.10