LCOV - code coverage report
Current view: top level - src/eap_server - eap_server_peap.c (source / functions) Hit Total Coverage
Test: hostapd/hlr_auc_gw (AS) hwsim test run 1388338050 Lines: 372 710 52.4 %
Date: 2013-12-29 Functions: 25 30 83.3 %
Branches: 135 328 41.2 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd / 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 "crypto/random.h"
      15                 :            : #include "eap_i.h"
      16                 :            : #include "eap_tls_common.h"
      17                 :            : #include "eap_common/eap_tlv_common.h"
      18                 :            : #include "eap_common/eap_peap_common.h"
      19                 :            : #include "tncs.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_reset(struct eap_sm *sm, void *priv);
      31                 :            : 
      32                 :            : 
      33                 :            : struct eap_peap_data {
      34                 :            :         struct eap_ssl_data ssl;
      35                 :            :         enum {
      36                 :            :                 START, PHASE1, PHASE1_ID2, PHASE2_START, PHASE2_ID,
      37                 :            :                 PHASE2_METHOD, PHASE2_SOH,
      38                 :            :                 PHASE2_TLV, SUCCESS_REQ, FAILURE_REQ, SUCCESS, FAILURE
      39                 :            :         } state;
      40                 :            : 
      41                 :            :         int peap_version;
      42                 :            :         int recv_version;
      43                 :            :         const struct eap_method *phase2_method;
      44                 :            :         void *phase2_priv;
      45                 :            :         int force_version;
      46                 :            :         struct wpabuf *pending_phase2_resp;
      47                 :            :         enum { TLV_REQ_NONE, TLV_REQ_SUCCESS, TLV_REQ_FAILURE } tlv_request;
      48                 :            :         int crypto_binding_sent;
      49                 :            :         int crypto_binding_used;
      50                 :            :         enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
      51                 :            :         u8 binding_nonce[32];
      52                 :            :         u8 ipmk[40];
      53                 :            :         u8 cmk[20];
      54                 :            :         u8 *phase2_key;
      55                 :            :         size_t phase2_key_len;
      56                 :            :         struct wpabuf *soh_response;
      57                 :            : };
      58                 :            : 
      59                 :            : 
      60                 :        108 : static const char * eap_peap_state_txt(int state)
      61                 :            : {
      62   [ +  +  -  +  :        108 :         switch (state) {
          +  +  -  +  +  
             -  +  -  - ]
      63                 :            :         case START:
      64                 :          9 :                 return "START";
      65                 :            :         case PHASE1:
      66                 :         18 :                 return "PHASE1";
      67                 :            :         case PHASE1_ID2:
      68                 :          0 :                 return "PHASE1_ID2";
      69                 :            :         case PHASE2_START:
      70                 :         18 :                 return "PHASE2_START";
      71                 :            :         case PHASE2_ID:
      72                 :         18 :                 return "PHASE2_ID";
      73                 :            :         case PHASE2_METHOD:
      74                 :         18 :                 return "PHASE2_METHOD";
      75                 :            :         case PHASE2_SOH:
      76                 :          0 :                 return "PHASE2_SOH";
      77                 :            :         case PHASE2_TLV:
      78                 :          4 :                 return "PHASE2_TLV";
      79                 :            :         case SUCCESS_REQ:
      80                 :         14 :                 return "SUCCESS_REQ";
      81                 :            :         case FAILURE_REQ:
      82                 :          0 :                 return "FAILURE_REQ";
      83                 :            :         case SUCCESS:
      84                 :          9 :                 return "SUCCESS";
      85                 :            :         case FAILURE:
      86                 :          0 :                 return "FAILURE";
      87                 :            :         default:
      88                 :        108 :                 return "Unknown?!";
      89                 :            :         }
      90                 :            : }
      91                 :            : 
      92                 :            : 
      93                 :         54 : static void eap_peap_state(struct eap_peap_data *data, int state)
      94                 :            : {
      95                 :         54 :         wpa_printf(MSG_DEBUG, "EAP-PEAP: %s -> %s",
      96                 :         54 :                    eap_peap_state_txt(data->state),
      97                 :            :                    eap_peap_state_txt(state));
      98                 :         54 :         data->state = state;
      99                 :         54 : }
     100                 :            : 
     101                 :            : 
     102                 :          0 : static struct wpabuf * eap_peapv2_tlv_eap_payload(struct wpabuf *buf)
     103                 :            : {
     104                 :            :         struct wpabuf *e;
     105                 :            :         struct eap_tlv_hdr *tlv;
     106                 :            : 
     107         [ #  # ]:          0 :         if (buf == NULL)
     108                 :          0 :                 return NULL;
     109                 :            : 
     110                 :            :         /* Encapsulate EAP packet in EAP-Payload TLV */
     111                 :          0 :         wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Add EAP-Payload TLV");
     112                 :          0 :         e = wpabuf_alloc(sizeof(*tlv) + wpabuf_len(buf));
     113         [ #  # ]:          0 :         if (e == NULL) {
     114                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Failed to allocate memory "
     115                 :            :                            "for TLV encapsulation");
     116                 :          0 :                 wpabuf_free(buf);
     117                 :          0 :                 return NULL;
     118                 :            :         }
     119                 :          0 :         tlv = wpabuf_put(e, sizeof(*tlv));
     120                 :          0 :         tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
     121                 :            :                                      EAP_TLV_EAP_PAYLOAD_TLV);
     122                 :          0 :         tlv->length = host_to_be16(wpabuf_len(buf));
     123                 :          0 :         wpabuf_put_buf(e, buf);
     124                 :          0 :         wpabuf_free(buf);
     125                 :          0 :         return e;
     126                 :            : }
     127                 :            : 
     128                 :            : 
     129                 :          9 : static void eap_peap_req_success(struct eap_sm *sm,
     130                 :            :                                  struct eap_peap_data *data)
     131                 :            : {
     132 [ +  - ][ -  + ]:          9 :         if (data->state == FAILURE || data->state == FAILURE_REQ) {
     133                 :          0 :                 eap_peap_state(data, FAILURE);
     134                 :          9 :                 return;
     135                 :            :         }
     136                 :            : 
     137         [ +  + ]:          9 :         if (data->peap_version == 0) {
     138                 :          2 :                 data->tlv_request = TLV_REQ_SUCCESS;
     139                 :          2 :                 eap_peap_state(data, PHASE2_TLV);
     140                 :            :         } else {
     141                 :          7 :                 eap_peap_state(data, SUCCESS_REQ);
     142                 :            :         }
     143                 :            : }
     144                 :            : 
     145                 :            : 
     146                 :          0 : static void eap_peap_req_failure(struct eap_sm *sm,
     147                 :            :                                  struct eap_peap_data *data)
     148                 :            : {
     149 [ #  # ][ #  # ]:          0 :         if (data->state == FAILURE || data->state == FAILURE_REQ ||
                 [ #  # ]
     150         [ #  # ]:          0 :             data->state == SUCCESS_REQ || data->tlv_request != TLV_REQ_NONE) {
     151                 :          0 :                 eap_peap_state(data, FAILURE);
     152                 :          0 :                 return;
     153                 :            :         }
     154                 :            : 
     155         [ #  # ]:          0 :         if (data->peap_version == 0) {
     156                 :          0 :                 data->tlv_request = TLV_REQ_FAILURE;
     157                 :          0 :                 eap_peap_state(data, PHASE2_TLV);
     158                 :            :         } else {
     159                 :          0 :                 eap_peap_state(data, FAILURE_REQ);
     160                 :            :         }
     161                 :            : }
     162                 :            : 
     163                 :            : 
     164                 :          9 : static void * eap_peap_init(struct eap_sm *sm)
     165                 :            : {
     166                 :            :         struct eap_peap_data *data;
     167                 :            : 
     168                 :          9 :         data = os_zalloc(sizeof(*data));
     169         [ -  + ]:          9 :         if (data == NULL)
     170                 :          0 :                 return NULL;
     171                 :          9 :         data->peap_version = EAP_PEAP_VERSION;
     172                 :          9 :         data->force_version = -1;
     173 [ +  - ][ -  + ]:          9 :         if (sm->user && sm->user->force_version >= 0) {
     174                 :          0 :                 data->force_version = sm->user->force_version;
     175                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: forcing version %d",
     176                 :            :                            data->force_version);
     177                 :          0 :                 data->peap_version = data->force_version;
     178                 :            :         }
     179                 :          9 :         data->state = START;
     180                 :          9 :         data->crypto_binding = OPTIONAL_BINDING;
     181                 :            : 
     182         [ -  + ]:          9 :         if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
     183                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
     184                 :          0 :                 eap_peap_reset(sm, data);
     185                 :          0 :                 return NULL;
     186                 :            :         }
     187                 :            : 
     188                 :          9 :         return data;
     189                 :            : }
     190                 :            : 
     191                 :            : 
     192                 :          9 : static void eap_peap_reset(struct eap_sm *sm, void *priv)
     193                 :            : {
     194                 :          9 :         struct eap_peap_data *data = priv;
     195         [ -  + ]:          9 :         if (data == NULL)
     196                 :          9 :                 return;
     197 [ -  + ][ #  # ]:          9 :         if (data->phase2_priv && data->phase2_method)
     198                 :          0 :                 data->phase2_method->reset(sm, data->phase2_priv);
     199                 :          9 :         eap_server_tls_ssl_deinit(sm, &data->ssl);
     200                 :          9 :         wpabuf_free(data->pending_phase2_resp);
     201                 :          9 :         os_free(data->phase2_key);
     202                 :          9 :         wpabuf_free(data->soh_response);
     203                 :          9 :         os_free(data);
     204                 :            : }
     205                 :            : 
     206                 :            : 
     207                 :          9 : static struct wpabuf * eap_peap_build_start(struct eap_sm *sm,
     208                 :            :                                             struct eap_peap_data *data, u8 id)
     209                 :            : {
     210                 :            :         struct wpabuf *req;
     211                 :            : 
     212                 :          9 :         req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PEAP, 1,
     213                 :            :                             EAP_CODE_REQUEST, id);
     214         [ -  + ]:          9 :         if (req == NULL) {
     215                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-PEAP: Failed to allocate memory for"
     216                 :            :                            " request");
     217                 :          0 :                 eap_peap_state(data, FAILURE);
     218                 :          0 :                 return NULL;
     219                 :            :         }
     220                 :            : 
     221                 :          9 :         wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->peap_version);
     222                 :            : 
     223                 :          9 :         eap_peap_state(data, PHASE1);
     224                 :            : 
     225                 :          9 :         return req;
     226                 :            : }
     227                 :            : 
     228                 :            : 
     229                 :         27 : static struct wpabuf * eap_peap_build_phase2_req(struct eap_sm *sm,
     230                 :            :                                                  struct eap_peap_data *data,
     231                 :            :                                                  u8 id)
     232                 :            : {
     233                 :            :         struct wpabuf *buf, *encr_req, msgbuf;
     234                 :            :         const u8 *req;
     235                 :            :         size_t req_len;
     236                 :            : 
     237 [ +  - ][ -  + ]:         27 :         if (data->phase2_method == NULL || data->phase2_priv == NULL) {
     238                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 method not ready");
     239                 :          0 :                 return NULL;
     240                 :            :         }
     241                 :         27 :         buf = data->phase2_method->buildReq(sm, data->phase2_priv, id);
     242 [ -  + ][ #  # ]:         27 :         if (data->peap_version >= 2 && buf)
     243                 :          0 :                 buf = eap_peapv2_tlv_eap_payload(buf);
     244         [ -  + ]:         27 :         if (buf == NULL)
     245                 :          0 :                 return NULL;
     246                 :            : 
     247                 :         27 :         req = wpabuf_head(buf);
     248                 :         27 :         req_len = wpabuf_len(buf);
     249                 :         27 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 data",
     250                 :            :                         req, req_len);
     251                 :            : 
     252 [ +  + ][ +  - ]:         27 :         if (data->peap_version == 0 &&
     253                 :          6 :             data->phase2_method->method != EAP_TYPE_TLV) {
     254                 :          6 :                 req += sizeof(struct eap_hdr);
     255                 :          6 :                 req_len -= sizeof(struct eap_hdr);
     256                 :            :         }
     257                 :            : 
     258                 :         27 :         wpabuf_set(&msgbuf, req, req_len);
     259                 :         27 :         encr_req = eap_server_tls_encrypt(sm, &data->ssl, &msgbuf);
     260                 :         27 :         wpabuf_free(buf);
     261                 :            : 
     262                 :         27 :         return encr_req;
     263                 :            : }
     264                 :            : 
     265                 :            : 
     266                 :            : #ifdef EAP_SERVER_TNC
     267                 :          0 : static struct wpabuf * eap_peap_build_phase2_soh(struct eap_sm *sm,
     268                 :            :                                                  struct eap_peap_data *data,
     269                 :            :                                                  u8 id)
     270                 :            : {
     271                 :            :         struct wpabuf *buf1, *buf, *encr_req, msgbuf;
     272                 :            :         const u8 *req;
     273                 :            :         size_t req_len;
     274                 :            : 
     275                 :          0 :         buf1 = tncs_build_soh_request();
     276         [ #  # ]:          0 :         if (buf1 == NULL)
     277                 :          0 :                 return NULL;
     278                 :            : 
     279                 :          0 :         buf = eap_msg_alloc(EAP_VENDOR_MICROSOFT, 0x21, wpabuf_len(buf1),
     280                 :            :                             EAP_CODE_REQUEST, id);
     281         [ #  # ]:          0 :         if (buf == NULL) {
     282                 :          0 :                 wpabuf_free(buf1);
     283                 :          0 :                 return NULL;
     284                 :            :         }
     285                 :          0 :         wpabuf_put_buf(buf, buf1);
     286                 :          0 :         wpabuf_free(buf1);
     287                 :            : 
     288                 :          0 :         req = wpabuf_head(buf);
     289                 :          0 :         req_len = wpabuf_len(buf);
     290                 :            : 
     291                 :          0 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 SOH data",
     292                 :            :                         req, req_len);
     293                 :            : 
     294                 :          0 :         req += sizeof(struct eap_hdr);
     295                 :          0 :         req_len -= sizeof(struct eap_hdr);
     296                 :          0 :         wpabuf_set(&msgbuf, req, req_len);
     297                 :            : 
     298                 :          0 :         encr_req = eap_server_tls_encrypt(sm, &data->ssl, &msgbuf);
     299                 :          0 :         wpabuf_free(buf);
     300                 :            : 
     301                 :          0 :         return encr_req;
     302                 :            : }
     303                 :            : #endif /* EAP_SERVER_TNC */
     304                 :            : 
     305                 :            : 
     306                 :          2 : static void eap_peap_get_isk(struct eap_peap_data *data,
     307                 :            :                              u8 *isk, size_t isk_len)
     308                 :            : {
     309                 :            :         size_t key_len;
     310                 :            : 
     311                 :          2 :         os_memset(isk, 0, isk_len);
     312         [ -  + ]:          2 :         if (data->phase2_key == NULL)
     313                 :          2 :                 return;
     314                 :            : 
     315                 :          2 :         key_len = data->phase2_key_len;
     316         [ -  + ]:          2 :         if (key_len > isk_len)
     317                 :          0 :                 key_len = isk_len;
     318                 :          2 :         os_memcpy(isk, data->phase2_key, key_len);
     319                 :            : }
     320                 :            : 
     321                 :            : 
     322                 :          2 : static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
     323                 :            : {
     324                 :            :         u8 *tk;
     325                 :            :         u8 isk[32], imck[60];
     326                 :            : 
     327                 :            :         /*
     328                 :            :          * Tunnel key (TK) is the first 60 octets of the key generated by
     329                 :            :          * phase 1 of PEAP (based on TLS).
     330                 :            :          */
     331                 :          2 :         tk = eap_server_tls_derive_key(sm, &data->ssl, "client EAP encryption",
     332                 :            :                                        EAP_TLS_KEY_LEN);
     333         [ -  + ]:          2 :         if (tk == NULL)
     334                 :          0 :                 return -1;
     335                 :          2 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
     336                 :            : 
     337                 :          2 :         eap_peap_get_isk(data, isk, sizeof(isk));
     338                 :          2 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
     339                 :            : 
     340                 :            :         /*
     341                 :            :          * IPMK Seed = "Inner Methods Compound Keys" | ISK
     342                 :            :          * TempKey = First 40 octets of TK
     343                 :            :          * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
     344                 :            :          * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
     345                 :            :          * in the end of the label just before ISK; is that just a typo?)
     346                 :            :          */
     347                 :          2 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
     348         [ -  + ]:          2 :         if (peap_prfplus(data->peap_version, tk, 40,
     349                 :            :                          "Inner Methods Compound Keys",
     350                 :            :                          isk, sizeof(isk), imck, sizeof(imck)) < 0) {
     351                 :          0 :                 os_free(tk);
     352                 :          0 :                 return -1;
     353                 :            :         }
     354                 :          2 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
     355                 :            :                         imck, sizeof(imck));
     356                 :            : 
     357                 :          2 :         os_free(tk);
     358                 :            : 
     359                 :            :         /* TODO: fast-connect: IPMK|CMK = TK */
     360                 :          2 :         os_memcpy(data->ipmk, imck, 40);
     361                 :          2 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
     362                 :          2 :         os_memcpy(data->cmk, imck + 40, 20);
     363                 :          2 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
     364                 :            : 
     365                 :          2 :         return 0;
     366                 :            : }
     367                 :            : 
     368                 :            : 
     369                 :          2 : static struct wpabuf * eap_peap_build_phase2_tlv(struct eap_sm *sm,
     370                 :            :                                                  struct eap_peap_data *data,
     371                 :            :                                                  u8 id)
     372                 :            : {
     373                 :            :         struct wpabuf *buf, *encr_req;
     374                 :            :         size_t mlen;
     375                 :            : 
     376                 :          2 :         mlen = 6; /* Result TLV */
     377         [ +  - ]:          2 :         if (data->crypto_binding != NO_BINDING)
     378                 :          2 :                 mlen += 60; /* Cryptobinding TLV */
     379                 :            : #ifdef EAP_SERVER_TNC
     380         [ -  + ]:          2 :         if (data->soh_response)
     381                 :          0 :                 mlen += wpabuf_len(data->soh_response);
     382                 :            : #endif /* EAP_SERVER_TNC */
     383                 :            : 
     384                 :          2 :         buf = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, mlen,
     385                 :            :                             EAP_CODE_REQUEST, id);
     386         [ -  + ]:          2 :         if (buf == NULL)
     387                 :          0 :                 return NULL;
     388                 :            : 
     389                 :          2 :         wpabuf_put_u8(buf, 0x80); /* Mandatory */
     390                 :          2 :         wpabuf_put_u8(buf, EAP_TLV_RESULT_TLV);
     391                 :            :         /* Length */
     392                 :          2 :         wpabuf_put_be16(buf, 2);
     393                 :            :         /* Status */
     394         [ +  - ]:          2 :         wpabuf_put_be16(buf, data->tlv_request == TLV_REQ_SUCCESS ?
     395                 :            :                         EAP_TLV_RESULT_SUCCESS : EAP_TLV_RESULT_FAILURE);
     396                 :            : 
     397 [ +  - ][ +  - ]:          2 :         if (data->peap_version == 0 && data->tlv_request == TLV_REQ_SUCCESS &&
                 [ +  - ]
     398                 :          2 :             data->crypto_binding != NO_BINDING) {
     399                 :            :                 u8 *mac;
     400                 :          2 :                 u8 eap_type = EAP_TYPE_PEAP;
     401                 :            :                 const u8 *addr[2];
     402                 :            :                 size_t len[2];
     403                 :            :                 u16 tlv_type;
     404                 :            : 
     405                 :            : #ifdef EAP_SERVER_TNC
     406         [ -  + ]:          2 :                 if (data->soh_response) {
     407                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Adding MS-SOH "
     408                 :            :                                    "Response TLV");
     409                 :          0 :                         wpabuf_put_buf(buf, data->soh_response);
     410                 :          0 :                         wpabuf_free(data->soh_response);
     411                 :          0 :                         data->soh_response = NULL;
     412                 :            :                 }
     413                 :            : #endif /* EAP_SERVER_TNC */
     414                 :            : 
     415   [ +  -  -  + ]:          4 :                 if (eap_peap_derive_cmk(sm, data) < 0 ||
     416                 :          2 :                     random_get_bytes(data->binding_nonce, 32)) {
     417                 :          0 :                         wpabuf_free(buf);
     418                 :          0 :                         return NULL;
     419                 :            :                 }
     420                 :            : 
     421                 :            :                 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
     422                 :          2 :                 addr[0] = wpabuf_put(buf, 0);
     423                 :          2 :                 len[0] = 60;
     424                 :          2 :                 addr[1] = &eap_type;
     425                 :          2 :                 len[1] = 1;
     426                 :            : 
     427                 :          2 :                 tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
     428         [ -  + ]:          2 :                 if (data->peap_version >= 2)
     429                 :          0 :                         tlv_type |= EAP_TLV_TYPE_MANDATORY;
     430                 :          2 :                 wpabuf_put_be16(buf, tlv_type);
     431                 :          2 :                 wpabuf_put_be16(buf, 56);
     432                 :            : 
     433                 :          2 :                 wpabuf_put_u8(buf, 0); /* Reserved */
     434                 :          2 :                 wpabuf_put_u8(buf, data->peap_version); /* Version */
     435                 :          2 :                 wpabuf_put_u8(buf, data->recv_version); /* RecvVersion */
     436                 :          2 :                 wpabuf_put_u8(buf, 0); /* SubType: 0 = Request, 1 = Response */
     437                 :          2 :                 wpabuf_put_data(buf, data->binding_nonce, 32); /* Nonce */
     438                 :          2 :                 mac = wpabuf_put(buf, 20); /* Compound_MAC */
     439                 :          2 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK",
     440                 :          2 :                             data->cmk, 20);
     441                 :          2 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
     442                 :          2 :                             addr[0], len[0]);
     443                 :          2 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
     444                 :          2 :                             addr[1], len[1]);
     445                 :          2 :                 hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac);
     446                 :          2 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC",
     447                 :            :                             mac, SHA1_MAC_LEN);
     448                 :          2 :                 data->crypto_binding_sent = 1;
     449                 :            :         }
     450                 :            : 
     451                 :          2 :         wpa_hexdump_buf_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 TLV data",
     452                 :            :                             buf);
     453                 :            : 
     454                 :          2 :         encr_req = eap_server_tls_encrypt(sm, &data->ssl, buf);
     455                 :          2 :         wpabuf_free(buf);
     456                 :            : 
     457                 :          2 :         return encr_req;
     458                 :            : }
     459                 :            : 
     460                 :            : 
     461                 :          7 : static struct wpabuf * eap_peap_build_phase2_term(struct eap_sm *sm,
     462                 :            :                                                   struct eap_peap_data *data,
     463                 :            :                                                   u8 id, int success)
     464                 :            : {
     465                 :            :         struct wpabuf *encr_req, msgbuf;
     466                 :            :         size_t req_len;
     467                 :            :         struct eap_hdr *hdr;
     468                 :            : 
     469                 :          7 :         req_len = sizeof(*hdr);
     470                 :          7 :         hdr = os_zalloc(req_len);
     471         [ -  + ]:          7 :         if (hdr == NULL)
     472                 :          0 :                 return NULL;
     473                 :            : 
     474         [ +  - ]:          7 :         hdr->code = success ? EAP_CODE_SUCCESS : EAP_CODE_FAILURE;
     475                 :          7 :         hdr->identifier = id;
     476                 :          7 :         hdr->length = host_to_be16(req_len);
     477                 :            : 
     478                 :          7 :         wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 data",
     479                 :            :                         (u8 *) hdr, req_len);
     480                 :            : 
     481                 :          7 :         wpabuf_set(&msgbuf, hdr, req_len);
     482                 :          7 :         encr_req = eap_server_tls_encrypt(sm, &data->ssl, &msgbuf);
     483                 :          7 :         os_free(hdr);
     484                 :            : 
     485                 :          7 :         return encr_req;
     486                 :            : }
     487                 :            : 
     488                 :            : 
     489                 :         72 : static struct wpabuf * eap_peap_buildReq(struct eap_sm *sm, void *priv, u8 id)
     490                 :            : {
     491                 :         72 :         struct eap_peap_data *data = priv;
     492                 :            : 
     493         [ -  + ]:         72 :         if (data->ssl.state == FRAG_ACK) {
     494                 :          0 :                 return eap_server_tls_build_ack(id, EAP_TYPE_PEAP,
     495                 :            :                                                 data->peap_version);
     496                 :            :         }
     497                 :            : 
     498         [ +  + ]:         72 :         if (data->ssl.state == WAIT_FRAG_ACK) {
     499                 :          9 :                 return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_PEAP,
     500                 :            :                                                 data->peap_version, id);
     501                 :            :         }
     502                 :            : 
     503   [ +  +  +  -  :         63 :         switch (data->state) {
             +  +  -  - ]
     504                 :            :         case START:
     505                 :          9 :                 return eap_peap_build_start(sm, data, id);
     506                 :            :         case PHASE1:
     507                 :            :         case PHASE1_ID2:
     508   [ +  -  +  + ]:         36 :                 if (data->peap_version < 2 &&
     509                 :         18 :                     tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
     510                 :          9 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase1 done, "
     511                 :            :                                    "starting Phase2");
     512                 :          9 :                         eap_peap_state(data, PHASE2_START);
     513                 :            :                 }
     514                 :         18 :                 break;
     515                 :            :         case PHASE2_ID:
     516                 :            :         case PHASE2_METHOD:
     517                 :         27 :                 wpabuf_free(data->ssl.tls_out);
     518                 :         27 :                 data->ssl.tls_out_pos = 0;
     519                 :         27 :                 data->ssl.tls_out = eap_peap_build_phase2_req(sm, data, id);
     520                 :         27 :                 break;
     521                 :            : #ifdef EAP_SERVER_TNC
     522                 :            :         case PHASE2_SOH:
     523                 :          0 :                 wpabuf_free(data->ssl.tls_out);
     524                 :          0 :                 data->ssl.tls_out_pos = 0;
     525                 :          0 :                 data->ssl.tls_out = eap_peap_build_phase2_soh(sm, data, id);
     526                 :          0 :                 break;
     527                 :            : #endif /* EAP_SERVER_TNC */
     528                 :            :         case PHASE2_TLV:
     529                 :          2 :                 wpabuf_free(data->ssl.tls_out);
     530                 :          2 :                 data->ssl.tls_out_pos = 0;
     531                 :          2 :                 data->ssl.tls_out = eap_peap_build_phase2_tlv(sm, data, id);
     532                 :          2 :                 break;
     533                 :            :         case SUCCESS_REQ:
     534                 :          7 :                 wpabuf_free(data->ssl.tls_out);
     535                 :          7 :                 data->ssl.tls_out_pos = 0;
     536                 :          7 :                 data->ssl.tls_out = eap_peap_build_phase2_term(sm, data, id,
     537                 :            :                                                                1);
     538                 :          7 :                 break;
     539                 :            :         case FAILURE_REQ:
     540                 :          0 :                 wpabuf_free(data->ssl.tls_out);
     541                 :          0 :                 data->ssl.tls_out_pos = 0;
     542                 :          0 :                 data->ssl.tls_out = eap_peap_build_phase2_term(sm, data, id,
     543                 :            :                                                                0);
     544                 :          0 :                 break;
     545                 :            :         default:
     546                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: %s - unexpected state %d",
     547                 :          0 :                            __func__, data->state);
     548                 :          0 :                 return NULL;
     549                 :            :         }
     550                 :            : 
     551                 :         72 :         return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_PEAP,
     552                 :            :                                         data->peap_version, id);
     553                 :            : }
     554                 :            : 
     555                 :            : 
     556                 :         72 : static Boolean eap_peap_check(struct eap_sm *sm, void *priv,
     557                 :            :                               struct wpabuf *respData)
     558                 :            : {
     559                 :            :         const u8 *pos;
     560                 :            :         size_t len;
     561                 :            : 
     562                 :         72 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PEAP, respData, &len);
     563 [ +  - ][ -  + ]:         72 :         if (pos == NULL || len < 1) {
     564                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Invalid frame");
     565                 :          0 :                 return TRUE;
     566                 :            :         }
     567                 :            : 
     568                 :         72 :         return FALSE;
     569                 :            : }
     570                 :            : 
     571                 :            : 
     572                 :         28 : static int eap_peap_phase2_init(struct eap_sm *sm, struct eap_peap_data *data,
     573                 :            :                                 EapType eap_type)
     574                 :            : {
     575 [ +  + ][ +  - ]:         28 :         if (data->phase2_priv && data->phase2_method) {
     576                 :         19 :                 data->phase2_method->reset(sm, data->phase2_priv);
     577                 :         19 :                 data->phase2_method = NULL;
     578                 :         19 :                 data->phase2_priv = NULL;
     579                 :            :         }
     580                 :         28 :         data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
     581                 :            :                                                         eap_type);
     582         [ +  + ]:         28 :         if (!data->phase2_method)
     583                 :          9 :                 return -1;
     584                 :            : 
     585                 :         19 :         sm->init_phase2 = 1;
     586                 :         19 :         data->phase2_priv = data->phase2_method->init(sm);
     587                 :         19 :         sm->init_phase2 = 0;
     588                 :         28 :         return 0;
     589                 :            : }
     590                 :            : 
     591                 :            : 
     592                 :          2 : static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
     593                 :            :                                           struct eap_peap_data *data,
     594                 :            :                                           const u8 *crypto_tlv,
     595                 :            :                                           size_t crypto_tlv_len)
     596                 :            : {
     597                 :            :         u8 buf[61], mac[SHA1_MAC_LEN];
     598                 :            :         const u8 *pos;
     599                 :            : 
     600         [ -  + ]:          2 :         if (crypto_tlv_len != 4 + 56) {
     601                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
     602                 :            :                            "length %d", (int) crypto_tlv_len);
     603                 :          0 :                 return -1;
     604                 :            :         }
     605                 :            : 
     606                 :          2 :         pos = crypto_tlv;
     607                 :          2 :         pos += 4; /* TLV header */
     608         [ -  + ]:          2 :         if (pos[1] != data->peap_version) {
     609                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
     610                 :            :                            "mismatch (was %d; expected %d)",
     611                 :          0 :                            pos[1], data->peap_version);
     612                 :          0 :                 return -1;
     613                 :            :         }
     614                 :            : 
     615         [ -  + ]:          2 :         if (pos[3] != 1) {
     616                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
     617                 :          0 :                            "SubType %d", pos[3]);
     618                 :          0 :                 return -1;
     619                 :            :         }
     620                 :          2 :         pos += 4;
     621                 :          2 :         pos += 32; /* Nonce */
     622                 :            : 
     623                 :            :         /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
     624                 :          2 :         os_memcpy(buf, crypto_tlv, 60);
     625                 :          2 :         os_memset(buf + 4 + 4 + 32, 0, 20); /* Compound_MAC */
     626                 :          2 :         buf[60] = EAP_TYPE_PEAP;
     627                 :          2 :         hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
     628                 :            : 
     629         [ -  + ]:          2 :         if (os_memcmp(mac, pos, SHA1_MAC_LEN) != 0) {
     630                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
     631                 :            :                            "cryptobinding TLV");
     632                 :          0 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK", data->cmk, 20);
     633                 :          0 :                 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding seed data",
     634                 :            :                             buf, 61);
     635                 :          0 :                 return -1;
     636                 :            :         }
     637                 :            : 
     638                 :          2 :         wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
     639                 :            : 
     640                 :          2 :         return 0;
     641                 :            : }
     642                 :            : 
     643                 :            : 
     644                 :          2 : static void eap_peap_process_phase2_tlv(struct eap_sm *sm,
     645                 :            :                                         struct eap_peap_data *data,
     646                 :            :                                         struct wpabuf *in_data)
     647                 :            : {
     648                 :            :         const u8 *pos;
     649                 :            :         size_t left;
     650                 :          2 :         const u8 *result_tlv = NULL, *crypto_tlv = NULL;
     651                 :          2 :         size_t result_tlv_len = 0, crypto_tlv_len = 0;
     652                 :            :         int tlv_type, mandatory, tlv_len;
     653                 :            : 
     654                 :          2 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, in_data, &left);
     655         [ -  + ]:          2 :         if (pos == NULL) {
     656                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid EAP-TLV header");
     657                 :          0 :                 return;
     658                 :            :         }
     659                 :            : 
     660                 :            :         /* Parse TLVs */
     661                 :          2 :         wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received TLVs", pos, left);
     662         [ +  + ]:          6 :         while (left >= 4) {
     663                 :          4 :                 mandatory = !!(pos[0] & 0x80);
     664                 :          4 :                 tlv_type = pos[0] & 0x3f;
     665                 :          4 :                 tlv_type = (tlv_type << 8) | pos[1];
     666                 :          4 :                 tlv_len = ((int) pos[2] << 8) | pos[3];
     667                 :          4 :                 pos += 4;
     668                 :          4 :                 left -= 4;
     669         [ -  + ]:          4 :                 if ((size_t) tlv_len > left) {
     670                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: TLV underrun "
     671                 :            :                                    "(tlv_len=%d left=%lu)", tlv_len,
     672                 :            :                                    (unsigned long) left);
     673                 :          0 :                         eap_peap_state(data, FAILURE);
     674                 :          0 :                         return;
     675                 :            :                 }
     676      [ +  +  - ]:          4 :                 switch (tlv_type) {
     677                 :            :                 case EAP_TLV_RESULT_TLV:
     678                 :          2 :                         result_tlv = pos;
     679                 :          2 :                         result_tlv_len = tlv_len;
     680                 :          2 :                         break;
     681                 :            :                 case EAP_TLV_CRYPTO_BINDING_TLV:
     682                 :          2 :                         crypto_tlv = pos;
     683                 :          2 :                         crypto_tlv_len = tlv_len;
     684                 :          2 :                         break;
     685                 :            :                 default:
     686         [ #  # ]:          0 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Unsupported TLV Type "
     687                 :            :                                    "%d%s", tlv_type,
     688                 :            :                                    mandatory ? " (mandatory)" : "");
     689         [ #  # ]:          0 :                         if (mandatory) {
     690                 :          0 :                                 eap_peap_state(data, FAILURE);
     691                 :          0 :                                 return;
     692                 :            :                         }
     693                 :            :                         /* Ignore this TLV, but process other TLVs */
     694                 :          0 :                         break;
     695                 :            :                 }
     696                 :            : 
     697                 :          4 :                 pos += tlv_len;
     698                 :          4 :                 left -= tlv_len;
     699                 :            :         }
     700         [ -  + ]:          2 :         if (left) {
     701                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Last TLV too short in "
     702                 :            :                            "Request (left=%lu)", (unsigned long) left);
     703                 :          0 :                 eap_peap_state(data, FAILURE);
     704                 :          0 :                 return;
     705                 :            :         }
     706                 :            : 
     707                 :            :         /* Process supported TLVs */
     708 [ +  - ][ +  - ]:          2 :         if (crypto_tlv && data->crypto_binding_sent) {
     709                 :          2 :                 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
     710                 :            :                             crypto_tlv, crypto_tlv_len);
     711         [ -  + ]:          2 :                 if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
     712                 :            :                                                    crypto_tlv_len + 4) < 0) {
     713                 :          0 :                         eap_peap_state(data, FAILURE);
     714                 :          0 :                         return;
     715                 :            :                 }
     716                 :          2 :                 data->crypto_binding_used = 1;
     717 [ #  # ][ #  # ]:          0 :         } else if (!crypto_tlv && data->crypto_binding_sent &&
                 [ #  # ]
     718                 :          0 :                    data->crypto_binding == REQUIRE_BINDING) {
     719                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
     720                 :          0 :                 eap_peap_state(data, FAILURE);
     721                 :          0 :                 return;
     722                 :            :         }
     723                 :            : 
     724         [ +  - ]:          2 :         if (result_tlv) {
     725                 :            :                 int status;
     726                 :            :                 const char *requested;
     727                 :            : 
     728                 :          2 :                 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Result TLV",
     729                 :            :                             result_tlv, result_tlv_len);
     730         [ -  + ]:          2 :                 if (result_tlv_len < 2) {
     731                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PEAP: Too short Result TLV "
     732                 :            :                                    "(len=%lu)",
     733                 :            :                                    (unsigned long) result_tlv_len);
     734                 :          0 :                         eap_peap_state(data, FAILURE);
     735                 :          0 :                         return;
     736                 :            :                 }
     737         [ +  - ]:          2 :                 requested = data->tlv_request == TLV_REQ_SUCCESS ? "Success" :
     738                 :            :                         "Failure";
     739                 :          2 :                 status = WPA_GET_BE16(result_tlv);
     740         [ +  - ]:          2 :                 if (status == EAP_TLV_RESULT_SUCCESS) {
     741                 :          2 :                         wpa_printf(MSG_INFO, "EAP-PEAP: TLV Result - Success "
     742                 :            :                                    "- requested %s", requested);
     743         [ +  - ]:          2 :                         if (data->tlv_request == TLV_REQ_SUCCESS)
     744                 :          2 :                                 eap_peap_state(data, SUCCESS);
     745                 :            :                         else
     746                 :          0 :                                 eap_peap_state(data, FAILURE);
     747                 :            :                         
     748         [ #  # ]:          0 :                 } else if (status == EAP_TLV_RESULT_FAILURE) {
     749                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PEAP: TLV Result - Failure "
     750                 :            :                                    "- requested %s", requested);
     751                 :          0 :                         eap_peap_state(data, FAILURE);
     752                 :            :                 } else {
     753                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PEAP: Unknown TLV Result "
     754                 :            :                                    "Status %d", status);
     755                 :          2 :                         eap_peap_state(data, FAILURE);
     756                 :            :                 }
     757                 :            :         }
     758                 :            : }
     759                 :            : 
     760                 :            : 
     761                 :            : #ifdef EAP_SERVER_TNC
     762                 :          0 : static void eap_peap_process_phase2_soh(struct eap_sm *sm,
     763                 :            :                                         struct eap_peap_data *data,
     764                 :            :                                         struct wpabuf *in_data)
     765                 :            : {
     766                 :            :         const u8 *pos, *vpos;
     767                 :            :         size_t left;
     768                 :          0 :         const u8 *soh_tlv = NULL;
     769                 :          0 :         size_t soh_tlv_len = 0;
     770                 :            :         int tlv_type, mandatory, tlv_len, vtlv_len;
     771                 :            :         u8 next_type;
     772                 :            :         u32 vendor_id;
     773                 :            : 
     774                 :          0 :         pos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21, in_data, &left);
     775         [ #  # ]:          0 :         if (pos == NULL) {
     776                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Not a valid SoH EAP "
     777                 :            :                            "Extensions Method header - skip TNC");
     778                 :          0 :                 goto auth_method;
     779                 :            :         }
     780                 :            : 
     781                 :            :         /* Parse TLVs */
     782                 :          0 :         wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received TLVs (SoH)", pos, left);
     783         [ #  # ]:          0 :         while (left >= 4) {
     784                 :          0 :                 mandatory = !!(pos[0] & 0x80);
     785                 :          0 :                 tlv_type = pos[0] & 0x3f;
     786                 :          0 :                 tlv_type = (tlv_type << 8) | pos[1];
     787                 :          0 :                 tlv_len = ((int) pos[2] << 8) | pos[3];
     788                 :          0 :                 pos += 4;
     789                 :          0 :                 left -= 4;
     790         [ #  # ]:          0 :                 if ((size_t) tlv_len > left) {
     791                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: TLV underrun "
     792                 :            :                                    "(tlv_len=%d left=%lu)", tlv_len,
     793                 :            :                                    (unsigned long) left);
     794                 :          0 :                         eap_peap_state(data, FAILURE);
     795                 :          0 :                         return;
     796                 :            :                 }
     797         [ #  # ]:          0 :                 switch (tlv_type) {
     798                 :            :                 case EAP_TLV_VENDOR_SPECIFIC_TLV:
     799         [ #  # ]:          0 :                         if (tlv_len < 4) {
     800                 :          0 :                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Too short "
     801                 :            :                                            "vendor specific TLV (len=%d)",
     802                 :            :                                            (int) tlv_len);
     803                 :          0 :                                 eap_peap_state(data, FAILURE);
     804                 :          0 :                                 return;
     805                 :            :                         }
     806                 :            : 
     807                 :          0 :                         vendor_id = WPA_GET_BE32(pos);
     808         [ #  # ]:          0 :                         if (vendor_id != EAP_VENDOR_MICROSOFT) {
     809         [ #  # ]:          0 :                                 if (mandatory) {
     810                 :          0 :                                         eap_peap_state(data, FAILURE);
     811                 :          0 :                                         return;
     812                 :            :                                 }
     813                 :          0 :                                 break;
     814                 :            :                         }
     815                 :            : 
     816                 :          0 :                         vpos = pos + 4;
     817                 :          0 :                         mandatory = !!(vpos[0] & 0x80);
     818                 :          0 :                         tlv_type = vpos[0] & 0x3f;
     819                 :          0 :                         tlv_type = (tlv_type << 8) | vpos[1];
     820                 :          0 :                         vtlv_len = ((int) vpos[2] << 8) | vpos[3];
     821                 :          0 :                         vpos += 4;
     822         [ #  # ]:          0 :                         if (vpos + vtlv_len > pos + left) {
     823                 :          0 :                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Vendor TLV "
     824                 :            :                                            "underrun");
     825                 :          0 :                                 eap_peap_state(data, FAILURE);
     826                 :          0 :                                 return;
     827                 :            :                         }
     828                 :            : 
     829         [ #  # ]:          0 :                         if (tlv_type == 1) {
     830                 :          0 :                                 soh_tlv = vpos;
     831                 :          0 :                                 soh_tlv_len = vtlv_len;
     832                 :          0 :                                 break;
     833                 :            :                         }
     834                 :            : 
     835         [ #  # ]:          0 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Unsupported MS-TLV "
     836                 :            :                                    "Type %d%s", tlv_type,
     837                 :            :                                    mandatory ? " (mandatory)" : "");
     838         [ #  # ]:          0 :                         if (mandatory) {
     839                 :          0 :                                 eap_peap_state(data, FAILURE);
     840                 :          0 :                                 return;
     841                 :            :                         }
     842                 :            :                         /* Ignore this TLV, but process other TLVs */
     843                 :          0 :                         break;
     844                 :            :                 default:
     845         [ #  # ]:          0 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Unsupported TLV Type "
     846                 :            :                                    "%d%s", tlv_type,
     847                 :            :                                    mandatory ? " (mandatory)" : "");
     848         [ #  # ]:          0 :                         if (mandatory) {
     849                 :          0 :                                 eap_peap_state(data, FAILURE);
     850                 :          0 :                                 return;
     851                 :            :                         }
     852                 :            :                         /* Ignore this TLV, but process other TLVs */
     853                 :          0 :                         break;
     854                 :            :                 }
     855                 :            : 
     856                 :          0 :                 pos += tlv_len;
     857                 :          0 :                 left -= tlv_len;
     858                 :            :         }
     859         [ #  # ]:          0 :         if (left) {
     860                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Last TLV too short in "
     861                 :            :                            "Request (left=%lu)", (unsigned long) left);
     862                 :          0 :                 eap_peap_state(data, FAILURE);
     863                 :          0 :                 return;
     864                 :            :         }
     865                 :            : 
     866                 :            :         /* Process supported TLVs */
     867         [ #  # ]:          0 :         if (soh_tlv) {
     868                 :          0 :                 int failure = 0;
     869                 :          0 :                 wpabuf_free(data->soh_response);
     870                 :          0 :                 data->soh_response = tncs_process_soh(soh_tlv, soh_tlv_len,
     871                 :            :                                                       &failure);
     872         [ #  # ]:          0 :                 if (failure) {
     873                 :          0 :                         eap_peap_state(data, FAILURE);
     874                 :          0 :                         return;
     875                 :            :                 }
     876                 :            :         } else {
     877                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: No SoH TLV received");
     878                 :          0 :                 eap_peap_state(data, FAILURE);
     879                 :          0 :                 return;
     880                 :            :         }
     881                 :            : 
     882                 :            : auth_method:
     883                 :          0 :         eap_peap_state(data, PHASE2_METHOD);
     884                 :          0 :         next_type = sm->user->methods[0].method;
     885                 :          0 :         sm->user_eap_method_index = 1;
     886                 :          0 :         wpa_printf(MSG_DEBUG, "EAP-PEAP: try EAP type %d", next_type);
     887                 :          0 :         eap_peap_phase2_init(sm, data, next_type);
     888                 :            : }
     889                 :            : #endif /* EAP_SERVER_TNC */
     890                 :            : 
     891                 :            : 
     892                 :         29 : static void eap_peap_process_phase2_response(struct eap_sm *sm,
     893                 :            :                                              struct eap_peap_data *data,
     894                 :            :                                              struct wpabuf *in_data)
     895                 :            : {
     896                 :         29 :         u8 next_type = EAP_TYPE_NONE;
     897                 :            :         const struct eap_hdr *hdr;
     898                 :            :         const u8 *pos;
     899                 :            :         size_t left;
     900                 :            : 
     901         [ +  + ]:         29 :         if (data->state == PHASE2_TLV) {
     902                 :          2 :                 eap_peap_process_phase2_tlv(sm, data, in_data);
     903                 :          2 :                 return;
     904                 :            :         }
     905                 :            : 
     906                 :            : #ifdef EAP_SERVER_TNC
     907         [ -  + ]:         27 :         if (data->state == PHASE2_SOH) {
     908                 :          0 :                 eap_peap_process_phase2_soh(sm, data, in_data);
     909                 :          0 :                 return;
     910                 :            :         }
     911                 :            : #endif /* EAP_SERVER_TNC */
     912                 :            : 
     913         [ -  + ]:         27 :         if (data->phase2_priv == NULL) {
     914                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: %s - Phase2 not "
     915                 :            :                            "initialized?!", __func__);
     916                 :          0 :                 return;
     917                 :            :         }
     918                 :            : 
     919                 :         27 :         hdr = wpabuf_head(in_data);
     920                 :         27 :         pos = (const u8 *) (hdr + 1);
     921                 :            : 
     922 [ +  - ][ +  + ]:         27 :         if (wpabuf_len(in_data) > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
     923                 :          1 :                 left = wpabuf_len(in_data) - sizeof(*hdr);
     924                 :          1 :                 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Phase2 type Nak'ed; "
     925                 :            :                             "allowed types", pos + 1, left - 1);
     926                 :          1 :                 eap_sm_process_nak(sm, pos + 1, left - 1);
     927 [ +  - ][ +  - ]:          1 :                 if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
                 [ +  - ]
     928                 :          1 :                     sm->user->methods[sm->user_eap_method_index].method !=
     929                 :            :                     EAP_TYPE_NONE) {
     930                 :          2 :                         next_type = sm->user->methods[
     931                 :          2 :                                 sm->user_eap_method_index++].method;
     932                 :          1 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: try EAP type %d",
     933                 :            :                                    next_type);
     934                 :            :                 } else {
     935                 :          0 :                         eap_peap_req_failure(sm, data);
     936                 :          0 :                         next_type = EAP_TYPE_NONE;
     937                 :            :                 }
     938                 :          1 :                 eap_peap_phase2_init(sm, data, next_type);
     939                 :          1 :                 return;
     940                 :            :         }
     941                 :            : 
     942         [ -  + ]:         26 :         if (data->phase2_method->check(sm, data->phase2_priv, in_data)) {
     943                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 check() asked to "
     944                 :            :                            "ignore the packet");
     945                 :          0 :                 return;
     946                 :            :         }
     947                 :            : 
     948                 :         26 :         data->phase2_method->process(sm, data->phase2_priv, in_data);
     949                 :            : 
     950         [ -  + ]:         26 :         if (sm->method_pending == METHOD_PENDING_WAIT) {
     951                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 method is in "
     952                 :            :                            "pending wait state - save decrypted response");
     953                 :          0 :                 wpabuf_free(data->pending_phase2_resp);
     954                 :          0 :                 data->pending_phase2_resp = wpabuf_dup(in_data);
     955                 :            :         }
     956                 :            : 
     957         [ +  + ]:         26 :         if (!data->phase2_method->isDone(sm, data->phase2_priv))
     958                 :          8 :                 return;
     959                 :            : 
     960         [ -  + ]:         18 :         if (!data->phase2_method->isSuccess(sm, data->phase2_priv)) {
     961                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 method failed");
     962                 :          0 :                 eap_peap_req_failure(sm, data);
     963                 :          0 :                 next_type = EAP_TYPE_NONE;
     964                 :          0 :                 eap_peap_phase2_init(sm, data, next_type);
     965                 :          0 :                 return;
     966                 :            :         }
     967                 :            : 
     968                 :         18 :         os_free(data->phase2_key);
     969         [ +  + ]:         18 :         if (data->phase2_method->getKey) {
     970                 :          8 :                 data->phase2_key = data->phase2_method->getKey(
     971                 :            :                         sm, data->phase2_priv, &data->phase2_key_len);
     972         [ -  + ]:          8 :                 if (data->phase2_key == NULL) {
     973                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 getKey "
     974                 :            :                                    "failed");
     975                 :          0 :                         eap_peap_req_failure(sm, data);
     976                 :          0 :                         eap_peap_phase2_init(sm, data, EAP_TYPE_NONE);
     977                 :          0 :                         return;
     978                 :            :                 }
     979                 :            :         }
     980                 :            : 
     981   [ +  +  -  - ]:         18 :         switch (data->state) {
     982                 :            :         case PHASE1_ID2:
     983                 :            :         case PHASE2_ID:
     984                 :            :         case PHASE2_SOH:
     985         [ -  + ]:          9 :                 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
     986                 :          0 :                         wpa_hexdump_ascii(MSG_DEBUG, "EAP_PEAP: Phase2 "
     987                 :            :                                           "Identity not found in the user "
     988                 :            :                                           "database",
     989                 :          0 :                                           sm->identity, sm->identity_len);
     990                 :          0 :                         eap_peap_req_failure(sm, data);
     991                 :          0 :                         next_type = EAP_TYPE_NONE;
     992                 :          0 :                         break;
     993                 :            :                 }
     994                 :            : 
     995                 :            : #ifdef EAP_SERVER_TNC
     996 [ +  - ][ -  + ]:          9 :                 if (data->state != PHASE2_SOH && sm->tnc &&
                 [ #  # ]
     997                 :          0 :                     data->peap_version == 0) {
     998                 :          0 :                         eap_peap_state(data, PHASE2_SOH);
     999                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Try to initialize "
    1000                 :            :                                    "TNC (NAP SOH)");
    1001                 :          0 :                         next_type = EAP_TYPE_NONE;
    1002                 :          0 :                         break;
    1003                 :            :                 }
    1004                 :            : #endif /* EAP_SERVER_TNC */
    1005                 :            : 
    1006                 :          9 :                 eap_peap_state(data, PHASE2_METHOD);
    1007                 :          9 :                 next_type = sm->user->methods[0].method;
    1008                 :          9 :                 sm->user_eap_method_index = 1;
    1009                 :          9 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: try EAP type %d", next_type);
    1010                 :          9 :                 break;
    1011                 :            :         case PHASE2_METHOD:
    1012                 :          9 :                 eap_peap_req_success(sm, data);
    1013                 :          9 :                 next_type = EAP_TYPE_NONE;
    1014                 :          9 :                 break;
    1015                 :            :         case FAILURE:
    1016                 :          0 :                 break;
    1017                 :            :         default:
    1018                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: %s - unexpected state %d",
    1019                 :          0 :                            __func__, data->state);
    1020                 :          0 :                 break;
    1021                 :            :         }
    1022                 :            : 
    1023                 :         29 :         eap_peap_phase2_init(sm, data, next_type);
    1024                 :            : }
    1025                 :            : 
    1026                 :            : 
    1027                 :         29 : static void eap_peap_process_phase2(struct eap_sm *sm,
    1028                 :            :                                     struct eap_peap_data *data,
    1029                 :            :                                     const struct wpabuf *respData,
    1030                 :            :                                     struct wpabuf *in_buf)
    1031                 :            : {
    1032                 :            :         struct wpabuf *in_decrypted;
    1033                 :            :         const struct eap_hdr *hdr;
    1034                 :            :         size_t len;
    1035                 :            : 
    1036                 :         29 :         wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
    1037                 :            :                    " Phase 2", (unsigned long) wpabuf_len(in_buf));
    1038                 :            : 
    1039         [ -  + ]:         29 :         if (data->pending_phase2_resp) {
    1040                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - "
    1041                 :            :                            "skip decryption and use old data");
    1042                 :          0 :                 eap_peap_process_phase2_response(sm, data,
    1043                 :            :                                                  data->pending_phase2_resp);
    1044                 :          0 :                 wpabuf_free(data->pending_phase2_resp);
    1045                 :          0 :                 data->pending_phase2_resp = NULL;
    1046                 :          0 :                 return;
    1047                 :            :         }
    1048                 :            : 
    1049                 :         29 :         in_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
    1050                 :            :                                               in_buf);
    1051         [ -  + ]:         29 :         if (in_decrypted == NULL) {
    1052                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to decrypt Phase 2 "
    1053                 :            :                            "data");
    1054                 :          0 :                 eap_peap_state(data, FAILURE);
    1055                 :          0 :                 return;
    1056                 :            :         }
    1057                 :            : 
    1058                 :         29 :         wpa_hexdump_buf_key(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
    1059                 :            :                             in_decrypted);
    1060                 :            : 
    1061 [ +  + ][ +  + ]:         35 :         if (data->peap_version == 0 && data->state != PHASE2_TLV) {
    1062                 :            :                 const struct eap_hdr *resp;
    1063                 :            :                 struct eap_hdr *nhdr;
    1064                 :          6 :                 struct wpabuf *nbuf =
    1065                 :          6 :                         wpabuf_alloc(sizeof(struct eap_hdr) +
    1066                 :          6 :                                      wpabuf_len(in_decrypted));
    1067         [ -  + ]:          6 :                 if (nbuf == NULL) {
    1068                 :          0 :                         wpabuf_free(in_decrypted);
    1069                 :          0 :                         return;
    1070                 :            :                 }
    1071                 :            : 
    1072                 :          6 :                 resp = wpabuf_head(respData);
    1073                 :          6 :                 nhdr = wpabuf_put(nbuf, sizeof(*nhdr));
    1074                 :          6 :                 nhdr->code = resp->code;
    1075                 :          6 :                 nhdr->identifier = resp->identifier;
    1076                 :          6 :                 nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
    1077                 :            :                                             wpabuf_len(in_decrypted));
    1078                 :          6 :                 wpabuf_put_buf(nbuf, in_decrypted);
    1079                 :          6 :                 wpabuf_free(in_decrypted);
    1080                 :            : 
    1081                 :          6 :                 in_decrypted = nbuf;
    1082         [ -  + ]:         23 :         } else if (data->peap_version >= 2) {
    1083                 :            :                 struct eap_tlv_hdr *tlv;
    1084                 :            :                 struct wpabuf *nmsg;
    1085                 :            : 
    1086         [ #  # ]:          0 :                 if (wpabuf_len(in_decrypted) < sizeof(*tlv) + sizeof(*hdr)) {
    1087                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PEAPv2: Too short Phase 2 "
    1088                 :            :                                    "EAP TLV");
    1089                 :          0 :                         wpabuf_free(in_decrypted);
    1090                 :          0 :                         return;
    1091                 :            :                 }
    1092                 :          0 :                 tlv = wpabuf_mhead(in_decrypted);
    1093         [ #  # ]:          0 :                 if ((be_to_host16(tlv->tlv_type) & EAP_TLV_TYPE_MASK) !=
    1094                 :            :                     EAP_TLV_EAP_PAYLOAD_TLV) {
    1095                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PEAPv2: Not an EAP TLV");
    1096                 :          0 :                         wpabuf_free(in_decrypted);
    1097                 :          0 :                         return;
    1098                 :            :                 }
    1099         [ #  # ]:          0 :                 if (sizeof(*tlv) + be_to_host16(tlv->length) >
    1100                 :          0 :                     wpabuf_len(in_decrypted)) {
    1101                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PEAPv2: Invalid EAP TLV "
    1102                 :            :                                    "length");
    1103                 :          0 :                         wpabuf_free(in_decrypted);
    1104                 :          0 :                         return;
    1105                 :            :                 }
    1106                 :          0 :                 hdr = (struct eap_hdr *) (tlv + 1);
    1107         [ #  # ]:          0 :                 if (be_to_host16(hdr->length) > be_to_host16(tlv->length)) {
    1108                 :          0 :                         wpa_printf(MSG_INFO, "EAP-PEAPv2: No room for full "
    1109                 :            :                                    "EAP packet in EAP TLV");
    1110                 :          0 :                         wpabuf_free(in_decrypted);
    1111                 :          0 :                         return;
    1112                 :            :                 }
    1113                 :            : 
    1114                 :          0 :                 nmsg = wpabuf_alloc(be_to_host16(hdr->length));
    1115         [ #  # ]:          0 :                 if (nmsg == NULL) {
    1116                 :          0 :                         wpabuf_free(in_decrypted);
    1117                 :          0 :                         return;
    1118                 :            :                 }
    1119                 :            : 
    1120                 :          0 :                 wpabuf_put_data(nmsg, hdr, be_to_host16(hdr->length));
    1121                 :          0 :                 wpabuf_free(in_decrypted);
    1122                 :          0 :                 in_decrypted = nmsg;
    1123                 :            :         }
    1124                 :            : 
    1125                 :         29 :         hdr = wpabuf_head(in_decrypted);
    1126         [ -  + ]:         29 :         if (wpabuf_len(in_decrypted) < (int) sizeof(*hdr)) {
    1127                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
    1128                 :            :                            "EAP frame (len=%lu)",
    1129                 :            :                            (unsigned long) wpabuf_len(in_decrypted));
    1130                 :          0 :                 wpabuf_free(in_decrypted);
    1131                 :          0 :                 eap_peap_req_failure(sm, data);
    1132                 :          0 :                 return;
    1133                 :            :         }
    1134                 :         29 :         len = be_to_host16(hdr->length);
    1135         [ -  + ]:         29 :         if (len > wpabuf_len(in_decrypted)) {
    1136                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
    1137                 :            :                            "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
    1138                 :            :                            (unsigned long) wpabuf_len(in_decrypted),
    1139                 :            :                            (unsigned long) len);
    1140                 :          0 :                 wpabuf_free(in_decrypted);
    1141                 :          0 :                 eap_peap_req_failure(sm, data);
    1142                 :          0 :                 return;
    1143                 :            :         }
    1144                 :         29 :         wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
    1145                 :         58 :                    "identifier=%d length=%lu", hdr->code, hdr->identifier,
    1146                 :            :                    (unsigned long) len);
    1147   [ +  -  -  - ]:         29 :         switch (hdr->code) {
    1148                 :            :         case EAP_CODE_RESPONSE:
    1149                 :         29 :                 eap_peap_process_phase2_response(sm, data, in_decrypted);
    1150                 :         29 :                 break;
    1151                 :            :         case EAP_CODE_SUCCESS:
    1152                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
    1153         [ #  # ]:          0 :                 if (data->state == SUCCESS_REQ) {
    1154                 :          0 :                         eap_peap_state(data, SUCCESS);
    1155                 :            :                 }
    1156                 :          0 :                 break;
    1157                 :            :         case EAP_CODE_FAILURE:
    1158                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
    1159                 :          0 :                 eap_peap_state(data, FAILURE);
    1160                 :          0 :                 break;
    1161                 :            :         default:
    1162                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
    1163                 :          0 :                            "Phase 2 EAP header", hdr->code);
    1164                 :          0 :                 break;
    1165                 :            :         }
    1166                 :            : 
    1167                 :         29 :         wpabuf_free(in_decrypted);
    1168                 :            : }
    1169                 :            : 
    1170                 :            : 
    1171                 :          0 : static int eap_peapv2_start_phase2(struct eap_sm *sm,
    1172                 :            :                                    struct eap_peap_data *data)
    1173                 :            : {
    1174                 :            :         struct wpabuf *buf, *buf2;
    1175                 :            : 
    1176                 :          0 :         wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Phase1 done, include first Phase2 "
    1177                 :            :                    "payload in the same message");
    1178                 :          0 :         eap_peap_state(data, PHASE1_ID2);
    1179         [ #  # ]:          0 :         if (eap_peap_phase2_init(sm, data, EAP_TYPE_IDENTITY))
    1180                 :          0 :                 return -1;
    1181                 :            : 
    1182                 :            :         /* TODO: which Id to use here? */
    1183                 :          0 :         buf = data->phase2_method->buildReq(sm, data->phase2_priv, 6);
    1184         [ #  # ]:          0 :         if (buf == NULL)
    1185                 :          0 :                 return -1;
    1186                 :            : 
    1187                 :          0 :         buf2 = eap_peapv2_tlv_eap_payload(buf);
    1188         [ #  # ]:          0 :         if (buf2 == NULL)
    1189                 :          0 :                 return -1;
    1190                 :            : 
    1191                 :          0 :         wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAPv2: Identity Request", buf2);
    1192                 :            : 
    1193                 :          0 :         buf = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
    1194                 :            :                                      buf2);
    1195                 :          0 :         wpabuf_free(buf2);
    1196                 :            : 
    1197         [ #  # ]:          0 :         if (buf == NULL) {
    1198                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PEAPv2: Failed to encrypt Phase 2 "
    1199                 :            :                            "data");
    1200                 :          0 :                 return -1;
    1201                 :            :         }
    1202                 :            : 
    1203                 :          0 :         wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAPv2: Encrypted Identity Request",
    1204                 :            :                         buf);
    1205                 :            : 
    1206                 :            :         /* Append TLS data into the pending buffer after the Server Finished */
    1207         [ #  # ]:          0 :         if (wpabuf_resize(&data->ssl.tls_out, wpabuf_len(buf)) < 0) {
    1208                 :          0 :                 wpabuf_free(buf);
    1209                 :          0 :                 return -1;
    1210                 :            :         }
    1211                 :          0 :         wpabuf_put_buf(data->ssl.tls_out, buf);
    1212                 :          0 :         wpabuf_free(buf);
    1213                 :            : 
    1214                 :          0 :         return 0;
    1215                 :            : }
    1216                 :            : 
    1217                 :            : 
    1218                 :         72 : static int eap_peap_process_version(struct eap_sm *sm, void *priv,
    1219                 :            :                                     int peer_version)
    1220                 :            : {
    1221                 :         72 :         struct eap_peap_data *data = priv;
    1222                 :            : 
    1223                 :         72 :         data->recv_version = peer_version;
    1224 [ -  + ][ #  # ]:         72 :         if (data->force_version >= 0 && peer_version != data->force_version) {
    1225                 :          0 :                 wpa_printf(MSG_INFO, "EAP-PEAP: peer did not select the forced"
    1226                 :            :                            " version (forced=%d peer=%d) - reject",
    1227                 :            :                            data->force_version, peer_version);
    1228                 :          0 :                 return -1;
    1229                 :            :         }
    1230         [ +  + ]:         72 :         if (peer_version < data->peap_version) {
    1231                 :          2 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: peer ver=%d, own ver=%d; "
    1232                 :            :                            "use version %d",
    1233                 :            :                            peer_version, data->peap_version, peer_version);
    1234                 :          2 :                 data->peap_version = peer_version;
    1235                 :            :         }
    1236                 :            : 
    1237                 :         72 :         return 0;
    1238                 :            : }
    1239                 :            : 
    1240                 :            : 
    1241                 :         63 : static void eap_peap_process_msg(struct eap_sm *sm, void *priv,
    1242                 :            :                                  const struct wpabuf *respData)
    1243                 :            : {
    1244                 :         63 :         struct eap_peap_data *data = priv;
    1245                 :            : 
    1246   [ +  +  +  +  :         63 :         switch (data->state) {
                   -  - ]
    1247                 :            :         case PHASE1:
    1248         [ -  + ]:         18 :                 if (eap_server_tls_phase1(sm, &data->ssl) < 0) {
    1249                 :          0 :                         eap_peap_state(data, FAILURE);
    1250                 :          0 :                         break;
    1251                 :            :                 }
    1252                 :            : 
    1253   [ -  +  #  # ]:         18 :                 if (data->peap_version >= 2 &&
    1254                 :          0 :                     tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
    1255         [ #  # ]:          0 :                         if (eap_peapv2_start_phase2(sm, data)) {
    1256                 :          0 :                                 eap_peap_state(data, FAILURE);
    1257                 :          0 :                                 break;
    1258                 :            :                         }
    1259                 :            :                 }
    1260                 :         18 :                 break;
    1261                 :            :         case PHASE2_START:
    1262                 :          9 :                 eap_peap_state(data, PHASE2_ID);
    1263                 :          9 :                 eap_peap_phase2_init(sm, data, EAP_TYPE_IDENTITY);
    1264                 :          9 :                 break;
    1265                 :            :         case PHASE1_ID2:
    1266                 :            :         case PHASE2_ID:
    1267                 :            :         case PHASE2_METHOD:
    1268                 :            :         case PHASE2_SOH:
    1269                 :            :         case PHASE2_TLV:
    1270                 :         29 :                 eap_peap_process_phase2(sm, data, respData, data->ssl.tls_in);
    1271                 :         29 :                 break;
    1272                 :            :         case SUCCESS_REQ:
    1273                 :          7 :                 eap_peap_state(data, SUCCESS);
    1274                 :          7 :                 break;
    1275                 :            :         case FAILURE_REQ:
    1276                 :          0 :                 eap_peap_state(data, FAILURE);
    1277                 :          0 :                 break;
    1278                 :            :         default:
    1279                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected state %d in %s",
    1280                 :          0 :                            data->state, __func__);
    1281                 :          0 :                 break;
    1282                 :            :         }
    1283                 :         63 : }
    1284                 :            : 
    1285                 :            : 
    1286                 :         72 : static void eap_peap_process(struct eap_sm *sm, void *priv,
    1287                 :            :                              struct wpabuf *respData)
    1288                 :            : {
    1289                 :         72 :         struct eap_peap_data *data = priv;
    1290         [ -  + ]:         72 :         if (eap_server_tls_process(sm, &data->ssl, respData, data,
    1291                 :            :                                    EAP_TYPE_PEAP, eap_peap_process_version,
    1292                 :            :                                    eap_peap_process_msg) < 0)
    1293                 :          0 :                 eap_peap_state(data, FAILURE);
    1294                 :         72 : }
    1295                 :            : 
    1296                 :            : 
    1297                 :         72 : static Boolean eap_peap_isDone(struct eap_sm *sm, void *priv)
    1298                 :            : {
    1299                 :         72 :         struct eap_peap_data *data = priv;
    1300 [ +  + ][ -  + ]:         72 :         return data->state == SUCCESS || data->state == FAILURE;
    1301                 :            : }
    1302                 :            : 
    1303                 :            : 
    1304                 :          9 : static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
    1305                 :            : {
    1306                 :          9 :         struct eap_peap_data *data = priv;
    1307                 :            :         u8 *eapKeyData;
    1308                 :            : 
    1309         [ -  + ]:          9 :         if (data->state != SUCCESS)
    1310                 :          0 :                 return NULL;
    1311                 :            : 
    1312         [ +  + ]:          9 :         if (data->crypto_binding_used) {
    1313                 :            :                 u8 csk[128];
    1314                 :            :                 /*
    1315                 :            :                  * Note: It looks like Microsoft implementation requires null
    1316                 :            :                  * termination for this label while the one used for deriving
    1317                 :            :                  * IPMK|CMK did not use null termination.
    1318                 :            :                  */
    1319         [ -  + ]:          2 :                 if (peap_prfplus(data->peap_version, data->ipmk, 40,
    1320                 :            :                                  "Session Key Generating Function",
    1321                 :            :                                  (u8 *) "\00", 1, csk, sizeof(csk)) < 0)
    1322                 :          0 :                         return NULL;
    1323                 :          2 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
    1324                 :          2 :                 eapKeyData = os_malloc(EAP_TLS_KEY_LEN);
    1325         [ +  - ]:          2 :                 if (eapKeyData) {
    1326                 :          2 :                         os_memcpy(eapKeyData, csk, EAP_TLS_KEY_LEN);
    1327                 :          2 :                         *len = EAP_TLS_KEY_LEN;
    1328                 :          2 :                         wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
    1329                 :            :                                     eapKeyData, EAP_TLS_KEY_LEN);
    1330                 :            :                 } else {
    1331                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to derive "
    1332                 :            :                                    "key");
    1333                 :            :                 }
    1334                 :            : 
    1335                 :          2 :                 return eapKeyData;
    1336                 :            :         }
    1337                 :            : 
    1338                 :            :         /* TODO: PEAPv1 - different label in some cases */
    1339                 :          7 :         eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
    1340                 :            :                                                "client EAP encryption",
    1341                 :            :                                                EAP_TLS_KEY_LEN);
    1342         [ +  - ]:          7 :         if (eapKeyData) {
    1343                 :          7 :                 *len = EAP_TLS_KEY_LEN;
    1344                 :          7 :                 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
    1345                 :            :                             eapKeyData, EAP_TLS_KEY_LEN);
    1346                 :            :         } else {
    1347                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to derive key");
    1348                 :            :         }
    1349                 :            : 
    1350                 :          9 :         return eapKeyData;
    1351                 :            : }
    1352                 :            : 
    1353                 :            : 
    1354                 :          9 : static Boolean eap_peap_isSuccess(struct eap_sm *sm, void *priv)
    1355                 :            : {
    1356                 :          9 :         struct eap_peap_data *data = priv;
    1357                 :          9 :         return data->state == SUCCESS;
    1358                 :            : }
    1359                 :            : 
    1360                 :            : 
    1361                 :          1 : int eap_server_peap_register(void)
    1362                 :            : {
    1363                 :            :         struct eap_method *eap;
    1364                 :            :         int ret;
    1365                 :            : 
    1366                 :          1 :         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
    1367                 :            :                                       EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
    1368         [ -  + ]:          1 :         if (eap == NULL)
    1369                 :          0 :                 return -1;
    1370                 :            : 
    1371                 :          1 :         eap->init = eap_peap_init;
    1372                 :          1 :         eap->reset = eap_peap_reset;
    1373                 :          1 :         eap->buildReq = eap_peap_buildReq;
    1374                 :          1 :         eap->check = eap_peap_check;
    1375                 :          1 :         eap->process = eap_peap_process;
    1376                 :          1 :         eap->isDone = eap_peap_isDone;
    1377                 :          1 :         eap->getKey = eap_peap_getKey;
    1378                 :          1 :         eap->isSuccess = eap_peap_isSuccess;
    1379                 :            : 
    1380                 :          1 :         ret = eap_server_method_register(eap);
    1381         [ -  + ]:          1 :         if (ret)
    1382                 :          0 :                 eap_server_method_free(eap);
    1383                 :          1 :         return ret;
    1384                 :            : }

Generated by: LCOV version 1.9