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 1388338050 Lines: 421 677 62.2 %
Date: 2013-12-29 Functions: 20 22 90.9 %
Branches: 185 361 51.2 %

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

Generated by: LCOV version 1.9