LCOV - code coverage report
Current view: top level - src/eap_server - eap_server_ttls.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 422 584 72.3 %
Date: 2014-03-02 Functions: 29 29 100.0 %
Branches: 194 309 62.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd / EAP-TTLS (RFC 5281)
       3                 :            :  * Copyright (c) 2004-2011, 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/ms_funcs.h"
      13                 :            : #include "crypto/sha1.h"
      14                 :            : #include "crypto/tls.h"
      15                 :            : #include "eap_server/eap_i.h"
      16                 :            : #include "eap_server/eap_tls_common.h"
      17                 :            : #include "eap_common/chap.h"
      18                 :            : #include "eap_common/eap_ttls.h"
      19                 :            : 
      20                 :            : 
      21                 :            : #define EAP_TTLS_VERSION 0
      22                 :            : 
      23                 :            : 
      24                 :            : static void eap_ttls_reset(struct eap_sm *sm, void *priv);
      25                 :            : 
      26                 :            : 
      27                 :            : struct eap_ttls_data {
      28                 :            :         struct eap_ssl_data ssl;
      29                 :            :         enum {
      30                 :            :                 START, PHASE1, PHASE2_START, PHASE2_METHOD,
      31                 :            :                 PHASE2_MSCHAPV2_RESP, SUCCESS, FAILURE
      32                 :            :         } state;
      33                 :            : 
      34                 :            :         int ttls_version;
      35                 :            :         const struct eap_method *phase2_method;
      36                 :            :         void *phase2_priv;
      37                 :            :         int mschapv2_resp_ok;
      38                 :            :         u8 mschapv2_auth_response[20];
      39                 :            :         u8 mschapv2_ident;
      40                 :            :         struct wpabuf *pending_phase2_eap_resp;
      41                 :            :         int tnc_started;
      42                 :            : };
      43                 :            : 
      44                 :            : 
      45                 :        734 : static const char * eap_ttls_state_txt(int state)
      46                 :            : {
      47   [ +  +  +  +  :        734 :         switch (state) {
             +  +  +  - ]
      48                 :            :         case START:
      49                 :        128 :                 return "START";
      50                 :            :         case PHASE1:
      51                 :        218 :                 return "PHASE1";
      52                 :            :         case PHASE2_START:
      53                 :        162 :                 return "PHASE2_START";
      54                 :            :         case PHASE2_METHOD:
      55                 :         24 :                 return "PHASE2_METHOD";
      56                 :            :         case PHASE2_MSCHAPV2_RESP:
      57                 :        112 :                 return "PHASE2_MSCHAPV2_RESP";
      58                 :            :         case SUCCESS:
      59                 :         79 :                 return "SUCCESS";
      60                 :            :         case FAILURE:
      61                 :         11 :                 return "FAILURE";
      62                 :            :         default:
      63                 :        734 :                 return "Unknown?!";
      64                 :            :         }
      65                 :            : }
      66                 :            : 
      67                 :            : 
      68                 :        367 : static void eap_ttls_state(struct eap_ttls_data *data, int state)
      69                 :            : {
      70                 :        367 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: %s -> %s",
      71                 :        367 :                    eap_ttls_state_txt(data->state),
      72                 :            :                    eap_ttls_state_txt(state));
      73                 :        367 :         data->state = state;
      74                 :        367 : }
      75                 :            : 
      76                 :            : 
      77                 :         80 : static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
      78                 :            :                              int mandatory, size_t len)
      79                 :            : {
      80                 :            :         struct ttls_avp_vendor *avp;
      81                 :            :         u8 flags;
      82                 :            :         size_t hdrlen;
      83                 :            : 
      84                 :         80 :         avp = (struct ttls_avp_vendor *) avphdr;
      85         [ +  - ]:         80 :         flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
      86         [ +  + ]:         80 :         if (vendor_id) {
      87                 :         56 :                 flags |= AVP_FLAGS_VENDOR;
      88                 :         56 :                 hdrlen = sizeof(*avp);
      89                 :         56 :                 avp->vendor_id = host_to_be32(vendor_id);
      90                 :            :         } else {
      91                 :         24 :                 hdrlen = sizeof(struct ttls_avp);
      92                 :            :         }
      93                 :            : 
      94                 :         80 :         avp->avp_code = host_to_be32(avp_code);
      95                 :         80 :         avp->avp_length = host_to_be32(((u32) flags << 24) |
      96                 :            :                                        ((u32) (hdrlen + len)));
      97                 :            : 
      98                 :         80 :         return avphdr + hdrlen;
      99                 :            : }
     100                 :            : 
     101                 :            : 
     102                 :         24 : static struct wpabuf * eap_ttls_avp_encapsulate(struct wpabuf *resp,
     103                 :            :                                                 u32 avp_code, int mandatory)
     104                 :            : {
     105                 :            :         struct wpabuf *avp;
     106                 :            :         u8 *pos;
     107                 :            : 
     108                 :         24 :         avp = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(resp) + 4);
     109         [ -  + ]:         24 :         if (avp == NULL) {
     110                 :          0 :                 wpabuf_free(resp);
     111                 :          0 :                 return NULL;
     112                 :            :         }
     113                 :            : 
     114                 :         24 :         pos = eap_ttls_avp_hdr(wpabuf_mhead(avp), avp_code, 0, mandatory,
     115                 :            :                                wpabuf_len(resp));
     116                 :         24 :         os_memcpy(pos, wpabuf_head(resp), wpabuf_len(resp));
     117                 :         24 :         pos += wpabuf_len(resp);
     118                 :         24 :         AVP_PAD((const u8 *) wpabuf_head(avp), pos);
     119                 :         24 :         wpabuf_free(resp);
     120                 :         24 :         wpabuf_put(avp, pos - (u8 *) wpabuf_head(avp));
     121                 :         24 :         return avp;
     122                 :            : }
     123                 :            : 
     124                 :            : 
     125                 :            : struct eap_ttls_avp {
     126                 :            :          /* Note: eap is allocated memory; caller is responsible for freeing
     127                 :            :           * it. All the other pointers are pointing to the packet data, i.e.,
     128                 :            :           * they must not be freed separately. */
     129                 :            :         u8 *eap;
     130                 :            :         size_t eap_len;
     131                 :            :         u8 *user_name;
     132                 :            :         size_t user_name_len;
     133                 :            :         u8 *user_password;
     134                 :            :         size_t user_password_len;
     135                 :            :         u8 *chap_challenge;
     136                 :            :         size_t chap_challenge_len;
     137                 :            :         u8 *chap_password;
     138                 :            :         size_t chap_password_len;
     139                 :            :         u8 *mschap_challenge;
     140                 :            :         size_t mschap_challenge_len;
     141                 :            :         u8 *mschap_response;
     142                 :            :         size_t mschap_response_len;
     143                 :            :         u8 *mschap2_response;
     144                 :            :         size_t mschap2_response_len;
     145                 :            : };
     146                 :            : 
     147                 :            : 
     148                 :        105 : static int eap_ttls_avp_parse(struct wpabuf *buf, struct eap_ttls_avp *parse)
     149                 :            : {
     150                 :            :         struct ttls_avp *avp;
     151                 :            :         u8 *pos;
     152                 :            :         int left;
     153                 :            : 
     154                 :        105 :         pos = wpabuf_mhead(buf);
     155                 :        105 :         left = wpabuf_len(buf);
     156                 :        105 :         os_memset(parse, 0, sizeof(*parse));
     157                 :            : 
     158         [ +  + ]:        345 :         while (left > 0) {
     159                 :        240 :                 u32 avp_code, avp_length, vendor_id = 0;
     160                 :            :                 u8 avp_flags, *dpos;
     161                 :            :                 size_t pad, dlen;
     162                 :        240 :                 avp = (struct ttls_avp *) pos;
     163                 :        240 :                 avp_code = be_to_host32(avp->avp_code);
     164                 :        240 :                 avp_length = be_to_host32(avp->avp_length);
     165                 :        240 :                 avp_flags = (avp_length >> 24) & 0xff;
     166                 :        240 :                 avp_length &= 0xffffff;
     167                 :        240 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
     168                 :            :                            "length=%d", (int) avp_code, avp_flags,
     169                 :            :                            (int) avp_length);
     170         [ -  + ]:        240 :                 if ((int) avp_length > left) {
     171                 :          0 :                         wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
     172                 :            :                                    "(len=%d, left=%d) - dropped",
     173                 :            :                                    (int) avp_length, left);
     174                 :          0 :                         goto fail;
     175                 :            :                 }
     176         [ -  + ]:        240 :                 if (avp_length < sizeof(*avp)) {
     177                 :          0 :                         wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length "
     178                 :            :                                    "%d", avp_length);
     179                 :          0 :                         goto fail;
     180                 :            :                 }
     181                 :        240 :                 dpos = (u8 *) (avp + 1);
     182                 :        240 :                 dlen = avp_length - sizeof(*avp);
     183         [ +  + ]:        240 :                 if (avp_flags & AVP_FLAGS_VENDOR) {
     184         [ -  + ]:        124 :                         if (dlen < 4) {
     185                 :          0 :                                 wpa_printf(MSG_WARNING, "EAP-TTLS: vendor AVP "
     186                 :            :                                            "underflow");
     187                 :          0 :                                 goto fail;
     188                 :            :                         }
     189                 :        124 :                         vendor_id = be_to_host32(* (be32 *) dpos);
     190                 :        124 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
     191                 :            :                                    (int) vendor_id);
     192                 :        124 :                         dpos += 4;
     193                 :        124 :                         dlen -= 4;
     194                 :            :                 }
     195                 :            : 
     196                 :        240 :                 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
     197                 :            : 
     198 [ +  + ][ +  + ]:        240 :                 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
     199                 :         36 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
     200         [ +  - ]:         72 :                         if (parse->eap == NULL) {
     201                 :         36 :                                 parse->eap = os_malloc(dlen);
     202         [ -  + ]:         36 :                                 if (parse->eap == NULL) {
     203                 :          0 :                                         wpa_printf(MSG_WARNING, "EAP-TTLS: "
     204                 :            :                                                    "failed to allocate memory "
     205                 :            :                                                    "for Phase 2 EAP data");
     206                 :          0 :                                         goto fail;
     207                 :            :                                 }
     208                 :         36 :                                 os_memcpy(parse->eap, dpos, dlen);
     209                 :         36 :                                 parse->eap_len = dlen;
     210                 :            :                         } else {
     211                 :          0 :                                 u8 *neweap = os_realloc(parse->eap,
     212                 :          0 :                                                         parse->eap_len + dlen);
     213         [ #  # ]:          0 :                                 if (neweap == NULL) {
     214                 :          0 :                                         wpa_printf(MSG_WARNING, "EAP-TTLS: "
     215                 :            :                                                    "failed to allocate memory "
     216                 :            :                                                    "for Phase 2 EAP data");
     217                 :          0 :                                         goto fail;
     218                 :            :                                 }
     219                 :          0 :                                 os_memcpy(neweap + parse->eap_len, dpos, dlen);
     220                 :          0 :                                 parse->eap = neweap;
     221                 :          0 :                                 parse->eap_len += dlen;
     222                 :            :                         }
     223 [ +  + ][ +  + ]:        204 :                 } else if (vendor_id == 0 &&
     224                 :            :                            avp_code == RADIUS_ATTR_USER_NAME) {
     225                 :         69 :                         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: User-Name",
     226                 :            :                                           dpos, dlen);
     227                 :         69 :                         parse->user_name = dpos;
     228                 :         69 :                         parse->user_name_len = dlen;
     229 [ +  + ][ +  + ]:        135 :                 } else if (vendor_id == 0 &&
     230                 :          3 :                            avp_code == RADIUS_ATTR_USER_PASSWORD) {
     231                 :          3 :                         u8 *password = dpos;
     232                 :          3 :                         size_t password_len = dlen;
     233 [ +  - ][ +  + ]:         27 :                         while (password_len > 0 &&
     234                 :         27 :                                password[password_len - 1] == '\0') {
     235                 :         24 :                                 password_len--;
     236                 :            :                         }
     237                 :          3 :                         wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: "
     238                 :            :                                               "User-Password (PAP)",
     239                 :            :                                               password, password_len);
     240                 :          3 :                         parse->user_password = password;
     241                 :          3 :                         parse->user_password_len = password_len;
     242 [ +  + ][ +  + ]:        132 :                 } else if (vendor_id == 0 &&
     243                 :            :                            avp_code == RADIUS_ATTR_CHAP_CHALLENGE) {
     244                 :          4 :                         wpa_hexdump(MSG_DEBUG,
     245                 :            :                                     "EAP-TTLS: CHAP-Challenge (CHAP)",
     246                 :            :                                     dpos, dlen);
     247                 :          4 :                         parse->chap_challenge = dpos;
     248                 :          4 :                         parse->chap_challenge_len = dlen;
     249 [ +  + ][ +  - ]:        128 :                 } else if (vendor_id == 0 &&
     250                 :            :                            avp_code == RADIUS_ATTR_CHAP_PASSWORD) {
     251                 :          4 :                         wpa_hexdump(MSG_DEBUG,
     252                 :            :                                     "EAP-TTLS: CHAP-Password (CHAP)",
     253                 :            :                                     dpos, dlen);
     254                 :          4 :                         parse->chap_password = dpos;
     255                 :          4 :                         parse->chap_password_len = dlen;
     256 [ +  - ][ +  + ]:        124 :                 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
     257                 :            :                            avp_code == RADIUS_ATTR_MS_CHAP_CHALLENGE) {
     258                 :         62 :                         wpa_hexdump(MSG_DEBUG,
     259                 :            :                                     "EAP-TTLS: MS-CHAP-Challenge",
     260                 :            :                                     dpos, dlen);
     261                 :         62 :                         parse->mschap_challenge = dpos;
     262                 :         62 :                         parse->mschap_challenge_len = dlen;
     263 [ +  - ][ +  + ]:         62 :                 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
     264                 :            :                            avp_code == RADIUS_ATTR_MS_CHAP_RESPONSE) {
     265                 :          6 :                         wpa_hexdump(MSG_DEBUG,
     266                 :            :                                     "EAP-TTLS: MS-CHAP-Response (MSCHAP)",
     267                 :            :                                     dpos, dlen);
     268                 :          6 :                         parse->mschap_response = dpos;
     269                 :          6 :                         parse->mschap_response_len = dlen;
     270 [ +  - ][ +  - ]:         56 :                 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
     271                 :            :                            avp_code == RADIUS_ATTR_MS_CHAP2_RESPONSE) {
     272                 :         56 :                         wpa_hexdump(MSG_DEBUG,
     273                 :            :                                     "EAP-TTLS: MS-CHAP2-Response (MSCHAPV2)",
     274                 :            :                                     dpos, dlen);
     275                 :         56 :                         parse->mschap2_response = dpos;
     276                 :         56 :                         parse->mschap2_response_len = dlen;
     277         [ #  # ]:          0 :                 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
     278                 :          0 :                         wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported "
     279                 :            :                                    "mandatory AVP code %d vendor_id %d - "
     280                 :            :                                    "dropped", (int) avp_code, (int) vendor_id);
     281                 :          0 :                         goto fail;
     282                 :            :                 } else {
     283                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported "
     284                 :            :                                    "AVP code %d vendor_id %d",
     285                 :            :                                    (int) avp_code, (int) vendor_id);
     286                 :            :                 }
     287                 :            : 
     288                 :        240 :                 pad = (4 - (avp_length & 3)) & 3;
     289                 :        240 :                 pos += avp_length + pad;
     290                 :        240 :                 left -= avp_length + pad;
     291                 :            :         }
     292                 :            : 
     293                 :        105 :         return 0;
     294                 :            : 
     295                 :            : fail:
     296                 :          0 :         os_free(parse->eap);
     297                 :          0 :         parse->eap = NULL;
     298                 :        105 :         return -1;
     299                 :            : }
     300                 :            : 
     301                 :            : 
     302                 :         66 : static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
     303                 :            :                                         struct eap_ttls_data *data, size_t len)
     304                 :            : {
     305                 :         66 :         return eap_server_tls_derive_key(sm, &data->ssl, "ttls challenge",
     306                 :            :                                          len);
     307                 :            : }
     308                 :            : 
     309                 :            : 
     310                 :        128 : static void * eap_ttls_init(struct eap_sm *sm)
     311                 :            : {
     312                 :            :         struct eap_ttls_data *data;
     313                 :            : 
     314                 :        128 :         data = os_zalloc(sizeof(*data));
     315         [ -  + ]:        128 :         if (data == NULL)
     316                 :          0 :                 return NULL;
     317                 :        128 :         data->ttls_version = EAP_TTLS_VERSION;
     318                 :        128 :         data->state = START;
     319                 :            : 
     320         [ -  + ]:        128 :         if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
     321                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
     322                 :          0 :                 eap_ttls_reset(sm, data);
     323                 :          0 :                 return NULL;
     324                 :            :         }
     325                 :            : 
     326                 :        128 :         return data;
     327                 :            : }
     328                 :            : 
     329                 :            : 
     330                 :        128 : static void eap_ttls_reset(struct eap_sm *sm, void *priv)
     331                 :            : {
     332                 :        128 :         struct eap_ttls_data *data = priv;
     333         [ -  + ]:        128 :         if (data == NULL)
     334                 :        128 :                 return;
     335 [ +  + ][ +  - ]:        128 :         if (data->phase2_priv && data->phase2_method)
     336                 :         12 :                 data->phase2_method->reset(sm, data->phase2_priv);
     337                 :        128 :         eap_server_tls_ssl_deinit(sm, &data->ssl);
     338                 :        128 :         wpabuf_free(data->pending_phase2_eap_resp);
     339                 :        128 :         os_free(data);
     340                 :            : }
     341                 :            : 
     342                 :            : 
     343                 :        128 : static struct wpabuf * eap_ttls_build_start(struct eap_sm *sm,
     344                 :            :                                             struct eap_ttls_data *data, u8 id)
     345                 :            : {       
     346                 :            :         struct wpabuf *req;
     347                 :            : 
     348                 :        128 :         req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS, 1,
     349                 :            :                             EAP_CODE_REQUEST, id);
     350         [ -  + ]:        128 :         if (req == NULL) {
     351                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to allocate memory for"
     352                 :            :                            " request");
     353                 :          0 :                 eap_ttls_state(data, FAILURE);
     354                 :          0 :                 return NULL;
     355                 :            :         }
     356                 :            : 
     357                 :        128 :         wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->ttls_version);
     358                 :            : 
     359                 :        128 :         eap_ttls_state(data, PHASE1);
     360                 :            : 
     361                 :        128 :         return req;
     362                 :            : }
     363                 :            : 
     364                 :            : 
     365                 :         24 : static struct wpabuf * eap_ttls_build_phase2_eap_req(
     366                 :            :         struct eap_sm *sm, struct eap_ttls_data *data, u8 id)
     367                 :            : {
     368                 :            :         struct wpabuf *buf, *encr_req;
     369                 :            : 
     370                 :            : 
     371                 :         24 :         buf = data->phase2_method->buildReq(sm, data->phase2_priv, id);
     372         [ -  + ]:         24 :         if (buf == NULL)
     373                 :          0 :                 return NULL;
     374                 :            : 
     375                 :         24 :         wpa_hexdump_buf_key(MSG_DEBUG,
     376                 :            :                             "EAP-TTLS/EAP: Encapsulate Phase 2 data", buf);
     377                 :            : 
     378                 :         24 :         buf = eap_ttls_avp_encapsulate(buf, RADIUS_ATTR_EAP_MESSAGE, 1);
     379         [ -  + ]:         24 :         if (buf == NULL) {
     380                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Failed to encapsulate "
     381                 :            :                            "packet");
     382                 :          0 :                 return NULL;
     383                 :            :         }
     384                 :            : 
     385                 :         24 :         wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS/EAP: Encrypt encapsulated "
     386                 :            :                             "Phase 2 data", buf);
     387                 :            : 
     388                 :         24 :         encr_req = eap_server_tls_encrypt(sm, &data->ssl, buf);
     389                 :         24 :         wpabuf_free(buf);
     390                 :            : 
     391                 :         24 :         return encr_req;
     392                 :            : }
     393                 :            : 
     394                 :            : 
     395                 :         56 : static struct wpabuf * eap_ttls_build_phase2_mschapv2(
     396                 :            :         struct eap_sm *sm, struct eap_ttls_data *data)
     397                 :            : {
     398                 :            :         struct wpabuf *encr_req, msgbuf;
     399                 :            :         u8 *req, *pos, *end;
     400                 :            :         int ret;
     401                 :            : 
     402                 :         56 :         pos = req = os_malloc(100);
     403         [ -  + ]:         56 :         if (req == NULL)
     404                 :          0 :                 return NULL;
     405                 :         56 :         end = req + 100;
     406                 :            : 
     407         [ +  + ]:         56 :         if (data->mschapv2_resp_ok) {
     408                 :         55 :                 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_SUCCESS,
     409                 :            :                                        RADIUS_VENDOR_ID_MICROSOFT, 1, 43);
     410                 :         55 :                 *pos++ = data->mschapv2_ident;
     411                 :         55 :                 ret = os_snprintf((char *) pos, end - pos, "S=");
     412 [ +  - ][ +  - ]:         55 :                 if (ret >= 0 && ret < end - pos)
     413                 :         55 :                         pos += ret;
     414                 :         55 :                 pos += wpa_snprintf_hex_uppercase(
     415                 :         55 :                         (char *) pos, end - pos, data->mschapv2_auth_response,
     416                 :            :                         sizeof(data->mschapv2_auth_response));
     417                 :            :         } else {
     418                 :          1 :                 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_ERROR,
     419                 :            :                                        RADIUS_VENDOR_ID_MICROSOFT, 1, 6);
     420                 :          1 :                 os_memcpy(pos, "Failed", 6);
     421                 :          1 :                 pos += 6;
     422                 :          1 :                 AVP_PAD(req, pos);
     423                 :            :         }
     424                 :            : 
     425                 :         56 :         wpabuf_set(&msgbuf, req, pos - req);
     426                 :         56 :         wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Encrypting Phase 2 "
     427                 :            :                             "data", &msgbuf);
     428                 :            : 
     429                 :         56 :         encr_req = eap_server_tls_encrypt(sm, &data->ssl, &msgbuf);
     430                 :         56 :         os_free(req);
     431                 :            : 
     432                 :         56 :         return encr_req;
     433                 :            : }
     434                 :            : 
     435                 :            : 
     436                 :        466 : static struct wpabuf * eap_ttls_buildReq(struct eap_sm *sm, void *priv, u8 id)
     437                 :            : {
     438                 :        466 :         struct eap_ttls_data *data = priv;
     439                 :            : 
     440         [ +  + ]:        466 :         if (data->ssl.state == FRAG_ACK) {
     441                 :          1 :                 return eap_server_tls_build_ack(id, EAP_TYPE_TTLS,
     442                 :            :                                                 data->ttls_version);
     443                 :            :         }
     444                 :            : 
     445         [ +  + ]:        465 :         if (data->ssl.state == WAIT_FRAG_ACK) {
     446                 :         86 :                 return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_TTLS,
     447                 :            :                                                 data->ttls_version, id);
     448                 :            :         }
     449                 :            : 
     450   [ +  +  +  +  :        379 :         switch (data->state) {
                      - ]
     451                 :            :         case START:
     452                 :        128 :                 return eap_ttls_build_start(sm, data, id);
     453                 :            :         case PHASE1:
     454         [ +  + ]:        171 :                 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
     455                 :         81 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase1 done, "
     456                 :            :                                    "starting Phase2");
     457                 :         81 :                         eap_ttls_state(data, PHASE2_START);
     458                 :            :                 }
     459                 :        171 :                 break;
     460                 :            :         case PHASE2_METHOD:
     461                 :         24 :                 wpabuf_free(data->ssl.tls_out);
     462                 :         24 :                 data->ssl.tls_out_pos = 0;
     463                 :         24 :                 data->ssl.tls_out = eap_ttls_build_phase2_eap_req(sm, data,
     464                 :            :                                                                   id);
     465                 :         24 :                 break;
     466                 :            :         case PHASE2_MSCHAPV2_RESP:
     467                 :         56 :                 wpabuf_free(data->ssl.tls_out);
     468                 :         56 :                 data->ssl.tls_out_pos = 0;
     469                 :         56 :                 data->ssl.tls_out = eap_ttls_build_phase2_mschapv2(sm, data);
     470                 :         56 :                 break;
     471                 :            :         default:
     472                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: %s - unexpected state %d",
     473                 :          0 :                            __func__, data->state);
     474                 :          0 :                 return NULL;
     475                 :            :         }
     476                 :            : 
     477                 :        466 :         return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_TTLS,
     478                 :            :                                         data->ttls_version, id);
     479                 :            : }
     480                 :            : 
     481                 :            : 
     482                 :        428 : static Boolean eap_ttls_check(struct eap_sm *sm, void *priv,
     483                 :            :                               struct wpabuf *respData)
     484                 :            : {
     485                 :            :         const u8 *pos;
     486                 :            :         size_t len;
     487                 :            : 
     488                 :        428 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TTLS, respData, &len);
     489 [ -  + ][ +  - ]:        428 :         if (pos == NULL || len < 1) {
     490                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Invalid frame");
     491                 :          0 :                 return TRUE;
     492                 :            :         }
     493                 :            : 
     494                 :        428 :         return FALSE;
     495                 :            : }
     496                 :            : 
     497                 :            : 
     498                 :          3 : static void eap_ttls_process_phase2_pap(struct eap_sm *sm,
     499                 :            :                                         struct eap_ttls_data *data,
     500                 :            :                                         const u8 *user_password,
     501                 :            :                                         size_t user_password_len)
     502                 :            : {
     503 [ +  - ][ +  - ]:          3 :         if (!sm->user || !sm->user->password || sm->user->password_hash ||
         [ +  - ][ -  + ]
     504                 :          3 :             !(sm->user->ttls_auth & EAP_TTLS_AUTH_PAP)) {
     505                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: No plaintext user "
     506                 :            :                            "password configured");
     507                 :          0 :                 eap_ttls_state(data, FAILURE);
     508                 :          0 :                 return;
     509                 :            :         }
     510                 :            : 
     511 [ +  - ][ -  + ]:          3 :         if (sm->user->password_len != user_password_len ||
     512                 :          3 :             os_memcmp(sm->user->password, user_password, user_password_len) !=
     513                 :            :             0) {
     514                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: Invalid user password");
     515                 :          0 :                 eap_ttls_state(data, FAILURE);
     516                 :          0 :                 return;
     517                 :            :         }
     518                 :            : 
     519                 :          3 :         wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: Correct user password");
     520                 :          3 :         eap_ttls_state(data, SUCCESS);
     521                 :            : }
     522                 :            : 
     523                 :            : 
     524                 :          4 : static void eap_ttls_process_phase2_chap(struct eap_sm *sm,
     525                 :            :                                          struct eap_ttls_data *data,
     526                 :            :                                          const u8 *challenge,
     527                 :            :                                          size_t challenge_len,
     528                 :            :                                          const u8 *password,
     529                 :            :                                          size_t password_len)
     530                 :            : {
     531                 :            :         u8 *chal, hash[CHAP_MD5_LEN];
     532                 :            : 
     533 [ +  - ][ +  - ]:          4 :         if (challenge == NULL || password == NULL ||
                 [ +  - ]
     534         [ -  + ]:          4 :             challenge_len != EAP_TTLS_CHAP_CHALLENGE_LEN ||
     535                 :            :             password_len != 1 + EAP_TTLS_CHAP_PASSWORD_LEN) {
     536                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Invalid CHAP attributes "
     537                 :            :                            "(challenge len %lu password len %lu)",
     538                 :            :                            (unsigned long) challenge_len,
     539                 :            :                            (unsigned long) password_len);
     540                 :          0 :                 eap_ttls_state(data, FAILURE);
     541                 :          0 :                 return;
     542                 :            :         }
     543                 :            : 
     544 [ +  - ][ +  - ]:          4 :         if (!sm->user || !sm->user->password || sm->user->password_hash ||
         [ +  - ][ -  + ]
     545                 :          4 :             !(sm->user->ttls_auth & EAP_TTLS_AUTH_CHAP)) {
     546                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: No plaintext user "
     547                 :            :                            "password configured");
     548                 :          0 :                 eap_ttls_state(data, FAILURE);
     549                 :          0 :                 return;
     550                 :            :         }
     551                 :            : 
     552                 :          4 :         chal = eap_ttls_implicit_challenge(sm, data,
     553                 :            :                                            EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
     554         [ -  + ]:          4 :         if (chal == NULL) {
     555                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Failed to generate "
     556                 :            :                            "challenge from TLS data");
     557                 :          0 :                 eap_ttls_state(data, FAILURE);
     558                 :          0 :                 return;
     559                 :            :         }
     560                 :            : 
     561 [ +  - ][ -  + ]:          4 :         if (os_memcmp(challenge, chal, EAP_TTLS_CHAP_CHALLENGE_LEN) != 0 ||
     562                 :          4 :             password[0] != chal[EAP_TTLS_CHAP_CHALLENGE_LEN]) {
     563                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Challenge mismatch");
     564                 :          0 :                 os_free(chal);
     565                 :          0 :                 eap_ttls_state(data, FAILURE);
     566                 :          0 :                 return;
     567                 :            :         }
     568                 :          4 :         os_free(chal);
     569                 :            : 
     570                 :            :         /* MD5(Ident + Password + Challenge) */
     571                 :          4 :         chap_md5(password[0], sm->user->password, sm->user->password_len,
     572                 :            :                  challenge, challenge_len, hash);
     573                 :            : 
     574         [ +  - ]:          4 :         if (os_memcmp(hash, password + 1, EAP_TTLS_CHAP_PASSWORD_LEN) == 0) {
     575                 :          4 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Correct user password");
     576                 :          4 :                 eap_ttls_state(data, SUCCESS);
     577                 :            :         } else {
     578                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Invalid user password");
     579                 :          4 :                 eap_ttls_state(data, FAILURE);
     580                 :            :         }
     581                 :            : }
     582                 :            : 
     583                 :            : 
     584                 :          6 : static void eap_ttls_process_phase2_mschap(struct eap_sm *sm,
     585                 :            :                                            struct eap_ttls_data *data,
     586                 :            :                                            u8 *challenge, size_t challenge_len,
     587                 :            :                                            u8 *response, size_t response_len)
     588                 :            : {
     589                 :            :         u8 *chal, nt_response[24];
     590                 :            : 
     591 [ +  - ][ +  - ]:          6 :         if (challenge == NULL || response == NULL ||
                 [ +  - ]
     592         [ -  + ]:          6 :             challenge_len != EAP_TTLS_MSCHAP_CHALLENGE_LEN ||
     593                 :            :             response_len != EAP_TTLS_MSCHAP_RESPONSE_LEN) {
     594                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Invalid MS-CHAP "
     595                 :            :                            "attributes (challenge len %lu response len %lu)",
     596                 :            :                            (unsigned long) challenge_len,
     597                 :            :                            (unsigned long) response_len);
     598                 :          0 :                 eap_ttls_state(data, FAILURE);
     599                 :          0 :                 return;
     600                 :            :         }
     601                 :            : 
     602 [ +  - ][ +  - ]:          6 :         if (!sm->user || !sm->user->password ||
                 [ -  + ]
     603                 :          6 :             !(sm->user->ttls_auth & EAP_TTLS_AUTH_MSCHAP)) {
     604                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: No user password "
     605                 :            :                            "configured");
     606                 :          0 :                 eap_ttls_state(data, FAILURE);
     607                 :          0 :                 return;
     608                 :            :         }
     609                 :            : 
     610                 :          6 :         chal = eap_ttls_implicit_challenge(sm, data,
     611                 :            :                                            EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
     612         [ -  + ]:          6 :         if (chal == NULL) {
     613                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Failed to generate "
     614                 :            :                            "challenge from TLS data");
     615                 :          0 :                 eap_ttls_state(data, FAILURE);
     616                 :          0 :                 return;
     617                 :            :         }
     618                 :            : 
     619 [ +  - ][ -  + ]:          6 :         if (os_memcmp(challenge, chal, EAP_TTLS_MSCHAP_CHALLENGE_LEN) != 0 ||
     620                 :          6 :             response[0] != chal[EAP_TTLS_MSCHAP_CHALLENGE_LEN]) {
     621                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Challenge mismatch");
     622                 :          0 :                 os_free(chal);
     623                 :          0 :                 eap_ttls_state(data, FAILURE);
     624                 :          0 :                 return;
     625                 :            :         }
     626                 :          6 :         os_free(chal);
     627                 :            : 
     628         [ -  + ]:          6 :         if (sm->user->password_hash)
     629                 :          0 :                 challenge_response(challenge, sm->user->password, nt_response);
     630                 :            :         else
     631                 :          6 :                 nt_challenge_response(challenge, sm->user->password,
     632                 :          6 :                                       sm->user->password_len, nt_response);
     633                 :            : 
     634         [ +  - ]:          6 :         if (os_memcmp(nt_response, response + 2 + 24, 24) == 0) {
     635                 :          6 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Correct response");
     636                 :          6 :                 eap_ttls_state(data, SUCCESS);
     637                 :            :         } else {
     638                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Invalid NT-Response");
     639                 :          0 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAP: Received",
     640                 :            :                             response + 2 + 24, 24);
     641                 :          0 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAP: Expected",
     642                 :            :                             nt_response, 24);
     643                 :          6 :                 eap_ttls_state(data, FAILURE);
     644                 :            :         }
     645                 :            : }
     646                 :            : 
     647                 :            : 
     648                 :         56 : static void eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
     649                 :            :                                              struct eap_ttls_data *data,
     650                 :            :                                              u8 *challenge,
     651                 :            :                                              size_t challenge_len,
     652                 :            :                                              u8 *response, size_t response_len)
     653                 :            : {
     654                 :            :         u8 *chal, *username, nt_response[24], *rx_resp, *peer_challenge,
     655                 :            :                 *auth_challenge;
     656                 :            :         size_t username_len, i;
     657                 :            : 
     658 [ +  - ][ +  - ]:         56 :         if (challenge == NULL || response == NULL ||
                 [ +  - ]
     659         [ -  + ]:         56 :             challenge_len != EAP_TTLS_MSCHAPV2_CHALLENGE_LEN ||
     660                 :            :             response_len != EAP_TTLS_MSCHAPV2_RESPONSE_LEN) {
     661                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Invalid MS-CHAP2 "
     662                 :            :                            "attributes (challenge len %lu response len %lu)",
     663                 :            :                            (unsigned long) challenge_len,
     664                 :            :                            (unsigned long) response_len);
     665                 :          0 :                 eap_ttls_state(data, FAILURE);
     666                 :          0 :                 return;
     667                 :            :         }
     668                 :            : 
     669 [ +  - ][ +  - ]:         56 :         if (!sm->user || !sm->user->password ||
                 [ -  + ]
     670                 :         56 :             !(sm->user->ttls_auth & EAP_TTLS_AUTH_MSCHAPV2)) {
     671                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: No user password "
     672                 :            :                            "configured");
     673                 :          0 :                 eap_ttls_state(data, FAILURE);
     674                 :          0 :                 return;
     675                 :            :         }
     676                 :            : 
     677         [ -  + ]:         56 :         if (sm->identity == NULL) {
     678                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: No user identity "
     679                 :            :                            "known");
     680                 :          0 :                 eap_ttls_state(data, FAILURE);
     681                 :          0 :                 return;
     682                 :            :         }
     683                 :            : 
     684                 :            :         /* MSCHAPv2 does not include optional domain name in the
     685                 :            :          * challenge-response calculation, so remove domain prefix
     686                 :            :          * (if present). */
     687                 :         56 :         username = sm->identity;
     688                 :         56 :         username_len = sm->identity_len;
     689         [ +  + ]:        541 :         for (i = 0; i < username_len; i++) {
     690         [ +  + ]:        493 :                 if (username[i] == '\\') {
     691                 :          8 :                         username_len -= i + 1;
     692                 :          8 :                         username += i + 1;
     693                 :          8 :                         break;
     694                 :            :                 }
     695                 :            :         }
     696                 :            : 
     697                 :         56 :         chal = eap_ttls_implicit_challenge(
     698                 :            :                 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
     699         [ -  + ]:         56 :         if (chal == NULL) {
     700                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Failed to generate "
     701                 :            :                            "challenge from TLS data");
     702                 :          0 :                 eap_ttls_state(data, FAILURE);
     703                 :          0 :                 return;
     704                 :            :         }
     705                 :            : 
     706 [ +  - ][ -  + ]:         56 :         if (os_memcmp(challenge, chal, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) != 0 ||
     707                 :         56 :             response[0] != chal[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN]) {
     708                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Challenge mismatch");
     709                 :          0 :                 os_free(chal);
     710                 :          0 :                 eap_ttls_state(data, FAILURE);
     711                 :          0 :                 return;
     712                 :            :         }
     713                 :         56 :         os_free(chal);
     714                 :            : 
     715                 :         56 :         auth_challenge = challenge;
     716                 :         56 :         peer_challenge = response + 2;
     717                 :            : 
     718                 :         56 :         wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: User",
     719                 :            :                           username, username_len);
     720                 :         56 :         wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: auth_challenge",
     721                 :            :                     auth_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
     722                 :         56 :         wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: peer_challenge",
     723                 :            :                     peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
     724                 :            : 
     725         [ +  + ]:         56 :         if (sm->user->password_hash) {
     726                 :          9 :                 generate_nt_response_pwhash(auth_challenge, peer_challenge,
     727                 :            :                                             username, username_len,
     728                 :          9 :                                             sm->user->password,
     729                 :            :                                             nt_response);
     730                 :            :         } else {
     731                 :         47 :                 generate_nt_response(auth_challenge, peer_challenge,
     732                 :            :                                      username, username_len,
     733                 :         47 :                                      sm->user->password,
     734                 :         47 :                                      sm->user->password_len,
     735                 :            :                                      nt_response);
     736                 :            :         }
     737                 :            : 
     738                 :         56 :         rx_resp = response + 2 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 8;
     739         [ +  + ]:         56 :         if (os_memcmp(nt_response, rx_resp, 24) == 0) {
     740                 :         55 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Correct "
     741                 :            :                            "NT-Response");
     742                 :         55 :                 data->mschapv2_resp_ok = 1;
     743                 :            : 
     744         [ +  + ]:         55 :                 if (sm->user->password_hash) {
     745                 :          8 :                         generate_authenticator_response_pwhash(
     746                 :          8 :                                 sm->user->password,
     747                 :            :                                 peer_challenge, auth_challenge,
     748                 :            :                                 username, username_len, nt_response,
     749                 :          8 :                                 data->mschapv2_auth_response);
     750                 :            :                 } else {
     751                 :         47 :                         generate_authenticator_response(
     752                 :         94 :                                 sm->user->password, sm->user->password_len,
     753                 :            :                                 peer_challenge, auth_challenge,
     754                 :            :                                 username, username_len, nt_response,
     755                 :         47 :                                 data->mschapv2_auth_response);
     756                 :            :                 }
     757                 :            :         } else {
     758                 :          1 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Invalid "
     759                 :            :                            "NT-Response");
     760                 :          1 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: Received",
     761                 :            :                             rx_resp, 24);
     762                 :          1 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: Expected",
     763                 :            :                             nt_response, 24);
     764                 :          1 :                 data->mschapv2_resp_ok = 0;
     765                 :            :         }
     766                 :         56 :         eap_ttls_state(data, PHASE2_MSCHAPV2_RESP);
     767                 :         56 :         data->mschapv2_ident = response[0];
     768                 :            : }
     769                 :            : 
     770                 :            : 
     771                 :         29 : static int eap_ttls_phase2_eap_init(struct eap_sm *sm,
     772                 :            :                                     struct eap_ttls_data *data,
     773                 :            :                                     EapType eap_type)
     774                 :            : {
     775 [ +  + ][ +  - ]:         29 :         if (data->phase2_priv && data->phase2_method) {
     776                 :         17 :                 data->phase2_method->reset(sm, data->phase2_priv);
     777                 :         17 :                 data->phase2_method = NULL;
     778                 :         17 :                 data->phase2_priv = NULL;
     779                 :            :         }
     780                 :         29 :         data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
     781                 :            :                                                         eap_type);
     782         [ -  + ]:         29 :         if (!data->phase2_method)
     783                 :          0 :                 return -1;
     784                 :            : 
     785                 :         29 :         sm->init_phase2 = 1;
     786                 :         29 :         data->phase2_priv = data->phase2_method->init(sm);
     787                 :         29 :         sm->init_phase2 = 0;
     788         [ -  + ]:         29 :         return data->phase2_priv == NULL ? -1 : 0;
     789                 :            : }
     790                 :            : 
     791                 :            : 
     792                 :         36 : static void eap_ttls_process_phase2_eap_response(struct eap_sm *sm,
     793                 :            :                                                  struct eap_ttls_data *data,
     794                 :            :                                                  u8 *in_data, size_t in_len)
     795                 :            : {
     796                 :         36 :         u8 next_type = EAP_TYPE_NONE;
     797                 :            :         struct eap_hdr *hdr;
     798                 :            :         u8 *pos;
     799                 :            :         size_t left;
     800                 :            :         struct wpabuf buf;
     801                 :         36 :         const struct eap_method *m = data->phase2_method;
     802                 :         36 :         void *priv = data->phase2_priv;
     803                 :            : 
     804         [ -  + ]:         36 :         if (priv == NULL) {
     805                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: %s - Phase2 not "
     806                 :            :                            "initialized?!", __func__);
     807                 :          0 :                 return;
     808                 :            :         }
     809                 :            : 
     810                 :         36 :         hdr = (struct eap_hdr *) in_data;
     811                 :         36 :         pos = (u8 *) (hdr + 1);
     812                 :            : 
     813 [ +  - ][ +  + ]:         36 :         if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
     814                 :          5 :                 left = in_len - sizeof(*hdr);
     815                 :          5 :                 wpa_hexdump(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 type Nak'ed; "
     816                 :            :                             "allowed types", pos + 1, left - 1);
     817                 :          5 :                 eap_sm_process_nak(sm, pos + 1, left - 1);
     818 [ +  - ][ +  - ]:          5 :                 if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
                 [ +  - ]
     819                 :          5 :                     sm->user->methods[sm->user_eap_method_index].method !=
     820                 :            :                     EAP_TYPE_NONE) {
     821                 :         10 :                         next_type = sm->user->methods[
     822                 :         10 :                                 sm->user_eap_method_index++].method;
     823                 :          5 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: try EAP type %d",
     824                 :            :                                    next_type);
     825         [ -  + ]:          5 :                         if (eap_ttls_phase2_eap_init(sm, data, next_type)) {
     826                 :          0 :                                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to "
     827                 :            :                                            "initialize EAP type %d",
     828                 :            :                                            next_type);
     829                 :          0 :                                 eap_ttls_state(data, FAILURE);
     830                 :          0 :                                 return;
     831                 :            :                         }
     832                 :            :                 } else {
     833                 :          0 :                         eap_ttls_state(data, FAILURE);
     834                 :            :                 }
     835                 :          5 :                 return;
     836                 :            :         }
     837                 :            : 
     838                 :         31 :         wpabuf_set(&buf, in_data, in_len);
     839                 :            : 
     840         [ -  + ]:         31 :         if (m->check(sm, priv, &buf)) {
     841                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 check() asked to "
     842                 :            :                            "ignore the packet");
     843                 :          0 :                 return;
     844                 :            :         }
     845                 :            : 
     846                 :         31 :         m->process(sm, priv, &buf);
     847                 :            : 
     848         [ -  + ]:         31 :         if (sm->method_pending == METHOD_PENDING_WAIT) {
     849                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 method is in "
     850                 :            :                            "pending wait state - save decrypted response");
     851                 :          0 :                 wpabuf_free(data->pending_phase2_eap_resp);
     852                 :          0 :                 data->pending_phase2_eap_resp = wpabuf_dup(&buf);
     853                 :            :         }
     854                 :            : 
     855         [ +  + ]:         31 :         if (!m->isDone(sm, priv))
     856                 :          7 :                 return;
     857                 :            : 
     858         [ +  + ]:         24 :         if (!m->isSuccess(sm, priv)) {
     859                 :          1 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 method failed");
     860                 :          1 :                 eap_ttls_state(data, FAILURE);
     861                 :          1 :                 return;
     862                 :            :         }
     863                 :            : 
     864   [ +  +  -  - ]:         23 :         switch (data->state) {
     865                 :            :         case PHASE2_START:
     866         [ -  + ]:         12 :                 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
     867                 :          0 :                         wpa_hexdump_ascii(MSG_DEBUG, "EAP_TTLS: Phase2 "
     868                 :            :                                           "Identity not found in the user "
     869                 :            :                                           "database",
     870                 :          0 :                                           sm->identity, sm->identity_len);
     871                 :          0 :                         eap_ttls_state(data, FAILURE);
     872                 :          0 :                         break;
     873                 :            :                 }
     874                 :            : 
     875                 :         12 :                 eap_ttls_state(data, PHASE2_METHOD);
     876                 :         12 :                 next_type = sm->user->methods[0].method;
     877                 :         12 :                 sm->user_eap_method_index = 1;
     878                 :         12 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: try EAP type %d", next_type);
     879         [ -  + ]:         12 :                 if (eap_ttls_phase2_eap_init(sm, data, next_type)) {
     880                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to initialize "
     881                 :            :                                    "EAP type %d", next_type);
     882                 :          0 :                         eap_ttls_state(data, FAILURE);
     883                 :            :                 }
     884                 :         12 :                 break;
     885                 :            :         case PHASE2_METHOD:
     886                 :         11 :                 eap_ttls_state(data, SUCCESS);
     887                 :         11 :                 break;
     888                 :            :         case FAILURE:
     889                 :          0 :                 break;
     890                 :            :         default:
     891                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: %s - unexpected state %d",
     892                 :          0 :                            __func__, data->state);
     893                 :         36 :                 break;
     894                 :            :         }
     895                 :            : }
     896                 :            : 
     897                 :            : 
     898                 :         36 : static void eap_ttls_process_phase2_eap(struct eap_sm *sm,
     899                 :            :                                         struct eap_ttls_data *data,
     900                 :            :                                         const u8 *eap, size_t eap_len)
     901                 :            : {
     902                 :            :         struct eap_hdr *hdr;
     903                 :            :         size_t len;
     904                 :            : 
     905         [ +  + ]:         36 :         if (data->state == PHASE2_START) {
     906                 :         12 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: initializing Phase 2");
     907         [ -  + ]:         12 :                 if (eap_ttls_phase2_eap_init(sm, data, EAP_TYPE_IDENTITY) < 0)
     908                 :            :                 {
     909                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: failed to "
     910                 :            :                                    "initialize EAP-Identity");
     911                 :          0 :                         return;
     912                 :            :                 }
     913                 :            :         }
     914                 :            : 
     915         [ -  + ]:         36 :         if (eap_len < sizeof(*hdr)) {
     916                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: too short Phase 2 EAP "
     917                 :            :                            "packet (len=%lu)", (unsigned long) eap_len);
     918                 :          0 :                 return;
     919                 :            :         }
     920                 :            : 
     921                 :         36 :         hdr = (struct eap_hdr *) eap;
     922                 :         36 :         len = be_to_host16(hdr->length);
     923                 :         36 :         wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: received Phase 2 EAP: code=%d "
     924                 :         72 :                    "identifier=%d length=%lu", hdr->code, hdr->identifier,
     925                 :            :                    (unsigned long) len);
     926         [ -  + ]:         36 :         if (len > eap_len) {
     927                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS/EAP: Length mismatch in Phase 2"
     928                 :            :                            " EAP frame (hdr len=%lu, data len in AVP=%lu)",
     929                 :            :                            (unsigned long) len, (unsigned long) eap_len);
     930                 :          0 :                 return;
     931                 :            :         }
     932                 :            : 
     933         [ +  - ]:         36 :         switch (hdr->code) {
     934                 :            :         case EAP_CODE_RESPONSE:
     935                 :         36 :                 eap_ttls_process_phase2_eap_response(sm, data, (u8 *) hdr,
     936                 :            :                                                      len);
     937                 :         36 :                 break;
     938                 :            :         default:
     939                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS/EAP: Unexpected code=%d in "
     940                 :          0 :                            "Phase 2 EAP header", hdr->code);
     941                 :         36 :                 break;
     942                 :            :         }
     943                 :            : }
     944                 :            : 
     945                 :            : 
     946                 :        105 : static void eap_ttls_process_phase2(struct eap_sm *sm,
     947                 :            :                                     struct eap_ttls_data *data,
     948                 :            :                                     struct wpabuf *in_buf)
     949                 :            : {
     950                 :            :         struct wpabuf *in_decrypted;
     951                 :            :         struct eap_ttls_avp parse;
     952                 :            : 
     953                 :        105 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
     954                 :            :                    " Phase 2", (unsigned long) wpabuf_len(in_buf));
     955                 :            : 
     956         [ -  + ]:        105 :         if (data->pending_phase2_eap_resp) {
     957                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 EAP response "
     958                 :            :                            "- skip decryption and use old data");
     959                 :          0 :                 eap_ttls_process_phase2_eap(
     960                 :          0 :                         sm, data, wpabuf_head(data->pending_phase2_eap_resp),
     961                 :          0 :                         wpabuf_len(data->pending_phase2_eap_resp));
     962                 :          0 :                 wpabuf_free(data->pending_phase2_eap_resp);
     963                 :          0 :                 data->pending_phase2_eap_resp = NULL;
     964                 :          0 :                 return;
     965                 :            :         }
     966                 :            : 
     967                 :        105 :         in_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
     968                 :            :                                               in_buf);
     969         [ -  + ]:        105 :         if (in_decrypted == NULL) {
     970                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to decrypt Phase 2 "
     971                 :            :                            "data");
     972                 :          0 :                 eap_ttls_state(data, FAILURE);
     973                 :          0 :                 return;
     974                 :            :         }
     975                 :            : 
     976                 :        105 :         wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 EAP",
     977                 :            :                             in_decrypted);
     978                 :            : 
     979         [ -  + ]:        105 :         if (eap_ttls_avp_parse(in_decrypted, &parse) < 0) {
     980                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to parse AVPs");
     981                 :          0 :                 wpabuf_free(in_decrypted);
     982                 :          0 :                 eap_ttls_state(data, FAILURE);
     983                 :          0 :                 return;
     984                 :            :         }
     985                 :            : 
     986         [ +  + ]:        105 :         if (parse.user_name) {
     987                 :         69 :                 os_free(sm->identity);
     988                 :         69 :                 sm->identity = os_malloc(parse.user_name_len);
     989         [ -  + ]:         69 :                 if (sm->identity == NULL) {
     990                 :          0 :                         eap_ttls_state(data, FAILURE);
     991                 :          0 :                         goto done;
     992                 :            :                 }
     993                 :         69 :                 os_memcpy(sm->identity, parse.user_name, parse.user_name_len);
     994                 :         69 :                 sm->identity_len = parse.user_name_len;
     995         [ -  + ]:         69 :                 if (eap_user_get(sm, parse.user_name, parse.user_name_len, 1)
     996                 :            :                     != 0) {
     997                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 Identity not "
     998                 :            :                                    "found in the user database");
     999                 :          0 :                         eap_ttls_state(data, FAILURE);
    1000                 :          0 :                         goto done;
    1001                 :            :                 }
    1002                 :            :         }
    1003                 :            : 
    1004                 :            : #ifdef EAP_SERVER_TNC
    1005 [ -  + ][ #  # ]:        105 :         if (data->tnc_started && parse.eap == NULL) {
    1006                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: TNC started but no EAP "
    1007                 :            :                            "response from peer");
    1008                 :          0 :                 eap_ttls_state(data, FAILURE);
    1009                 :          0 :                 goto done;
    1010                 :            :         }
    1011                 :            : #endif /* EAP_SERVER_TNC */
    1012                 :            : 
    1013         [ +  + ]:        105 :         if (parse.eap) {
    1014                 :         36 :                 eap_ttls_process_phase2_eap(sm, data, parse.eap,
    1015                 :            :                                             parse.eap_len);
    1016         [ +  + ]:         69 :         } else if (parse.user_password) {
    1017                 :          3 :                 eap_ttls_process_phase2_pap(sm, data, parse.user_password,
    1018                 :            :                                             parse.user_password_len);
    1019         [ +  + ]:         66 :         } else if (parse.chap_password) {
    1020                 :          4 :                 eap_ttls_process_phase2_chap(sm, data,
    1021                 :          4 :                                              parse.chap_challenge,
    1022                 :            :                                              parse.chap_challenge_len,
    1023                 :          4 :                                              parse.chap_password,
    1024                 :            :                                              parse.chap_password_len);
    1025         [ +  + ]:         62 :         } else if (parse.mschap_response) {
    1026                 :          6 :                 eap_ttls_process_phase2_mschap(sm, data,
    1027                 :            :                                                parse.mschap_challenge,
    1028                 :            :                                                parse.mschap_challenge_len,
    1029                 :            :                                                parse.mschap_response,
    1030                 :            :                                                parse.mschap_response_len);
    1031         [ +  - ]:         56 :         } else if (parse.mschap2_response) {
    1032                 :         56 :                 eap_ttls_process_phase2_mschapv2(sm, data,
    1033                 :            :                                                  parse.mschap_challenge,
    1034                 :            :                                                  parse.mschap_challenge_len,
    1035                 :            :                                                  parse.mschap2_response,
    1036                 :            :                                                  parse.mschap2_response_len);
    1037                 :            :         }
    1038                 :            : 
    1039                 :            : done:
    1040                 :        105 :         wpabuf_free(in_decrypted);
    1041                 :        105 :         os_free(parse.eap);
    1042                 :            : }
    1043                 :            : 
    1044                 :            : 
    1045                 :        161 : static void eap_ttls_start_tnc(struct eap_sm *sm, struct eap_ttls_data *data)
    1046                 :            : {
    1047                 :            : #ifdef EAP_SERVER_TNC
    1048 [ -  + ][ #  # ]:        161 :         if (!sm->tnc || data->state != SUCCESS || data->tnc_started)
                 [ #  # ]
    1049                 :        161 :                 return;
    1050                 :            : 
    1051                 :          0 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: Initialize TNC");
    1052         [ #  # ]:          0 :         if (eap_ttls_phase2_eap_init(sm, data, EAP_TYPE_TNC)) {
    1053                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to initialize TNC");
    1054                 :          0 :                 eap_ttls_state(data, FAILURE);
    1055                 :          0 :                 return;
    1056                 :            :         }
    1057                 :            : 
    1058                 :          0 :         data->tnc_started = 1;
    1059                 :        161 :         eap_ttls_state(data, PHASE2_METHOD);
    1060                 :            : #endif /* EAP_SERVER_TNC */
    1061                 :            : }
    1062                 :            : 
    1063                 :            : 
    1064                 :        428 : static int eap_ttls_process_version(struct eap_sm *sm, void *priv,
    1065                 :            :                                     int peer_version)
    1066                 :            : {
    1067                 :        428 :         struct eap_ttls_data *data = priv;
    1068         [ -  + ]:        428 :         if (peer_version < data->ttls_version) {
    1069                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: peer ver=%d, own ver=%d; "
    1070                 :            :                            "use version %d",
    1071                 :            :                            peer_version, data->ttls_version, peer_version);
    1072                 :          0 :                 data->ttls_version = peer_version;
    1073                 :            :         }
    1074                 :            : 
    1075                 :        428 :         return 0;
    1076                 :            : }
    1077                 :            : 
    1078                 :            : 
    1079                 :        341 : static void eap_ttls_process_msg(struct eap_sm *sm, void *priv,
    1080                 :            :                                  const struct wpabuf *respData)
    1081                 :            : {
    1082                 :        341 :         struct eap_ttls_data *data = priv;
    1083                 :            : 
    1084   [ +  +  +  - ]:        341 :         switch (data->state) {
    1085                 :            :         case PHASE1:
    1086         [ +  + ]:        180 :                 if (eap_server_tls_phase1(sm, &data->ssl) < 0)
    1087                 :          9 :                         eap_ttls_state(data, FAILURE);
    1088                 :        180 :                 break;
    1089                 :            :         case PHASE2_START:
    1090                 :            :         case PHASE2_METHOD:
    1091                 :        105 :                 eap_ttls_process_phase2(sm, data, data->ssl.tls_in);
    1092                 :        105 :                 eap_ttls_start_tnc(sm, data);
    1093                 :        105 :                 break;
    1094                 :            :         case PHASE2_MSCHAPV2_RESP:
    1095 [ +  + ][ +  - ]:         56 :                 if (data->mschapv2_resp_ok && wpabuf_len(data->ssl.tls_in) ==
    1096                 :            :                     0) {
    1097                 :         55 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Peer "
    1098                 :            :                                    "acknowledged response");
    1099                 :         55 :                         eap_ttls_state(data, SUCCESS);
    1100         [ +  - ]:          1 :                 } else if (!data->mschapv2_resp_ok) {
    1101                 :          1 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Peer "
    1102                 :            :                                    "acknowledged error");
    1103                 :          1 :                         eap_ttls_state(data, FAILURE);
    1104                 :            :                 } else {
    1105                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Unexpected "
    1106                 :            :                                    "frame from peer (payload len %lu, "
    1107                 :            :                                    "expected empty frame)",
    1108                 :            :                                    (unsigned long)
    1109                 :          0 :                                    wpabuf_len(data->ssl.tls_in));
    1110                 :          0 :                         eap_ttls_state(data, FAILURE);
    1111                 :            :                 }
    1112                 :         56 :                 eap_ttls_start_tnc(sm, data);
    1113                 :         56 :                 break;
    1114                 :            :         default:
    1115                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected state %d in %s",
    1116                 :          0 :                            data->state, __func__);
    1117                 :          0 :                 break;
    1118                 :            :         }
    1119                 :        341 : }
    1120                 :            : 
    1121                 :            : 
    1122                 :        428 : static void eap_ttls_process(struct eap_sm *sm, void *priv,
    1123                 :            :                              struct wpabuf *respData)
    1124                 :            : {
    1125                 :        428 :         struct eap_ttls_data *data = priv;
    1126         [ -  + ]:        428 :         if (eap_server_tls_process(sm, &data->ssl, respData, data,
    1127                 :            :                                    EAP_TYPE_TTLS, eap_ttls_process_version,
    1128                 :            :                                    eap_ttls_process_msg) < 0)
    1129                 :          0 :                 eap_ttls_state(data, FAILURE);
    1130                 :        428 : }
    1131                 :            : 
    1132                 :            : 
    1133                 :        439 : static Boolean eap_ttls_isDone(struct eap_sm *sm, void *priv)
    1134                 :            : {
    1135                 :        439 :         struct eap_ttls_data *data = priv;
    1136 [ +  + ][ +  + ]:        439 :         return data->state == SUCCESS || data->state == FAILURE;
    1137                 :            : }
    1138                 :            : 
    1139                 :            : 
    1140                 :         90 : static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
    1141                 :            : {
    1142                 :         90 :         struct eap_ttls_data *data = priv;
    1143                 :            :         u8 *eapKeyData;
    1144                 :            : 
    1145         [ +  + ]:         90 :         if (data->state != SUCCESS)
    1146                 :         11 :                 return NULL;
    1147                 :            : 
    1148                 :         79 :         eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
    1149                 :            :                                                "ttls keying material",
    1150                 :            :                                                EAP_TLS_KEY_LEN);
    1151         [ +  - ]:         79 :         if (eapKeyData) {
    1152                 :         79 :                 *len = EAP_TLS_KEY_LEN;
    1153                 :         79 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
    1154                 :            :                                 eapKeyData, EAP_TLS_KEY_LEN);
    1155                 :            :         } else {
    1156                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
    1157                 :            :         }
    1158                 :            : 
    1159                 :         90 :         return eapKeyData;
    1160                 :            : }
    1161                 :            : 
    1162                 :            : 
    1163                 :        101 : static Boolean eap_ttls_isSuccess(struct eap_sm *sm, void *priv)
    1164                 :            : {
    1165                 :        101 :         struct eap_ttls_data *data = priv;
    1166                 :        101 :         return data->state == SUCCESS;
    1167                 :            : }
    1168                 :            : 
    1169                 :            : 
    1170                 :          2 : int eap_server_ttls_register(void)
    1171                 :            : {
    1172                 :            :         struct eap_method *eap;
    1173                 :            :         int ret;
    1174                 :            : 
    1175                 :          2 :         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
    1176                 :            :                                       EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
    1177         [ -  + ]:          2 :         if (eap == NULL)
    1178                 :          0 :                 return -1;
    1179                 :            : 
    1180                 :          2 :         eap->init = eap_ttls_init;
    1181                 :          2 :         eap->reset = eap_ttls_reset;
    1182                 :          2 :         eap->buildReq = eap_ttls_buildReq;
    1183                 :          2 :         eap->check = eap_ttls_check;
    1184                 :          2 :         eap->process = eap_ttls_process;
    1185                 :          2 :         eap->isDone = eap_ttls_isDone;
    1186                 :          2 :         eap->getKey = eap_ttls_getKey;
    1187                 :          2 :         eap->isSuccess = eap_ttls_isSuccess;
    1188                 :            : 
    1189                 :          2 :         ret = eap_server_method_register(eap);
    1190         [ -  + ]:          2 :         if (ret)
    1191                 :          0 :                 eap_server_method_free(eap);
    1192                 :          2 :         return ret;
    1193                 :            : }

Generated by: LCOV version 1.9