LCOV - code coverage report
Current view: top level - src/eap_peer - eap_ttls.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 592 781 75.8 %
Date: 2014-03-02 Functions: 39 40 97.5 %
Branches: 238 414 57.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * EAP peer method: 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_common/chap.h"
      16                 :            : #include "eap_common/eap_ttls.h"
      17                 :            : #include "mschapv2.h"
      18                 :            : #include "eap_i.h"
      19                 :            : #include "eap_tls_common.h"
      20                 :            : #include "eap_config.h"
      21                 :            : 
      22                 :            : 
      23                 :            : #define EAP_TTLS_VERSION 0
      24                 :            : 
      25                 :            : 
      26                 :            : static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
      27                 :            : 
      28                 :            : 
      29                 :            : struct eap_ttls_data {
      30                 :            :         struct eap_ssl_data ssl;
      31                 :            : 
      32                 :            :         int ttls_version;
      33                 :            : 
      34                 :            :         const struct eap_method *phase2_method;
      35                 :            :         void *phase2_priv;
      36                 :            :         int phase2_success;
      37                 :            :         int phase2_start;
      38                 :            : 
      39                 :            :         enum phase2_types {
      40                 :            :                 EAP_TTLS_PHASE2_EAP,
      41                 :            :                 EAP_TTLS_PHASE2_MSCHAPV2,
      42                 :            :                 EAP_TTLS_PHASE2_MSCHAP,
      43                 :            :                 EAP_TTLS_PHASE2_PAP,
      44                 :            :                 EAP_TTLS_PHASE2_CHAP
      45                 :            :         } phase2_type;
      46                 :            :         struct eap_method_type phase2_eap_type;
      47                 :            :         struct eap_method_type *phase2_eap_types;
      48                 :            :         size_t num_phase2_eap_types;
      49                 :            : 
      50                 :            :         u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
      51                 :            :         int auth_response_valid;
      52                 :            :         u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
      53                 :            :         u8 ident;
      54                 :            :         int resuming; /* starting a resumed session */
      55                 :            :         int reauth; /* reauthentication */
      56                 :            :         u8 *key_data;
      57                 :            :         u8 *session_id;
      58                 :            :         size_t id_len;
      59                 :            : 
      60                 :            :         struct wpabuf *pending_phase2_req;
      61                 :            : 
      62                 :            : #ifdef EAP_TNC
      63                 :            :         int ready_for_tnc;
      64                 :            :         int tnc_started;
      65                 :            : #endif /* EAP_TNC */
      66                 :            : };
      67                 :            : 
      68                 :            : 
      69                 :         80 : static void * eap_ttls_init(struct eap_sm *sm)
      70                 :            : {
      71                 :            :         struct eap_ttls_data *data;
      72                 :         80 :         struct eap_peer_config *config = eap_get_config(sm);
      73                 :            :         char *selected;
      74                 :            : 
      75                 :         80 :         data = os_zalloc(sizeof(*data));
      76         [ -  + ]:         80 :         if (data == NULL)
      77                 :          0 :                 return NULL;
      78                 :         80 :         data->ttls_version = EAP_TTLS_VERSION;
      79                 :         80 :         selected = "EAP";
      80                 :         80 :         data->phase2_type = EAP_TTLS_PHASE2_EAP;
      81                 :            : 
      82 [ +  - ][ +  + ]:         80 :         if (config && config->phase2) {
      83         [ +  + ]:         79 :                 if (os_strstr(config->phase2, "autheap=")) {
      84                 :          9 :                         selected = "EAP";
      85                 :          9 :                         data->phase2_type = EAP_TTLS_PHASE2_EAP;
      86         [ +  + ]:         70 :                 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
      87                 :         58 :                         selected = "MSCHAPV2";
      88                 :         58 :                         data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
      89         [ +  + ]:         12 :                 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
      90                 :          7 :                         selected = "MSCHAP";
      91                 :          7 :                         data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
      92         [ +  + ]:          5 :                 } else if (os_strstr(config->phase2, "auth=PAP")) {
      93                 :          2 :                         selected = "PAP";
      94                 :          2 :                         data->phase2_type = EAP_TTLS_PHASE2_PAP;
      95         [ +  - ]:          3 :                 } else if (os_strstr(config->phase2, "auth=CHAP")) {
      96                 :          3 :                         selected = "CHAP";
      97                 :          3 :                         data->phase2_type = EAP_TTLS_PHASE2_CHAP;
      98                 :            :                 }
      99                 :            :         }
     100                 :         80 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
     101                 :            : 
     102         [ +  + ]:         80 :         if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
     103         [ -  + ]:         10 :                 if (eap_peer_select_phase2_methods(config, "autheap=",
     104                 :            :                                                    &data->phase2_eap_types,
     105                 :            :                                                    &data->num_phase2_eap_types)
     106                 :            :                     < 0) {
     107                 :          0 :                         eap_ttls_deinit(sm, data);
     108                 :          0 :                         return NULL;
     109                 :            :                 }
     110                 :            : 
     111                 :         10 :                 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
     112                 :         10 :                 data->phase2_eap_type.method = EAP_TYPE_NONE;
     113                 :            :         }
     114                 :            : 
     115         [ -  + ]:         80 :         if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TTLS)) {
     116                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
     117                 :          0 :                 eap_ttls_deinit(sm, data);
     118                 :          0 :                 return NULL;
     119                 :            :         }
     120                 :            : 
     121                 :         80 :         return data;
     122                 :            : }
     123                 :            : 
     124                 :            : 
     125                 :         80 : static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
     126                 :            :                                        struct eap_ttls_data *data)
     127                 :            : {
     128 [ +  + ][ +  - ]:         80 :         if (data->phase2_priv && data->phase2_method) {
     129                 :          9 :                 data->phase2_method->deinit(sm, data->phase2_priv);
     130                 :          9 :                 data->phase2_method = NULL;
     131                 :          9 :                 data->phase2_priv = NULL;
     132                 :            :         }
     133                 :         80 : }
     134                 :            : 
     135                 :            : 
     136                 :         80 : static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
     137                 :            : {
     138                 :         80 :         struct eap_ttls_data *data = priv;
     139         [ -  + ]:         80 :         if (data == NULL)
     140                 :         80 :                 return;
     141                 :         80 :         eap_ttls_phase2_eap_deinit(sm, data);
     142                 :         80 :         os_free(data->phase2_eap_types);
     143                 :         80 :         eap_peer_tls_ssl_deinit(sm, &data->ssl);
     144                 :         80 :         os_free(data->key_data);
     145                 :         80 :         os_free(data->session_id);
     146                 :         80 :         wpabuf_free(data->pending_phase2_req);
     147                 :         80 :         os_free(data);
     148                 :            : }
     149                 :            : 
     150                 :            : 
     151                 :        240 : static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
     152                 :            :                              int mandatory, size_t len)
     153                 :            : {
     154                 :            :         struct ttls_avp_vendor *avp;
     155                 :            :         u8 flags;
     156                 :            :         size_t hdrlen;
     157                 :            : 
     158                 :        240 :         avp = (struct ttls_avp_vendor *) avphdr;
     159         [ +  - ]:        240 :         flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
     160         [ +  + ]:        240 :         if (vendor_id) {
     161                 :        124 :                 flags |= AVP_FLAGS_VENDOR;
     162                 :        124 :                 hdrlen = sizeof(*avp);
     163                 :        124 :                 avp->vendor_id = host_to_be32(vendor_id);
     164                 :            :         } else {
     165                 :        116 :                 hdrlen = sizeof(struct ttls_avp);
     166                 :            :         }
     167                 :            : 
     168                 :        240 :         avp->avp_code = host_to_be32(avp_code);
     169                 :        240 :         avp->avp_length = host_to_be32((flags << 24) | (u32) (hdrlen + len));
     170                 :            : 
     171                 :        240 :         return avphdr + hdrlen;
     172                 :            : }
     173                 :            : 
     174                 :            : 
     175                 :        135 : static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
     176                 :            :                              u32 vendor_id, int mandatory,
     177                 :            :                              const u8 *data, size_t len)
     178                 :            : {
     179                 :            :         u8 *pos;
     180                 :        135 :         pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
     181                 :        135 :         os_memcpy(pos, data, len);
     182                 :        135 :         pos += len;
     183                 :        135 :         AVP_PAD(start, pos);
     184                 :        135 :         return pos;
     185                 :            : }
     186                 :            : 
     187                 :            : 
     188                 :         36 : static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
     189                 :            :                                     int mandatory)
     190                 :            : {
     191                 :            :         struct wpabuf *msg;
     192                 :            :         u8 *avp, *pos;
     193                 :            : 
     194                 :         36 :         msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
     195         [ -  + ]:         36 :         if (msg == NULL) {
     196                 :          0 :                 wpabuf_free(*resp);
     197                 :          0 :                 *resp = NULL;
     198                 :          0 :                 return -1;
     199                 :            :         }
     200                 :            : 
     201                 :         36 :         avp = wpabuf_mhead(msg);
     202                 :         36 :         pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
     203                 :         36 :         os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
     204                 :         36 :         pos += wpabuf_len(*resp);
     205                 :         36 :         AVP_PAD(avp, pos);
     206                 :         36 :         wpabuf_free(*resp);
     207                 :         36 :         wpabuf_put(msg, pos - avp);
     208                 :         36 :         *resp = msg;
     209                 :         36 :         return 0;
     210                 :            : }
     211                 :            : 
     212                 :            : 
     213                 :         81 : static int eap_ttls_v0_derive_key(struct eap_sm *sm,
     214                 :            :                                   struct eap_ttls_data *data)
     215                 :            : {
     216                 :         81 :         os_free(data->key_data);
     217                 :         81 :         data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
     218                 :            :                                                  "ttls keying material",
     219                 :            :                                                  EAP_TLS_KEY_LEN);
     220         [ -  + ]:         81 :         if (!data->key_data) {
     221                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
     222                 :          0 :                 return -1;
     223                 :            :         }
     224                 :            : 
     225                 :         81 :         wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
     226                 :         81 :                         data->key_data, EAP_TLS_KEY_LEN);
     227                 :            : 
     228                 :         81 :         os_free(data->session_id);
     229                 :         81 :         data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
     230                 :            :                                                           EAP_TYPE_TTLS,
     231                 :            :                                                           &data->id_len);
     232         [ +  - ]:         81 :         if (data->session_id) {
     233                 :         81 :                 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Derived Session-Id",
     234                 :         81 :                             data->session_id, data->id_len);
     235                 :            :         } else {
     236                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to derive Session-Id");
     237                 :            :         }
     238                 :            : 
     239                 :         81 :         return 0;
     240                 :            : }
     241                 :            : 
     242                 :            : 
     243                 :         66 : static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
     244                 :            :                                         struct eap_ttls_data *data, size_t len)
     245                 :            : {
     246                 :         66 :         return eap_peer_tls_derive_key(sm, &data->ssl, "ttls challenge", len);
     247                 :            : }
     248                 :            : 
     249                 :            : 
     250                 :         12 : static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
     251                 :            :                                               u8 method)
     252                 :            : {
     253                 :            :         size_t i;
     254         [ +  + ]:         15 :         for (i = 0; i < data->num_phase2_eap_types; i++) {
     255 [ +  - ][ +  + ]:         12 :                 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
     256                 :         12 :                     data->phase2_eap_types[i].method != method)
     257                 :          3 :                         continue;
     258                 :            : 
     259                 :          9 :                 data->phase2_eap_type.vendor =
     260                 :          9 :                         data->phase2_eap_types[i].vendor;
     261                 :          9 :                 data->phase2_eap_type.method =
     262                 :          9 :                         data->phase2_eap_types[i].method;
     263                 :          9 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
     264                 :            :                            "Phase 2 EAP vendor %d method %d",
     265                 :            :                            data->phase2_eap_type.vendor,
     266                 :            :                            data->phase2_eap_type.method);
     267                 :          9 :                 break;
     268                 :            :         }
     269                 :         12 : }
     270                 :            : 
     271                 :            : 
     272                 :         21 : static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
     273                 :            :                                        struct eap_ttls_data *data,
     274                 :            :                                        struct eap_method_ret *ret,
     275                 :            :                                        struct eap_hdr *hdr, size_t len,
     276                 :            :                                        struct wpabuf **resp)
     277                 :            : {
     278                 :            :         struct wpabuf msg;
     279                 :            :         struct eap_method_ret iret;
     280                 :            : 
     281                 :         21 :         os_memset(&iret, 0, sizeof(iret));
     282                 :         21 :         wpabuf_set(&msg, hdr, len);
     283                 :         21 :         *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
     284                 :            :                                              &msg);
     285 [ +  + ][ +  + ]:         21 :         if ((iret.methodState == METHOD_DONE ||
     286         [ +  + ]:         19 :              iret.methodState == METHOD_MAY_CONT) &&
     287         [ +  + ]:         13 :             (iret.decision == DECISION_UNCOND_SUCC ||
     288         [ +  - ]:          8 :              iret.decision == DECISION_COND_SUCC ||
     289                 :          8 :              iret.decision == DECISION_FAIL)) {
     290                 :         19 :                 ret->methodState = iret.methodState;
     291                 :         19 :                 ret->decision = iret.decision;
     292                 :            :         }
     293                 :            : 
     294                 :         21 :         return 0;
     295                 :            : }
     296                 :            : 
     297                 :            : 
     298                 :         26 : static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
     299                 :            :                                               struct eap_ttls_data *data,
     300                 :            :                                               struct eap_method_ret *ret,
     301                 :            :                                               struct eap_hdr *hdr, size_t len,
     302                 :            :                                               u8 method, struct wpabuf **resp)
     303                 :            : {
     304                 :            : #ifdef EAP_TNC
     305 [ -  + ][ #  # ]:         26 :         if (data->tnc_started && data->phase2_method &&
                 [ #  # ]
     306 [ #  # ][ #  # ]:          0 :             data->phase2_priv && method == EAP_TYPE_TNC &&
     307                 :          0 :             data->phase2_eap_type.method == EAP_TYPE_TNC)
     308                 :          0 :                 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
     309                 :            :                                                    resp);
     310                 :            : 
     311 [ -  + ][ #  # ]:         26 :         if (data->ready_for_tnc && !data->tnc_started &&
                 [ #  # ]
     312                 :            :             method == EAP_TYPE_TNC) {
     313                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
     314                 :            :                            "EAP method");
     315                 :          0 :                 data->tnc_started = 1;
     316                 :            :         }
     317                 :            : 
     318         [ -  + ]:         26 :         if (data->tnc_started) {
     319 [ #  # ][ #  # ]:          0 :                 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
     320                 :          0 :                     data->phase2_eap_type.method == EAP_TYPE_TNC) {
     321                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
     322                 :            :                                    "type %d for TNC", method);
     323                 :          0 :                         return -1;
     324                 :            :                 }
     325                 :            : 
     326                 :          0 :                 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
     327                 :          0 :                 data->phase2_eap_type.method = method;
     328                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
     329                 :            :                            "Phase 2 EAP vendor %d method %d (TNC)",
     330                 :            :                            data->phase2_eap_type.vendor,
     331                 :            :                            data->phase2_eap_type.method);
     332                 :            : 
     333         [ #  # ]:          0 :                 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
     334                 :          0 :                         eap_ttls_phase2_eap_deinit(sm, data);
     335                 :            :         }
     336                 :            : #endif /* EAP_TNC */
     337                 :            : 
     338 [ +  - ][ +  + ]:         26 :         if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
     339                 :         26 :             data->phase2_eap_type.method == EAP_TYPE_NONE)
     340                 :         12 :                 eap_ttls_phase2_select_eap_method(data, method);
     341                 :            : 
     342 [ +  + ][ -  + ]:         26 :         if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
     343                 :            :         {
     344         [ -  + ]:          5 :                 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
     345                 :            :                                             data->num_phase2_eap_types,
     346                 :            :                                             hdr, resp))
     347                 :          0 :                         return -1;
     348                 :          5 :                 return 0;
     349                 :            :         }
     350                 :            : 
     351         [ +  + ]:         21 :         if (data->phase2_priv == NULL) {
     352                 :          9 :                 data->phase2_method = eap_peer_get_eap_method(
     353                 :            :                         EAP_VENDOR_IETF, method);
     354         [ +  - ]:          9 :                 if (data->phase2_method) {
     355                 :          9 :                         sm->init_phase2 = 1;
     356                 :          9 :                         data->phase2_priv = data->phase2_method->init(sm);
     357                 :          9 :                         sm->init_phase2 = 0;
     358                 :            :                 }
     359                 :            :         }
     360 [ +  - ][ -  + ]:         21 :         if (data->phase2_priv == NULL || data->phase2_method == NULL) {
     361                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
     362                 :            :                            "Phase 2 EAP method %d", method);
     363                 :          0 :                 return -1;
     364                 :            :         }
     365                 :            : 
     366                 :         26 :         return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
     367                 :            : }
     368                 :            : 
     369                 :            : 
     370                 :         38 : static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
     371                 :            :                                        struct eap_ttls_data *data,
     372                 :            :                                        struct eap_method_ret *ret,
     373                 :            :                                        struct eap_hdr *hdr,
     374                 :            :                                        struct wpabuf **resp)
     375                 :            : {
     376                 :         38 :         size_t len = be_to_host16(hdr->length);
     377                 :            :         u8 *pos;
     378                 :         38 :         struct eap_peer_config *config = eap_get_config(sm);
     379                 :            : 
     380         [ -  + ]:         38 :         if (len <= sizeof(struct eap_hdr)) {
     381                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
     382                 :            :                            "Phase 2 request (len=%lu)", (unsigned long) len);
     383                 :          0 :                 return -1;
     384                 :            :         }
     385                 :         38 :         pos = (u8 *) (hdr + 1);
     386                 :         38 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
     387         [ +  + ]:         38 :         switch (*pos) {
     388                 :            :         case EAP_TYPE_IDENTITY:
     389                 :         12 :                 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
     390                 :         12 :                 break;
     391                 :            :         default:
     392         [ -  + ]:         26 :                 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
     393                 :         26 :                                                        *pos, resp) < 0)
     394                 :          0 :                         return -1;
     395                 :         26 :                 break;
     396                 :            :         }
     397                 :            : 
     398 [ +  + ][ +  - ]:         38 :         if (*resp == NULL &&
     399 [ -  + ][ #  # ]:          2 :             (config->pending_req_identity || config->pending_req_password ||
     400                 :          0 :              config->pending_req_otp)) {
     401                 :          2 :                 return 0;
     402                 :            :         }
     403                 :            : 
     404         [ -  + ]:         36 :         if (*resp == NULL)
     405                 :          0 :                 return -1;
     406                 :            : 
     407                 :         36 :         wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
     408                 :            :                         *resp);
     409                 :         38 :         return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
     410                 :            : }
     411                 :            : 
     412                 :            : 
     413                 :         56 : static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
     414                 :            :                                             struct eap_ttls_data *data,
     415                 :            :                                             struct eap_method_ret *ret,
     416                 :            :                                             struct wpabuf **resp)
     417                 :            : {
     418                 :            : #ifdef EAP_MSCHAPv2
     419                 :            :         struct wpabuf *msg;
     420                 :            :         u8 *buf, *pos, *challenge, *peer_challenge;
     421                 :            :         const u8 *identity, *password;
     422                 :            :         size_t identity_len, password_len;
     423                 :            :         int pwhash;
     424                 :            : 
     425                 :         56 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
     426                 :            : 
     427                 :         56 :         identity = eap_get_config_identity(sm, &identity_len);
     428                 :         56 :         password = eap_get_config_password2(sm, &password_len, &pwhash);
     429 [ +  - ][ -  + ]:         56 :         if (identity == NULL || password == NULL)
     430                 :          0 :                 return -1;
     431                 :            : 
     432                 :         56 :         msg = wpabuf_alloc(identity_len + 1000);
     433         [ -  + ]:         56 :         if (msg == NULL) {
     434                 :          0 :                 wpa_printf(MSG_ERROR,
     435                 :            :                            "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
     436                 :          0 :                 return -1;
     437                 :            :         }
     438                 :         56 :         pos = buf = wpabuf_mhead(msg);
     439                 :            : 
     440                 :            :         /* User-Name */
     441                 :         56 :         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
     442                 :            :                                identity, identity_len);
     443                 :            : 
     444                 :            :         /* MS-CHAP-Challenge */
     445                 :         56 :         challenge = eap_ttls_implicit_challenge(
     446                 :            :                 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
     447         [ -  + ]:         56 :         if (challenge == NULL) {
     448                 :          0 :                 wpabuf_free(msg);
     449                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
     450                 :            :                            "implicit challenge");
     451                 :          0 :                 return -1;
     452                 :            :         }
     453                 :            : 
     454                 :         56 :         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
     455                 :            :                                RADIUS_VENDOR_ID_MICROSOFT, 1,
     456                 :            :                                challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
     457                 :            : 
     458                 :            :         /* MS-CHAP2-Response */
     459                 :         56 :         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
     460                 :            :                                RADIUS_VENDOR_ID_MICROSOFT, 1,
     461                 :            :                                EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
     462                 :         56 :         data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
     463                 :         56 :         *pos++ = data->ident;
     464                 :         56 :         *pos++ = 0; /* Flags */
     465         [ -  + ]:         56 :         if (os_get_random(pos, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) < 0) {
     466                 :          0 :                 os_free(challenge);
     467                 :          0 :                 wpabuf_free(msg);
     468                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to get "
     469                 :            :                            "random data for peer challenge");
     470                 :          0 :                 return -1;
     471                 :            :         }
     472                 :         56 :         peer_challenge = pos;
     473                 :         56 :         pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
     474                 :         56 :         os_memset(pos, 0, 8); /* Reserved, must be zero */
     475                 :         56 :         pos += 8;
     476         [ -  + ]:         56 :         if (mschapv2_derive_response(identity, identity_len, password,
     477                 :            :                                      password_len, pwhash, challenge,
     478                 :         56 :                                      peer_challenge, pos, data->auth_response,
     479                 :         56 :                                      data->master_key)) {
     480                 :          0 :                 os_free(challenge);
     481                 :          0 :                 wpabuf_free(msg);
     482                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
     483                 :            :                            "response");
     484                 :          0 :                 return -1;
     485                 :            :         }
     486                 :         56 :         data->auth_response_valid = 1;
     487                 :            : 
     488                 :         56 :         pos += 24;
     489                 :         56 :         os_free(challenge);
     490                 :         56 :         AVP_PAD(buf, pos);
     491                 :            : 
     492                 :         56 :         wpabuf_put(msg, pos - buf);
     493                 :         56 :         *resp = msg;
     494                 :            : 
     495         [ +  - ]:         56 :         if (sm->workaround) {
     496                 :            :                 /* At least FreeRADIUS seems to be terminating
     497                 :            :                  * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
     498                 :            :                  * packet. */
     499                 :         56 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
     500                 :            :                            "allow success without tunneled response");
     501                 :         56 :                 ret->methodState = METHOD_MAY_CONT;
     502                 :         56 :                 ret->decision = DECISION_COND_SUCC;
     503                 :            :         }
     504                 :            : 
     505                 :         56 :         return 0;
     506                 :            : #else /* EAP_MSCHAPv2 */
     507                 :            :         wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
     508                 :            :         return -1;
     509                 :            : #endif /* EAP_MSCHAPv2 */
     510                 :            : }
     511                 :            : 
     512                 :            : 
     513                 :          6 : static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
     514                 :            :                                           struct eap_ttls_data *data,
     515                 :            :                                           struct eap_method_ret *ret,
     516                 :            :                                           struct wpabuf **resp)
     517                 :            : {
     518                 :            :         struct wpabuf *msg;
     519                 :            :         u8 *buf, *pos, *challenge;
     520                 :            :         const u8 *identity, *password;
     521                 :            :         size_t identity_len, password_len;
     522                 :            :         int pwhash;
     523                 :            : 
     524                 :          6 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
     525                 :            : 
     526                 :          6 :         identity = eap_get_config_identity(sm, &identity_len);
     527                 :          6 :         password = eap_get_config_password2(sm, &password_len, &pwhash);
     528 [ +  - ][ -  + ]:          6 :         if (identity == NULL || password == NULL)
     529                 :          0 :                 return -1;
     530                 :            : 
     531                 :          6 :         msg = wpabuf_alloc(identity_len + 1000);
     532         [ -  + ]:          6 :         if (msg == NULL) {
     533                 :          0 :                 wpa_printf(MSG_ERROR,
     534                 :            :                            "EAP-TTLS/MSCHAP: Failed to allocate memory");
     535                 :          0 :                 return -1;
     536                 :            :         }
     537                 :          6 :         pos = buf = wpabuf_mhead(msg);
     538                 :            : 
     539                 :            :         /* User-Name */
     540                 :          6 :         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
     541                 :            :                                identity, identity_len);
     542                 :            : 
     543                 :            :         /* MS-CHAP-Challenge */
     544                 :          6 :         challenge = eap_ttls_implicit_challenge(
     545                 :            :                 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
     546         [ -  + ]:          6 :         if (challenge == NULL) {
     547                 :          0 :                 wpabuf_free(msg);
     548                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
     549                 :            :                            "implicit challenge");
     550                 :          0 :                 return -1;
     551                 :            :         }
     552                 :            : 
     553                 :          6 :         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
     554                 :            :                                RADIUS_VENDOR_ID_MICROSOFT, 1,
     555                 :            :                                challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
     556                 :            : 
     557                 :            :         /* MS-CHAP-Response */
     558                 :          6 :         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
     559                 :            :                                RADIUS_VENDOR_ID_MICROSOFT, 1,
     560                 :            :                                EAP_TTLS_MSCHAP_RESPONSE_LEN);
     561                 :          6 :         data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
     562                 :          6 :         *pos++ = data->ident;
     563                 :          6 :         *pos++ = 1; /* Flags: Use NT style passwords */
     564                 :          6 :         os_memset(pos, 0, 24); /* LM-Response */
     565                 :          6 :         pos += 24;
     566         [ -  + ]:          6 :         if (pwhash) {
     567                 :          0 :                 challenge_response(challenge, password, pos); /* NT-Response */
     568                 :          0 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
     569                 :            :                                 password, 16);
     570                 :            :         } else {
     571                 :          6 :                 nt_challenge_response(challenge, password, password_len,
     572                 :            :                                       pos); /* NT-Response */
     573                 :          6 :                 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
     574                 :            :                                       password, password_len);
     575                 :            :         }
     576                 :          6 :         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
     577                 :            :                     challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
     578                 :          6 :         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
     579                 :          6 :         pos += 24;
     580                 :          6 :         os_free(challenge);
     581                 :          6 :         AVP_PAD(buf, pos);
     582                 :            : 
     583                 :          6 :         wpabuf_put(msg, pos - buf);
     584                 :          6 :         *resp = msg;
     585                 :            : 
     586                 :            :         /* EAP-TTLS/MSCHAP does not provide tunneled success
     587                 :            :          * notification, so assume that Phase2 succeeds. */
     588                 :          6 :         ret->methodState = METHOD_DONE;
     589                 :          6 :         ret->decision = DECISION_COND_SUCC;
     590                 :            : 
     591                 :          6 :         return 0;
     592                 :            : }
     593                 :            : 
     594                 :            : 
     595                 :          3 : static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
     596                 :            :                                        struct eap_ttls_data *data,
     597                 :            :                                        struct eap_method_ret *ret,
     598                 :            :                                        struct wpabuf **resp)
     599                 :            : {
     600                 :            :         struct wpabuf *msg;
     601                 :            :         u8 *buf, *pos;
     602                 :            :         size_t pad;
     603                 :            :         const u8 *identity, *password;
     604                 :            :         size_t identity_len, password_len;
     605                 :            : 
     606                 :          3 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
     607                 :            : 
     608                 :          3 :         identity = eap_get_config_identity(sm, &identity_len);
     609                 :          3 :         password = eap_get_config_password(sm, &password_len);
     610 [ +  - ][ -  + ]:          3 :         if (identity == NULL || password == NULL)
     611                 :          0 :                 return -1;
     612                 :            : 
     613                 :          3 :         msg = wpabuf_alloc(identity_len + password_len + 100);
     614         [ -  + ]:          3 :         if (msg == NULL) {
     615                 :          0 :                 wpa_printf(MSG_ERROR,
     616                 :            :                            "EAP-TTLS/PAP: Failed to allocate memory");
     617                 :          0 :                 return -1;
     618                 :            :         }
     619                 :          3 :         pos = buf = wpabuf_mhead(msg);
     620                 :            : 
     621                 :            :         /* User-Name */
     622                 :          3 :         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
     623                 :            :                                identity, identity_len);
     624                 :            : 
     625                 :            :         /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
     626                 :            :          * the data, so no separate encryption is used in the AVP itself.
     627                 :            :          * However, the password is padded to obfuscate its length. */
     628         [ +  - ]:          3 :         pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
     629                 :          3 :         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
     630                 :            :                                password_len + pad);
     631                 :          3 :         os_memcpy(pos, password, password_len);
     632                 :          3 :         pos += password_len;
     633                 :          3 :         os_memset(pos, 0, pad);
     634                 :          3 :         pos += pad;
     635                 :          3 :         AVP_PAD(buf, pos);
     636                 :            : 
     637                 :          3 :         wpabuf_put(msg, pos - buf);
     638                 :          3 :         *resp = msg;
     639                 :            : 
     640                 :            :         /* EAP-TTLS/PAP does not provide tunneled success notification,
     641                 :            :          * so assume that Phase2 succeeds. */
     642                 :          3 :         ret->methodState = METHOD_DONE;
     643                 :          3 :         ret->decision = DECISION_COND_SUCC;
     644                 :            : 
     645                 :          3 :         return 0;
     646                 :            : }
     647                 :            : 
     648                 :            : 
     649                 :          4 : static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
     650                 :            :                                         struct eap_ttls_data *data,
     651                 :            :                                         struct eap_method_ret *ret,
     652                 :            :                                         struct wpabuf **resp)
     653                 :            : {
     654                 :            :         struct wpabuf *msg;
     655                 :            :         u8 *buf, *pos, *challenge;
     656                 :            :         const u8 *identity, *password;
     657                 :            :         size_t identity_len, password_len;
     658                 :            : 
     659                 :          4 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
     660                 :            : 
     661                 :          4 :         identity = eap_get_config_identity(sm, &identity_len);
     662                 :          4 :         password = eap_get_config_password(sm, &password_len);
     663 [ +  - ][ -  + ]:          4 :         if (identity == NULL || password == NULL)
     664                 :          0 :                 return -1;
     665                 :            : 
     666                 :          4 :         msg = wpabuf_alloc(identity_len + 1000);
     667         [ -  + ]:          4 :         if (msg == NULL) {
     668                 :          0 :                 wpa_printf(MSG_ERROR,
     669                 :            :                            "EAP-TTLS/CHAP: Failed to allocate memory");
     670                 :          0 :                 return -1;
     671                 :            :         }
     672                 :          4 :         pos = buf = wpabuf_mhead(msg);
     673                 :            : 
     674                 :            :         /* User-Name */
     675                 :          4 :         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
     676                 :            :                                identity, identity_len);
     677                 :            : 
     678                 :            :         /* CHAP-Challenge */
     679                 :          4 :         challenge = eap_ttls_implicit_challenge(
     680                 :            :                 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
     681         [ -  + ]:          4 :         if (challenge == NULL) {
     682                 :          0 :                 wpabuf_free(msg);
     683                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
     684                 :            :                            "implicit challenge");
     685                 :          0 :                 return -1;
     686                 :            :         }
     687                 :            : 
     688                 :          4 :         pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
     689                 :            :                                challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
     690                 :            : 
     691                 :            :         /* CHAP-Password */
     692                 :          4 :         pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
     693                 :            :                                1 + EAP_TTLS_CHAP_PASSWORD_LEN);
     694                 :          4 :         data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
     695                 :          4 :         *pos++ = data->ident;
     696                 :            : 
     697                 :            :         /* MD5(Ident + Password + Challenge) */
     698                 :          4 :         chap_md5(data->ident, password, password_len, challenge,
     699                 :            :                  EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
     700                 :            : 
     701                 :          4 :         wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
     702                 :            :                           identity, identity_len);
     703                 :          4 :         wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
     704                 :            :                               password, password_len);
     705                 :          4 :         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
     706                 :            :                     challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
     707                 :          4 :         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
     708                 :            :                     pos, EAP_TTLS_CHAP_PASSWORD_LEN);
     709                 :          4 :         pos += EAP_TTLS_CHAP_PASSWORD_LEN;
     710                 :          4 :         os_free(challenge);
     711                 :          4 :         AVP_PAD(buf, pos);
     712                 :            : 
     713                 :          4 :         wpabuf_put(msg, pos - buf);
     714                 :          4 :         *resp = msg;
     715                 :            : 
     716                 :            :         /* EAP-TTLS/CHAP does not provide tunneled success
     717                 :            :          * notification, so assume that Phase2 succeeds. */
     718                 :          4 :         ret->methodState = METHOD_DONE;
     719                 :          4 :         ret->decision = DECISION_COND_SUCC;
     720                 :            : 
     721                 :          4 :         return 0;
     722                 :            : }
     723                 :            : 
     724                 :            : 
     725                 :        110 : static int eap_ttls_phase2_request(struct eap_sm *sm,
     726                 :            :                                    struct eap_ttls_data *data,
     727                 :            :                                    struct eap_method_ret *ret,
     728                 :            :                                    struct eap_hdr *hdr,
     729                 :            :                                    struct wpabuf **resp)
     730                 :            : {
     731                 :        110 :         int res = 0;
     732                 :            :         size_t len;
     733                 :        110 :         enum phase2_types phase2_type = data->phase2_type;
     734                 :            : 
     735                 :            : #ifdef EAP_TNC
     736         [ -  + ]:        110 :         if (data->tnc_started) {
     737                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
     738                 :          0 :                 phase2_type = EAP_TTLS_PHASE2_EAP;
     739                 :            :         }
     740                 :            : #endif /* EAP_TNC */
     741                 :            : 
     742 [ +  + ][ +  + ]:        110 :         if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
     743         [ +  + ]:         45 :             phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
     744         [ +  + ]:         42 :             phase2_type == EAP_TTLS_PHASE2_PAP ||
     745                 :            :             phase2_type == EAP_TTLS_PHASE2_CHAP) {
     746         [ +  + ]:         72 :                 if (eap_get_config_identity(sm, &len) == NULL) {
     747                 :          1 :                         wpa_printf(MSG_INFO,
     748                 :            :                                    "EAP-TTLS: Identity not configured");
     749                 :          1 :                         eap_sm_request_identity(sm);
     750         [ +  - ]:          1 :                         if (eap_get_config_password(sm, &len) == NULL)
     751                 :          1 :                                 eap_sm_request_password(sm);
     752                 :          1 :                         return 0;
     753                 :            :                 }
     754                 :            : 
     755         [ +  + ]:         71 :                 if (eap_get_config_password(sm, &len) == NULL) {
     756                 :          2 :                         wpa_printf(MSG_INFO,
     757                 :            :                                    "EAP-TTLS: Password not configured");
     758                 :          2 :                         eap_sm_request_password(sm);
     759                 :          2 :                         return 0;
     760                 :            :                 }
     761                 :            :         }
     762                 :            : 
     763   [ +  +  +  +  :        107 :         switch (phase2_type) {
                   +  - ]
     764                 :            :         case EAP_TTLS_PHASE2_EAP:
     765                 :         38 :                 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
     766                 :         38 :                 break;
     767                 :            :         case EAP_TTLS_PHASE2_MSCHAPV2:
     768                 :         56 :                 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
     769                 :         56 :                 break;
     770                 :            :         case EAP_TTLS_PHASE2_MSCHAP:
     771                 :          6 :                 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
     772                 :          6 :                 break;
     773                 :            :         case EAP_TTLS_PHASE2_PAP:
     774                 :          3 :                 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
     775                 :          3 :                 break;
     776                 :            :         case EAP_TTLS_PHASE2_CHAP:
     777                 :          4 :                 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
     778                 :          4 :                 break;
     779                 :            :         default:
     780                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
     781                 :          0 :                 res = -1;
     782                 :          0 :                 break;
     783                 :            :         }
     784                 :            : 
     785         [ -  + ]:        107 :         if (res < 0) {
     786                 :          0 :                 ret->methodState = METHOD_DONE;
     787                 :          0 :                 ret->decision = DECISION_FAIL;
     788                 :            :         }
     789                 :            : 
     790                 :        110 :         return res;
     791                 :            : }
     792                 :            : 
     793                 :            : 
     794                 :            : struct ttls_parse_avp {
     795                 :            :         u8 *mschapv2;
     796                 :            :         u8 *eapdata;
     797                 :            :         size_t eap_len;
     798                 :            :         int mschapv2_error;
     799                 :            : };
     800                 :            : 
     801                 :            : 
     802                 :         26 : static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
     803                 :            :                                    struct ttls_parse_avp *parse)
     804                 :            : {
     805                 :         26 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
     806         [ +  - ]:         26 :         if (parse->eapdata == NULL) {
     807                 :         26 :                 parse->eapdata = os_malloc(dlen);
     808         [ -  + ]:         26 :                 if (parse->eapdata == NULL) {
     809                 :          0 :                         wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
     810                 :            :                                    "memory for Phase 2 EAP data");
     811                 :          0 :                         return -1;
     812                 :            :                 }
     813                 :         26 :                 os_memcpy(parse->eapdata, dpos, dlen);
     814                 :         26 :                 parse->eap_len = dlen;
     815                 :            :         } else {
     816                 :          0 :                 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
     817         [ #  # ]:          0 :                 if (neweap == NULL) {
     818                 :          0 :                         wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
     819                 :            :                                    "memory for Phase 2 EAP data");
     820                 :          0 :                         return -1;
     821                 :            :                 }
     822                 :          0 :                 os_memcpy(neweap + parse->eap_len, dpos, dlen);
     823                 :          0 :                 parse->eapdata = neweap;
     824                 :          0 :                 parse->eap_len += dlen;
     825                 :            :         }
     826                 :            : 
     827                 :         26 :         return 0;
     828                 :            : }
     829                 :            : 
     830                 :            : 
     831                 :         82 : static int eap_ttls_parse_avp(u8 *pos, size_t left,
     832                 :            :                               struct ttls_parse_avp *parse)
     833                 :            : {
     834                 :            :         struct ttls_avp *avp;
     835                 :         82 :         u32 avp_code, avp_length, vendor_id = 0;
     836                 :            :         u8 avp_flags, *dpos;
     837                 :            :         size_t dlen;
     838                 :            : 
     839                 :         82 :         avp = (struct ttls_avp *) pos;
     840                 :         82 :         avp_code = be_to_host32(avp->avp_code);
     841                 :         82 :         avp_length = be_to_host32(avp->avp_length);
     842                 :         82 :         avp_flags = (avp_length >> 24) & 0xff;
     843                 :         82 :         avp_length &= 0xffffff;
     844                 :         82 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
     845                 :            :                    "length=%d", (int) avp_code, avp_flags,
     846                 :            :                    (int) avp_length);
     847                 :            : 
     848         [ -  + ]:         82 :         if (avp_length > left) {
     849                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
     850                 :            :                            "(len=%d, left=%lu) - dropped",
     851                 :            :                            (int) avp_length, (unsigned long) left);
     852                 :          0 :                 return -1;
     853                 :            :         }
     854                 :            : 
     855         [ -  + ]:         82 :         if (avp_length < sizeof(*avp)) {
     856                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
     857                 :            :                            avp_length);
     858                 :          0 :                 return -1;
     859                 :            :         }
     860                 :            : 
     861                 :         82 :         dpos = (u8 *) (avp + 1);
     862                 :         82 :         dlen = avp_length - sizeof(*avp);
     863         [ +  + ]:         82 :         if (avp_flags & AVP_FLAGS_VENDOR) {
     864         [ -  + ]:         56 :                 if (dlen < 4) {
     865                 :          0 :                         wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
     866                 :            :                                    "underflow");
     867                 :          0 :                         return -1;
     868                 :            :                 }
     869                 :         56 :                 vendor_id = WPA_GET_BE32(dpos);
     870                 :         56 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
     871                 :            :                            (int) vendor_id);
     872                 :         56 :                 dpos += 4;
     873                 :         56 :                 dlen -= 4;
     874                 :            :         }
     875                 :            : 
     876                 :         82 :         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
     877                 :            : 
     878 [ +  + ][ +  - ]:         82 :         if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
     879         [ -  + ]:         26 :                 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
     880                 :          0 :                         return -1;
     881 [ -  + ][ #  # ]:         56 :         } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
     882                 :            :                 /* This is an optional message that can be displayed to
     883                 :            :                  * the user. */
     884                 :          0 :                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
     885                 :            :                                   dpos, dlen);
     886 [ +  - ][ +  + ]:         56 :         } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
     887                 :            :                    avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
     888                 :         55 :                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
     889                 :            :                                   dpos, dlen);
     890         [ -  + ]:         55 :                 if (dlen != 43) {
     891                 :          0 :                         wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
     892                 :            :                                    "MS-CHAP2-Success length "
     893                 :            :                                    "(len=%lu, expected 43)",
     894                 :            :                                    (unsigned long) dlen);
     895                 :          0 :                         return -1;
     896                 :            :                 }
     897                 :         55 :                 parse->mschapv2 = dpos;
     898 [ +  - ][ +  - ]:          1 :         } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
     899                 :            :                    avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
     900                 :          1 :                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
     901                 :            :                                   dpos, dlen);
     902                 :          1 :                 parse->mschapv2_error = 1;
     903         [ #  # ]:          0 :         } else if (avp_flags & AVP_FLAGS_MANDATORY) {
     904                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
     905                 :            :                            "code %d vendor_id %d - dropped",
     906                 :            :                            (int) avp_code, (int) vendor_id);
     907                 :          0 :                 return -1;
     908                 :            :         } else {
     909                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
     910                 :            :                            "code %d vendor_id %d",
     911                 :            :                            (int) avp_code, (int) vendor_id);
     912                 :            :         }
     913                 :            : 
     914                 :         82 :         return avp_length;
     915                 :            : }
     916                 :            : 
     917                 :            : 
     918                 :         82 : static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
     919                 :            :                                struct ttls_parse_avp *parse)
     920                 :            : {
     921                 :            :         u8 *pos;
     922                 :            :         size_t left, pad;
     923                 :            :         int avp_length;
     924                 :            : 
     925                 :         82 :         pos = wpabuf_mhead(in_decrypted);
     926                 :         82 :         left = wpabuf_len(in_decrypted);
     927                 :         82 :         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
     928         [ -  + ]:         82 :         if (left < sizeof(struct ttls_avp)) {
     929                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
     930                 :            :                            " len=%lu expected %lu or more - dropped",
     931                 :            :                            (unsigned long) left,
     932                 :            :                            (unsigned long) sizeof(struct ttls_avp));
     933                 :          0 :                 return -1;
     934                 :            :         }
     935                 :            : 
     936                 :            :         /* Parse AVPs */
     937                 :         82 :         os_memset(parse, 0, sizeof(*parse));
     938                 :            : 
     939         [ +  + ]:        164 :         while (left > 0) {
     940                 :         82 :                 avp_length = eap_ttls_parse_avp(pos, left, parse);
     941         [ -  + ]:         82 :                 if (avp_length < 0)
     942                 :          0 :                         return -1;
     943                 :            : 
     944                 :         82 :                 pad = (4 - (avp_length & 3)) & 3;
     945                 :         82 :                 pos += avp_length + pad;
     946         [ +  + ]:         82 :                 if (left < avp_length + pad)
     947                 :         55 :                         left = 0;
     948                 :            :                 else
     949                 :         27 :                         left -= avp_length + pad;
     950                 :            :         }
     951                 :            : 
     952                 :         82 :         return 0;
     953                 :            : }
     954                 :            : 
     955                 :            : 
     956                 :         84 : static u8 * eap_ttls_fake_identity_request(void)
     957                 :            : {
     958                 :            :         struct eap_hdr *hdr;
     959                 :            :         u8 *buf;
     960                 :            : 
     961                 :         84 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
     962                 :            :                    "Phase 2 - use fake EAP-Request Identity");
     963                 :         84 :         buf = os_malloc(sizeof(*hdr) + 1);
     964         [ -  + ]:         84 :         if (buf == NULL) {
     965                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
     966                 :            :                            "memory for fake EAP-Identity Request");
     967                 :          0 :                 return NULL;
     968                 :            :         }
     969                 :            : 
     970                 :         84 :         hdr = (struct eap_hdr *) buf;
     971                 :         84 :         hdr->code = EAP_CODE_REQUEST;
     972                 :         84 :         hdr->identifier = 0;
     973                 :         84 :         hdr->length = host_to_be16(sizeof(*hdr) + 1);
     974                 :         84 :         buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
     975                 :            : 
     976                 :         84 :         return buf;
     977                 :            : }
     978                 :            : 
     979                 :            : 
     980                 :        108 : static int eap_ttls_encrypt_response(struct eap_sm *sm,
     981                 :            :                                      struct eap_ttls_data *data,
     982                 :            :                                      struct wpabuf *resp, u8 identifier,
     983                 :            :                                      struct wpabuf **out_data)
     984                 :            : {
     985         [ +  + ]:        108 :         if (resp == NULL)
     986                 :          3 :                 return 0;
     987                 :            : 
     988                 :        105 :         wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
     989                 :            :                             resp);
     990         [ -  + ]:        105 :         if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
     991                 :            :                                  data->ttls_version, identifier,
     992                 :            :                                  resp, out_data)) {
     993                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
     994                 :            :                            "frame");
     995                 :          0 :                 return -1;
     996                 :            :         }
     997                 :        105 :         wpabuf_free(resp);
     998                 :            : 
     999                 :        108 :         return 0;
    1000                 :            : }
    1001                 :            : 
    1002                 :            : 
    1003                 :         26 : static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
    1004                 :            :                                        struct eap_ttls_data *data,
    1005                 :            :                                        struct eap_method_ret *ret,
    1006                 :            :                                        struct ttls_parse_avp *parse,
    1007                 :            :                                        struct wpabuf **resp)
    1008                 :            : {
    1009                 :            :         struct eap_hdr *hdr;
    1010                 :            :         size_t len;
    1011                 :            : 
    1012         [ -  + ]:         26 :         if (parse->eapdata == NULL) {
    1013                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
    1014                 :            :                            "packet - dropped");
    1015                 :          0 :                 return -1;
    1016                 :            :         }
    1017                 :            : 
    1018                 :         26 :         wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
    1019                 :         26 :                     parse->eapdata, parse->eap_len);
    1020                 :         26 :         hdr = (struct eap_hdr *) parse->eapdata;
    1021                 :            : 
    1022         [ -  + ]:         26 :         if (parse->eap_len < sizeof(*hdr)) {
    1023                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
    1024                 :            :                            "frame (len=%lu, expected %lu or more) - dropped",
    1025                 :            :                            (unsigned long) parse->eap_len,
    1026                 :            :                            (unsigned long) sizeof(*hdr));
    1027                 :          0 :                 return -1;
    1028                 :            :         }
    1029                 :         26 :         len = be_to_host16(hdr->length);
    1030         [ -  + ]:         26 :         if (len > parse->eap_len) {
    1031                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
    1032                 :            :                            "EAP frame (EAP hdr len=%lu, EAP data len in "
    1033                 :            :                            "AVP=%lu)",
    1034                 :            :                            (unsigned long) len,
    1035                 :            :                            (unsigned long) parse->eap_len);
    1036                 :          0 :                 return -1;
    1037                 :            :         }
    1038                 :         26 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
    1039                 :            :                    "identifier=%d length=%lu",
    1040                 :         52 :                    hdr->code, hdr->identifier, (unsigned long) len);
    1041         [ +  - ]:         26 :         switch (hdr->code) {
    1042                 :            :         case EAP_CODE_REQUEST:
    1043         [ -  + ]:         26 :                 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
    1044                 :          0 :                         wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
    1045                 :            :                                    "processing failed");
    1046                 :          0 :                         return -1;
    1047                 :            :                 }
    1048                 :         26 :                 break;
    1049                 :            :         default:
    1050                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
    1051                 :          0 :                            "Phase 2 EAP header", hdr->code);
    1052                 :          0 :                 return -1;
    1053                 :            :         }
    1054                 :            : 
    1055                 :         26 :         return 0;
    1056                 :            : }
    1057                 :            : 
    1058                 :            : 
    1059                 :         56 : static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
    1060                 :            :                                             struct eap_ttls_data *data,
    1061                 :            :                                             struct eap_method_ret *ret,
    1062                 :            :                                             struct ttls_parse_avp *parse)
    1063                 :            : {
    1064                 :            : #ifdef EAP_MSCHAPv2
    1065         [ +  + ]:         56 :         if (parse->mschapv2_error) {
    1066                 :          1 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
    1067                 :            :                            "MS-CHAP-Error - failed");
    1068                 :          1 :                 ret->methodState = METHOD_DONE;
    1069                 :          1 :                 ret->decision = DECISION_FAIL;
    1070                 :            :                 /* Reply with empty data to ACK error */
    1071                 :          1 :                 return 1;
    1072                 :            :         }
    1073                 :            : 
    1074         [ -  + ]:         55 :         if (parse->mschapv2 == NULL) {
    1075                 :            : #ifdef EAP_TNC
    1076 [ #  # ][ #  # ]:          0 :                 if (data->phase2_success && parse->eapdata) {
    1077                 :            :                         /*
    1078                 :            :                          * Allow EAP-TNC to be started after successfully
    1079                 :            :                          * completed MSCHAPV2.
    1080                 :            :                          */
    1081                 :          0 :                         return 1;
    1082                 :            :                 }
    1083                 :            : #endif /* EAP_TNC */
    1084                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
    1085                 :            :                            "received for Phase2 MSCHAPV2");
    1086                 :          0 :                 return -1;
    1087                 :            :         }
    1088         [ -  + ]:         55 :         if (parse->mschapv2[0] != data->ident) {
    1089                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
    1090                 :            :                            "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
    1091                 :          0 :                            parse->mschapv2[0], data->ident);
    1092                 :          0 :                 return -1;
    1093                 :            :         }
    1094   [ +  -  -  + ]:        110 :         if (!data->auth_response_valid ||
    1095                 :         55 :             mschapv2_verify_auth_response(data->auth_response,
    1096                 :         55 :                                           parse->mschapv2 + 1, 42)) {
    1097                 :          0 :                 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
    1098                 :            :                            "response in Phase 2 MSCHAPV2 success request");
    1099                 :          0 :                 return -1;
    1100                 :            :         }
    1101                 :            : 
    1102                 :         55 :         wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
    1103                 :            :                    "authentication succeeded");
    1104                 :         55 :         ret->methodState = METHOD_DONE;
    1105                 :         55 :         ret->decision = DECISION_UNCOND_SUCC;
    1106                 :         55 :         data->phase2_success = 1;
    1107                 :            : 
    1108                 :            :         /*
    1109                 :            :          * Reply with empty data; authentication server will reply
    1110                 :            :          * with EAP-Success after this.
    1111                 :            :          */
    1112                 :         56 :         return 1;
    1113                 :            : #else /* EAP_MSCHAPv2 */
    1114                 :            :         wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
    1115                 :            :         return -1;
    1116                 :            : #endif /* EAP_MSCHAPv2 */
    1117                 :            : }
    1118                 :            : 
    1119                 :            : 
    1120                 :            : #ifdef EAP_TNC
    1121                 :          0 : static int eap_ttls_process_tnc_start(struct eap_sm *sm,
    1122                 :            :                                       struct eap_ttls_data *data,
    1123                 :            :                                       struct eap_method_ret *ret,
    1124                 :            :                                       struct ttls_parse_avp *parse,
    1125                 :            :                                       struct wpabuf **resp)
    1126                 :            : {
    1127                 :            :         /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
    1128         [ #  # ]:          0 :         if (parse->eapdata == NULL) {
    1129                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
    1130                 :            :                            "unexpected tunneled data (no EAP)");
    1131                 :          0 :                 return -1;
    1132                 :            :         }
    1133                 :            : 
    1134         [ #  # ]:          0 :         if (!data->ready_for_tnc) {
    1135                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
    1136                 :            :                            "EAP after non-EAP, but not ready for TNC");
    1137                 :          0 :                 return -1;
    1138                 :            :         }
    1139                 :            : 
    1140                 :          0 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
    1141                 :            :                    "non-EAP method");
    1142                 :          0 :         data->tnc_started = 1;
    1143                 :            : 
    1144         [ #  # ]:          0 :         if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
    1145                 :          0 :                 return -1;
    1146                 :            : 
    1147                 :          0 :         return 0;
    1148                 :            : }
    1149                 :            : #endif /* EAP_TNC */
    1150                 :            : 
    1151                 :            : 
    1152                 :         82 : static int eap_ttls_process_decrypted(struct eap_sm *sm,
    1153                 :            :                                       struct eap_ttls_data *data,
    1154                 :            :                                       struct eap_method_ret *ret,
    1155                 :            :                                       u8 identifier,
    1156                 :            :                                       struct ttls_parse_avp *parse,
    1157                 :            :                                       struct wpabuf *in_decrypted,
    1158                 :            :                                       struct wpabuf **out_data)
    1159                 :            : {
    1160                 :         82 :         struct wpabuf *resp = NULL;
    1161                 :         82 :         struct eap_peer_config *config = eap_get_config(sm);
    1162                 :            :         int res;
    1163                 :         82 :         enum phase2_types phase2_type = data->phase2_type;
    1164                 :            : 
    1165                 :            : #ifdef EAP_TNC
    1166         [ -  + ]:         82 :         if (data->tnc_started)
    1167                 :          0 :                 phase2_type = EAP_TTLS_PHASE2_EAP;
    1168                 :            : #endif /* EAP_TNC */
    1169                 :            : 
    1170   [ +  +  -  - ]:         82 :         switch (phase2_type) {
    1171                 :            :         case EAP_TTLS_PHASE2_EAP:
    1172         [ -  + ]:         26 :                 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
    1173                 :            :                     0)
    1174                 :          0 :                         return -1;
    1175                 :         26 :                 break;
    1176                 :            :         case EAP_TTLS_PHASE2_MSCHAPV2:
    1177                 :         56 :                 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
    1178                 :            : #ifdef EAP_TNC
    1179 [ +  - ][ -  + ]:         56 :                 if (res == 1 && parse->eapdata && data->phase2_success) {
                 [ #  # ]
    1180                 :            :                         /*
    1181                 :            :                          * TNC may be required as the next
    1182                 :            :                          * authentication method within the tunnel.
    1183                 :            :                          */
    1184                 :          0 :                         ret->methodState = METHOD_MAY_CONT;
    1185                 :          0 :                         data->ready_for_tnc = 1;
    1186         [ #  # ]:          0 :                         if (eap_ttls_process_tnc_start(sm, data, ret, parse,
    1187                 :            :                                                        &resp) == 0)
    1188                 :          0 :                                 break;
    1189                 :            :                 }
    1190                 :            : #endif /* EAP_TNC */
    1191                 :         56 :                 return res;
    1192                 :            :         case EAP_TTLS_PHASE2_MSCHAP:
    1193                 :            :         case EAP_TTLS_PHASE2_PAP:
    1194                 :            :         case EAP_TTLS_PHASE2_CHAP:
    1195                 :            : #ifdef EAP_TNC
    1196         [ #  # ]:          0 :                 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
    1197                 :            :                     0)
    1198                 :          0 :                         return -1;
    1199                 :          0 :                 break;
    1200                 :            : #else /* EAP_TNC */
    1201                 :            :                 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
    1202                 :            :                  * requests to the supplicant */
    1203                 :            :                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
    1204                 :            :                            "tunneled data");
    1205                 :            :                 return -1;
    1206                 :            : #endif /* EAP_TNC */
    1207                 :            :         }
    1208                 :            : 
    1209         [ +  + ]:         26 :         if (resp) {
    1210         [ -  + ]:         24 :                 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
    1211                 :            :                                               out_data) < 0)
    1212                 :          0 :                         return -1;
    1213 [ +  - ][ -  + ]:          2 :         } else if (config->pending_req_identity ||
    1214         [ #  # ]:          0 :                    config->pending_req_password ||
    1215         [ #  # ]:          0 :                    config->pending_req_otp ||
    1216                 :          0 :                    config->pending_req_new_password) {
    1217                 :          2 :                 wpabuf_free(data->pending_phase2_req);
    1218                 :          2 :                 data->pending_phase2_req = wpabuf_dup(in_decrypted);
    1219                 :            :         }
    1220                 :            : 
    1221                 :         82 :         return 0;
    1222                 :            : }
    1223                 :            : 
    1224                 :            : 
    1225                 :         84 : static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
    1226                 :            :                                               struct eap_ttls_data *data,
    1227                 :            :                                               struct eap_method_ret *ret,
    1228                 :            :                                               u8 identifier,
    1229                 :            :                                               struct wpabuf **out_data)
    1230                 :            : {
    1231                 :         84 :         int retval = 0;
    1232                 :            :         struct eap_hdr *hdr;
    1233                 :            :         struct wpabuf *resp;
    1234                 :            : 
    1235                 :         84 :         hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
    1236         [ -  + ]:         84 :         if (hdr == NULL) {
    1237                 :          0 :                 ret->methodState = METHOD_DONE;
    1238                 :          0 :                 ret->decision = DECISION_FAIL;
    1239                 :          0 :                 return -1;
    1240                 :            :         }
    1241                 :            : 
    1242                 :         84 :         resp = NULL;
    1243         [ -  + ]:         84 :         if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
    1244                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
    1245                 :            :                            "processing failed");
    1246                 :          0 :                 retval = -1;
    1247                 :            :         } else {
    1248                 :         84 :                 struct eap_peer_config *config = eap_get_config(sm);
    1249 [ +  + ][ +  + ]:         84 :                 if (resp == NULL &&
    1250         [ -  + ]:          2 :                     (config->pending_req_identity ||
    1251         [ #  # ]:          0 :                      config->pending_req_password ||
    1252         [ #  # ]:          0 :                      config->pending_req_otp ||
    1253                 :          0 :                      config->pending_req_new_password)) {
    1254                 :            :                         /*
    1255                 :            :                          * Use empty buffer to force implicit request
    1256                 :            :                          * processing when EAP request is re-processed after
    1257                 :            :                          * user input.
    1258                 :            :                          */
    1259                 :          3 :                         wpabuf_free(data->pending_phase2_req);
    1260                 :          3 :                         data->pending_phase2_req = wpabuf_alloc(0);
    1261                 :            :                 }
    1262                 :            : 
    1263                 :         84 :                 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
    1264                 :            :                                                    out_data);
    1265                 :            :         }
    1266                 :            : 
    1267                 :         84 :         os_free(hdr);
    1268                 :            : 
    1269         [ -  + ]:         84 :         if (retval < 0) {
    1270                 :          0 :                 ret->methodState = METHOD_DONE;
    1271                 :          0 :                 ret->decision = DECISION_FAIL;
    1272                 :            :         }
    1273                 :            : 
    1274                 :         84 :         return retval;
    1275                 :            : }
    1276                 :            : 
    1277                 :            : 
    1278                 :         81 : static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
    1279                 :            :                                  struct eap_method_ret *ret, u8 identifier,
    1280                 :            :                                  struct wpabuf **out_data)
    1281                 :            : {
    1282                 :         81 :         data->phase2_start = 0;
    1283                 :            : 
    1284                 :            :         /*
    1285                 :            :          * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
    1286                 :            :          * if TLS part was indeed resuming a previous session. Most
    1287                 :            :          * Authentication Servers terminate EAP-TTLS before reaching this
    1288                 :            :          * point, but some do not. Make wpa_supplicant stop phase 2 here, if
    1289                 :            :          * needed.
    1290                 :            :          */
    1291   [ +  +  -  + ]:         91 :         if (data->reauth &&
    1292                 :         10 :             tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
    1293                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
    1294                 :            :                            "skip phase 2");
    1295                 :          0 :                 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
    1296                 :            :                                                    data->ttls_version);
    1297                 :          0 :                 ret->methodState = METHOD_DONE;
    1298                 :          0 :                 ret->decision = DECISION_UNCOND_SUCC;
    1299                 :          0 :                 data->phase2_success = 1;
    1300                 :          0 :                 return 0;
    1301                 :            :         }
    1302                 :            : 
    1303                 :         81 :         return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
    1304                 :            :                                                   out_data);
    1305                 :            : }
    1306                 :            : 
    1307                 :            : 
    1308                 :        166 : static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
    1309                 :            :                             struct eap_method_ret *ret, u8 identifier,
    1310                 :            :                             const struct wpabuf *in_data,
    1311                 :            :                             struct wpabuf **out_data)
    1312                 :            : {
    1313                 :        166 :         struct wpabuf *in_decrypted = NULL;
    1314                 :        166 :         int retval = 0;
    1315                 :            :         struct ttls_parse_avp parse;
    1316                 :            : 
    1317                 :        166 :         os_memset(&parse, 0, sizeof(parse));
    1318                 :            : 
    1319         [ +  + ]:        166 :         wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
    1320                 :            :                    " Phase 2",
    1321                 :            :                    in_data ? (unsigned long) wpabuf_len(in_data) : 0);
    1322                 :            : 
    1323         [ +  + ]:        166 :         if (data->pending_phase2_req) {
    1324                 :          5 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
    1325                 :            :                            "skip decryption and use old data");
    1326                 :            :                 /* Clear TLS reassembly state. */
    1327                 :          5 :                 eap_peer_tls_reset_input(&data->ssl);
    1328                 :            : 
    1329                 :          5 :                 in_decrypted = data->pending_phase2_req;
    1330                 :          5 :                 data->pending_phase2_req = NULL;
    1331         [ +  + ]:          5 :                 if (wpabuf_len(in_decrypted) == 0) {
    1332                 :          3 :                         wpabuf_free(in_decrypted);
    1333                 :          3 :                         return eap_ttls_implicit_identity_request(
    1334                 :            :                                 sm, data, ret, identifier, out_data);
    1335                 :            :                 }
    1336                 :          2 :                 goto continue_req;
    1337                 :            :         }
    1338                 :            : 
    1339 [ +  + ][ -  + ]:        161 :         if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
                 [ +  - ]
    1340                 :         81 :             data->phase2_start) {
    1341                 :         81 :                 return eap_ttls_phase2_start(sm, data, ret, identifier,
    1342                 :            :                                              out_data);
    1343                 :            :         }
    1344                 :            : 
    1345 [ +  - ][ -  + ]:         80 :         if (in_data == NULL || wpabuf_len(in_data) == 0) {
    1346                 :            :                 /* Received TLS ACK - requesting more fragments */
    1347                 :          0 :                 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
    1348                 :            :                                             data->ttls_version,
    1349                 :            :                                             identifier, NULL, out_data);
    1350                 :            :         }
    1351                 :            : 
    1352                 :         80 :         retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
    1353         [ -  + ]:         80 :         if (retval)
    1354                 :          0 :                 goto done;
    1355                 :            : 
    1356                 :            : continue_req:
    1357                 :         82 :         data->phase2_start = 0;
    1358                 :            : 
    1359         [ -  + ]:         82 :         if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
    1360                 :          0 :                 retval = -1;
    1361                 :          0 :                 goto done;
    1362                 :            :         }
    1363                 :            : 
    1364                 :         82 :         retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
    1365                 :            :                                             &parse, in_decrypted, out_data);
    1366                 :            : 
    1367                 :            : done:
    1368                 :         82 :         wpabuf_free(in_decrypted);
    1369                 :         82 :         os_free(parse.eapdata);
    1370                 :            : 
    1371         [ -  + ]:         82 :         if (retval < 0) {
    1372                 :          0 :                 ret->methodState = METHOD_DONE;
    1373                 :          0 :                 ret->decision = DECISION_FAIL;
    1374                 :            :         }
    1375                 :            : 
    1376                 :        166 :         return retval;
    1377                 :            : }
    1378                 :            : 
    1379                 :            : 
    1380                 :        348 : static int eap_ttls_process_handshake(struct eap_sm *sm,
    1381                 :            :                                       struct eap_ttls_data *data,
    1382                 :            :                                       struct eap_method_ret *ret,
    1383                 :            :                                       u8 identifier,
    1384                 :            :                                       const u8 *in_data, size_t in_len,
    1385                 :            :                                       struct wpabuf **out_data)
    1386                 :            : {
    1387                 :            :         int res;
    1388                 :            : 
    1389                 :        348 :         res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
    1390                 :            :                                           data->ttls_version, identifier,
    1391                 :            :                                           in_data, in_len, out_data);
    1392                 :            : 
    1393         [ +  + ]:        348 :         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
    1394                 :         81 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
    1395                 :            :                            "Phase 2");
    1396         [ +  + ]:         81 :                 if (data->resuming) {
    1397                 :         10 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
    1398                 :            :                                    "skip Phase 2");
    1399                 :         10 :                         ret->decision = DECISION_COND_SUCC;
    1400                 :         10 :                         ret->methodState = METHOD_MAY_CONT;
    1401                 :            :                 }
    1402                 :         81 :                 data->phase2_start = 1;
    1403                 :         81 :                 eap_ttls_v0_derive_key(sm, data);
    1404                 :            : 
    1405 [ -  + ][ #  # ]:         81 :                 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
    1406         [ -  + ]:         81 :                         if (eap_ttls_decrypt(sm, data, ret, identifier,
    1407                 :            :                                              NULL, out_data)) {
    1408                 :          0 :                                 wpa_printf(MSG_WARNING, "EAP-TTLS: "
    1409                 :            :                                            "failed to process early "
    1410                 :            :                                            "start for Phase 2");
    1411                 :            :                         }
    1412                 :         81 :                         res = 0;
    1413                 :            :                 }
    1414                 :         81 :                 data->resuming = 0;
    1415                 :            :         }
    1416                 :            : 
    1417         [ -  + ]:        348 :         if (res == 2) {
    1418                 :            :                 struct wpabuf msg;
    1419                 :            :                 /*
    1420                 :            :                  * Application data included in the handshake message.
    1421                 :            :                  */
    1422                 :          0 :                 wpabuf_free(data->pending_phase2_req);
    1423                 :          0 :                 data->pending_phase2_req = *out_data;
    1424                 :          0 :                 *out_data = NULL;
    1425                 :          0 :                 wpabuf_set(&msg, in_data, in_len);
    1426                 :          0 :                 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
    1427                 :            :                                        out_data);
    1428                 :            :         }
    1429                 :            : 
    1430                 :        348 :         return res;
    1431                 :            : }
    1432                 :            : 
    1433                 :            : 
    1434                 :        433 : static void eap_ttls_check_auth_status(struct eap_sm *sm, 
    1435                 :            :                                        struct eap_ttls_data *data,
    1436                 :            :                                        struct eap_method_ret *ret)
    1437                 :            : {
    1438         [ +  + ]:        433 :         if (ret->methodState == METHOD_DONE) {
    1439                 :         81 :                 ret->allowNotifications = FALSE;
    1440 [ +  + ][ +  + ]:         81 :                 if (ret->decision == DECISION_UNCOND_SUCC ||
    1441                 :         20 :                     ret->decision == DECISION_COND_SUCC) {
    1442                 :         79 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
    1443                 :            :                                    "completed successfully");
    1444                 :         79 :                         data->phase2_success = 1;
    1445                 :            : #ifdef EAP_TNC
    1446 [ +  - ][ +  - ]:         79 :                         if (!data->ready_for_tnc && !data->tnc_started) {
    1447                 :            :                                 /*
    1448                 :            :                                  * TNC may be required as the next
    1449                 :            :                                  * authentication method within the tunnel.
    1450                 :            :                                  */
    1451                 :         79 :                                 ret->methodState = METHOD_MAY_CONT;
    1452                 :         79 :                                 data->ready_for_tnc = 1;
    1453                 :            :                         }
    1454                 :            : #endif /* EAP_TNC */
    1455                 :            :                 }
    1456 [ +  - ][ +  - ]:        352 :         } else if (ret->methodState == METHOD_MAY_CONT &&
    1457         [ +  + ]:        352 :                    (ret->decision == DECISION_UNCOND_SUCC ||
    1458                 :        352 :                     ret->decision == DECISION_COND_SUCC)) {
    1459                 :         59 :                         wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
    1460                 :            :                                    "completed successfully (MAY_CONT)");
    1461                 :         59 :                         data->phase2_success = 1;
    1462                 :            :         }
    1463                 :        433 : }
    1464                 :            : 
    1465                 :            : 
    1466                 :        433 : static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
    1467                 :            :                                         struct eap_method_ret *ret,
    1468                 :            :                                         const struct wpabuf *reqData)
    1469                 :            : {
    1470                 :            :         size_t left;
    1471                 :            :         int res;
    1472                 :            :         u8 flags, id;
    1473                 :            :         struct wpabuf *resp;
    1474                 :            :         const u8 *pos;
    1475                 :        433 :         struct eap_ttls_data *data = priv;
    1476                 :            : 
    1477                 :        433 :         pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
    1478                 :            :                                         reqData, &left, &flags);
    1479         [ -  + ]:        433 :         if (pos == NULL)
    1480                 :          0 :                 return NULL;
    1481                 :        433 :         id = eap_get_id(reqData);
    1482                 :            : 
    1483         [ +  + ]:        433 :         if (flags & EAP_TLS_FLAGS_START) {
    1484                 :         90 :                 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own "
    1485                 :            :                            "ver=%d)", flags & EAP_TLS_VERSION_MASK,
    1486                 :            :                            data->ttls_version);
    1487                 :            : 
    1488                 :            :                 /* RFC 5281, Ch. 9.2:
    1489                 :            :                  * "This packet MAY contain additional information in the form
    1490                 :            :                  * of AVPs, which may provide useful hints to the client"
    1491                 :            :                  * For now, ignore any potential extra data.
    1492                 :            :                  */
    1493                 :         90 :                 left = 0;
    1494                 :            :         }
    1495                 :            : 
    1496                 :        433 :         resp = NULL;
    1497 [ +  + ][ +  + ]:        433 :         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
    1498                 :         85 :             !data->resuming) {
    1499                 :            :                 struct wpabuf msg;
    1500                 :         85 :                 wpabuf_set(&msg, pos, left);
    1501                 :         85 :                 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
    1502                 :            :         } else {
    1503                 :        348 :                 res = eap_ttls_process_handshake(sm, data, ret, id,
    1504                 :            :                                                  pos, left, &resp);
    1505                 :            :         }
    1506                 :            : 
    1507                 :        433 :         eap_ttls_check_auth_status(sm, data, ret);
    1508                 :            : 
    1509                 :            :         /* FIX: what about res == -1? Could just move all error processing into
    1510                 :            :          * the other functions and get rid of this res==1 case here. */
    1511         [ +  + ]:        433 :         if (res == 1) {
    1512                 :        142 :                 wpabuf_free(resp);
    1513                 :        142 :                 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
    1514                 :            :                                               data->ttls_version);
    1515                 :            :         }
    1516                 :        433 :         return resp;
    1517                 :            : }
    1518                 :            : 
    1519                 :            : 
    1520                 :         26 : static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
    1521                 :            : {
    1522                 :         26 :         struct eap_ttls_data *data = priv;
    1523 [ +  - ][ +  - ]:         26 :         return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
    1524                 :         26 :                 data->phase2_success;
    1525                 :            : }
    1526                 :            : 
    1527                 :            : 
    1528                 :         16 : static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
    1529                 :            : {
    1530                 :         16 :         struct eap_ttls_data *data = priv;
    1531                 :         16 :         wpabuf_free(data->pending_phase2_req);
    1532                 :         16 :         data->pending_phase2_req = NULL;
    1533                 :            : #ifdef EAP_TNC
    1534                 :         16 :         data->ready_for_tnc = 0;
    1535                 :         16 :         data->tnc_started = 0;
    1536                 :            : #endif /* EAP_TNC */
    1537                 :         16 : }
    1538                 :            : 
    1539                 :            : 
    1540                 :         10 : static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
    1541                 :            : {
    1542                 :         10 :         struct eap_ttls_data *data = priv;
    1543                 :         10 :         os_free(data->key_data);
    1544                 :         10 :         data->key_data = NULL;
    1545                 :         10 :         os_free(data->session_id);
    1546                 :         10 :         data->session_id = NULL;
    1547         [ -  + ]:         10 :         if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
    1548                 :          0 :                 os_free(data);
    1549                 :          0 :                 return NULL;
    1550                 :            :         }
    1551 [ +  + ][ +  - ]:         10 :         if (data->phase2_priv && data->phase2_method &&
                 [ -  + ]
    1552                 :          3 :             data->phase2_method->init_for_reauth)
    1553                 :          0 :                 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
    1554                 :         10 :         data->phase2_start = 0;
    1555                 :         10 :         data->phase2_success = 0;
    1556                 :         10 :         data->resuming = 1;
    1557                 :         10 :         data->reauth = 1;
    1558                 :         10 :         return priv;
    1559                 :            : }
    1560                 :            : 
    1561                 :            : 
    1562                 :         49 : static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
    1563                 :            :                                size_t buflen, int verbose)
    1564                 :            : {
    1565                 :         49 :         struct eap_ttls_data *data = priv;
    1566                 :            :         int len, ret;
    1567                 :            : 
    1568                 :         49 :         len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
    1569                 :         49 :         ret = os_snprintf(buf + len, buflen - len,
    1570                 :            :                           "EAP-TTLSv%d Phase2 method=",
    1571                 :            :                           data->ttls_version);
    1572 [ +  - ][ -  + ]:         49 :         if (ret < 0 || (size_t) ret >= buflen - len)
    1573                 :          0 :                 return len;
    1574                 :         49 :         len += ret;
    1575   [ +  +  +  +  :         49 :         switch (data->phase2_type) {
                   +  - ]
    1576                 :            :         case EAP_TTLS_PHASE2_EAP:
    1577         [ +  - ]:          8 :                 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
    1578                 :          8 :                                   data->phase2_method ?
    1579                 :          8 :                                   data->phase2_method->name : "?");
    1580                 :          8 :                 break;
    1581                 :            :         case EAP_TTLS_PHASE2_MSCHAPV2:
    1582                 :         32 :                 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
    1583                 :         32 :                 break;
    1584                 :            :         case EAP_TTLS_PHASE2_MSCHAP:
    1585                 :          3 :                 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
    1586                 :          3 :                 break;
    1587                 :            :         case EAP_TTLS_PHASE2_PAP:
    1588                 :          3 :                 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
    1589                 :          3 :                 break;
    1590                 :            :         case EAP_TTLS_PHASE2_CHAP:
    1591                 :          3 :                 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
    1592                 :          3 :                 break;
    1593                 :            :         default:
    1594                 :          0 :                 ret = 0;
    1595                 :          0 :                 break;
    1596                 :            :         }
    1597 [ +  - ][ -  + ]:         49 :         if (ret < 0 || (size_t) ret >= buflen - len)
    1598                 :          0 :                 return len;
    1599                 :         49 :         len += ret;
    1600                 :            : 
    1601                 :         49 :         return len;
    1602                 :            : }
    1603                 :            : 
    1604                 :            : 
    1605                 :        433 : static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
    1606                 :            : {
    1607                 :        433 :         struct eap_ttls_data *data = priv;
    1608 [ +  + ][ +  + ]:        433 :         return data->key_data != NULL && data->phase2_success;
    1609                 :            : }
    1610                 :            : 
    1611                 :            : 
    1612                 :        142 : static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
    1613                 :            : {
    1614                 :        142 :         struct eap_ttls_data *data = priv;
    1615                 :            :         u8 *key;
    1616                 :            : 
    1617 [ +  - ][ -  + ]:        142 :         if (data->key_data == NULL || !data->phase2_success)
    1618                 :          0 :                 return NULL;
    1619                 :            : 
    1620                 :        142 :         key = os_malloc(EAP_TLS_KEY_LEN);
    1621         [ -  + ]:        142 :         if (key == NULL)
    1622                 :          0 :                 return NULL;
    1623                 :            : 
    1624                 :        142 :         *len = EAP_TLS_KEY_LEN;
    1625                 :        142 :         os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
    1626                 :            : 
    1627                 :        142 :         return key;
    1628                 :            : }
    1629                 :            : 
    1630                 :            : 
    1631                 :        142 : static u8 * eap_ttls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
    1632                 :            : {
    1633                 :        142 :         struct eap_ttls_data *data = priv;
    1634                 :            :         u8 *id;
    1635                 :            : 
    1636 [ +  - ][ -  + ]:        142 :         if (data->session_id == NULL || !data->phase2_success)
    1637                 :          0 :                 return NULL;
    1638                 :            : 
    1639                 :        142 :         id = os_malloc(data->id_len);
    1640         [ -  + ]:        142 :         if (id == NULL)
    1641                 :          0 :                 return NULL;
    1642                 :            : 
    1643                 :        142 :         *len = data->id_len;
    1644                 :        142 :         os_memcpy(id, data->session_id, data->id_len);
    1645                 :            : 
    1646                 :        142 :         return id;
    1647                 :            : }
    1648                 :            : 
    1649                 :            : 
    1650                 :          4 : int eap_peer_ttls_register(void)
    1651                 :            : {
    1652                 :            :         struct eap_method *eap;
    1653                 :            :         int ret;
    1654                 :            : 
    1655                 :          4 :         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
    1656                 :            :                                     EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
    1657         [ -  + ]:          4 :         if (eap == NULL)
    1658                 :          0 :                 return -1;
    1659                 :            : 
    1660                 :          4 :         eap->init = eap_ttls_init;
    1661                 :          4 :         eap->deinit = eap_ttls_deinit;
    1662                 :          4 :         eap->process = eap_ttls_process;
    1663                 :          4 :         eap->isKeyAvailable = eap_ttls_isKeyAvailable;
    1664                 :          4 :         eap->getKey = eap_ttls_getKey;
    1665                 :          4 :         eap->getSessionId = eap_ttls_get_session_id;
    1666                 :          4 :         eap->get_status = eap_ttls_get_status;
    1667                 :          4 :         eap->has_reauth_data = eap_ttls_has_reauth_data;
    1668                 :          4 :         eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
    1669                 :          4 :         eap->init_for_reauth = eap_ttls_init_for_reauth;
    1670                 :            : 
    1671                 :          4 :         ret = eap_peer_method_register(eap);
    1672         [ -  + ]:          4 :         if (ret)
    1673                 :          0 :                 eap_peer_method_free(eap);
    1674                 :          4 :         return ret;
    1675                 :            : }

Generated by: LCOV version 1.9