LCOV - code coverage report
Current view: top level - src/eap_peer - eap_tls_common.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1388338050 Lines: 298 438 68.0 %
Date: 2013-12-29 Functions: 25 26 96.2 %
Branches: 141 244 57.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
       3                 :            :  * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
       4                 :            :  *
       5                 :            :  * This software may be distributed under the terms of the BSD license.
       6                 :            :  * See README for more details.
       7                 :            :  */
       8                 :            : 
       9                 :            : #include "includes.h"
      10                 :            : 
      11                 :            : #include "common.h"
      12                 :            : #include "crypto/sha1.h"
      13                 :            : #include "crypto/tls.h"
      14                 :            : #include "eap_i.h"
      15                 :            : #include "eap_tls_common.h"
      16                 :            : #include "eap_config.h"
      17                 :            : 
      18                 :            : 
      19                 :        259 : static struct wpabuf * eap_tls_msg_alloc(EapType type, size_t payload_len,
      20                 :            :                                          u8 code, u8 identifier)
      21                 :            : {
      22         [ -  + ]:        259 :         if (type == EAP_UNAUTH_TLS_TYPE)
      23                 :          0 :                 return eap_msg_alloc(EAP_VENDOR_UNAUTH_TLS,
      24                 :            :                                      EAP_VENDOR_TYPE_UNAUTH_TLS, payload_len,
      25                 :            :                                      code, identifier);
      26                 :        259 :         return eap_msg_alloc(EAP_VENDOR_IETF, type, payload_len, code,
      27                 :            :                              identifier);
      28                 :            : }
      29                 :            : 
      30                 :            : 
      31                 :        140 : static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
      32                 :            :                               const u8 **data, size_t *data_len)
      33                 :            : {
      34                 :            :         const struct wpa_config_blob *blob;
      35                 :            : 
      36 [ +  + ][ +  - ]:        140 :         if (*name == NULL || os_strncmp(*name, "blob://", 7) != 0)
      37                 :        140 :                 return 0;
      38                 :            : 
      39                 :          0 :         blob = eap_get_config_blob(sm, *name + 7);
      40         [ #  # ]:          0 :         if (blob == NULL) {
      41                 :          0 :                 wpa_printf(MSG_ERROR, "%s: Named configuration blob '%s' not "
      42                 :          0 :                            "found", __func__, *name + 7);
      43                 :          0 :                 return -1;
      44                 :            :         }
      45                 :            : 
      46                 :          0 :         *name = NULL;
      47                 :          0 :         *data = blob->data;
      48                 :          0 :         *data_len = blob->len;
      49                 :            : 
      50                 :        140 :         return 0;
      51                 :            : }
      52                 :            : 
      53                 :            : 
      54                 :         35 : static void eap_tls_params_flags(struct tls_connection_params *params,
      55                 :            :                                  const char *txt)
      56                 :            : {
      57         [ +  + ]:         35 :         if (txt == NULL)
      58                 :         35 :                 return;
      59         [ -  + ]:          5 :         if (os_strstr(txt, "tls_allow_md5=1"))
      60                 :          0 :                 params->flags |= TLS_CONN_ALLOW_SIGN_RSA_MD5;
      61         [ -  + ]:          5 :         if (os_strstr(txt, "tls_disable_time_checks=1"))
      62                 :          0 :                 params->flags |= TLS_CONN_DISABLE_TIME_CHECKS;
      63         [ -  + ]:          5 :         if (os_strstr(txt, "tls_disable_session_ticket=1"))
      64                 :          0 :                 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
      65         [ -  + ]:          5 :         if (os_strstr(txt, "tls_disable_session_ticket=0"))
      66                 :          0 :                 params->flags &= ~TLS_CONN_DISABLE_SESSION_TICKET;
      67                 :            : }
      68                 :            : 
      69                 :            : 
      70                 :         35 : static void eap_tls_params_from_conf1(struct tls_connection_params *params,
      71                 :            :                                       struct eap_peer_config *config)
      72                 :            : {
      73                 :         35 :         params->ca_cert = (char *) config->ca_cert;
      74                 :         35 :         params->ca_path = (char *) config->ca_path;
      75                 :         35 :         params->client_cert = (char *) config->client_cert;
      76                 :         35 :         params->private_key = (char *) config->private_key;
      77                 :         35 :         params->private_key_passwd = (char *) config->private_key_passwd;
      78                 :         35 :         params->dh_file = (char *) config->dh_file;
      79                 :         35 :         params->subject_match = (char *) config->subject_match;
      80                 :         35 :         params->altsubject_match = (char *) config->altsubject_match;
      81                 :         35 :         params->suffix_match = config->domain_suffix_match;
      82                 :         35 :         params->engine = config->engine;
      83                 :         35 :         params->engine_id = config->engine_id;
      84                 :         35 :         params->pin = config->pin;
      85                 :         35 :         params->key_id = config->key_id;
      86                 :         35 :         params->cert_id = config->cert_id;
      87                 :         35 :         params->ca_cert_id = config->ca_cert_id;
      88                 :         35 :         eap_tls_params_flags(params, config->phase1);
      89                 :         35 : }
      90                 :            : 
      91                 :            : 
      92                 :          0 : static void eap_tls_params_from_conf2(struct tls_connection_params *params,
      93                 :            :                                       struct eap_peer_config *config)
      94                 :            : {
      95                 :          0 :         params->ca_cert = (char *) config->ca_cert2;
      96                 :          0 :         params->ca_path = (char *) config->ca_path2;
      97                 :          0 :         params->client_cert = (char *) config->client_cert2;
      98                 :          0 :         params->private_key = (char *) config->private_key2;
      99                 :          0 :         params->private_key_passwd = (char *) config->private_key2_passwd;
     100                 :          0 :         params->dh_file = (char *) config->dh_file2;
     101                 :          0 :         params->subject_match = (char *) config->subject_match2;
     102                 :          0 :         params->altsubject_match = (char *) config->altsubject_match2;
     103                 :          0 :         params->suffix_match = config->domain_suffix_match2;
     104                 :          0 :         params->engine = config->engine2;
     105                 :          0 :         params->engine_id = config->engine2_id;
     106                 :          0 :         params->pin = config->pin2;
     107                 :          0 :         params->key_id = config->key2_id;
     108                 :          0 :         params->cert_id = config->cert2_id;
     109                 :          0 :         params->ca_cert_id = config->ca_cert2_id;
     110                 :          0 :         eap_tls_params_flags(params, config->phase2);
     111                 :          0 : }
     112                 :            : 
     113                 :            : 
     114                 :         35 : static int eap_tls_params_from_conf(struct eap_sm *sm,
     115                 :            :                                     struct eap_ssl_data *data,
     116                 :            :                                     struct tls_connection_params *params,
     117                 :            :                                     struct eap_peer_config *config, int phase2)
     118                 :            : {
     119                 :         35 :         os_memset(params, 0, sizeof(*params));
     120 [ +  - ][ +  + ]:         35 :         if (sm->workaround && data->eap_type != EAP_TYPE_FAST) {
     121                 :            :                 /*
     122                 :            :                  * Some deployed authentication servers seem to be unable to
     123                 :            :                  * handle the TLS Session Ticket extension (they are supposed
     124                 :            :                  * to ignore unrecognized TLS extensions, but end up rejecting
     125                 :            :                  * the ClientHello instead). As a workaround, disable use of
     126                 :            :                  * TLS Sesson Ticket extension for EAP-TLS, EAP-PEAP, and
     127                 :            :                  * EAP-TTLS (EAP-FAST uses session ticket, so any server that
     128                 :            :                  * supports EAP-FAST does not need this workaround).
     129                 :            :                  */
     130                 :         33 :                 params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
     131                 :            :         }
     132         [ -  + ]:         35 :         if (phase2) {
     133                 :          0 :                 wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
     134                 :          0 :                 eap_tls_params_from_conf2(params, config);
     135                 :            :         } else {
     136                 :         35 :                 wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
     137                 :         35 :                 eap_tls_params_from_conf1(params, config);
     138                 :            :         }
     139                 :            : 
     140                 :            :         /*
     141                 :            :          * Use blob data, if available. Otherwise, leave reference to external
     142                 :            :          * file as-is.
     143                 :            :          */
     144         [ +  - ]:         35 :         if (eap_tls_check_blob(sm, &params->ca_cert, &params->ca_cert_blob,
     145         [ +  - ]:         35 :                                &params->ca_cert_blob_len) ||
     146                 :         35 :             eap_tls_check_blob(sm, &params->client_cert,
     147                 :            :                                &params->client_cert_blob,
     148         [ +  - ]:         35 :                                &params->client_cert_blob_len) ||
     149                 :         35 :             eap_tls_check_blob(sm, &params->private_key,
     150                 :            :                                &params->private_key_blob,
     151         [ -  + ]:         35 :                                &params->private_key_blob_len) ||
     152                 :         35 :             eap_tls_check_blob(sm, &params->dh_file, &params->dh_blob,
     153                 :            :                                &params->dh_blob_len)) {
     154                 :          0 :                 wpa_printf(MSG_INFO, "SSL: Failed to get configuration blobs");
     155                 :          0 :                 return -1;
     156                 :            :         }
     157                 :            : 
     158                 :         35 :         return 0;
     159                 :            : }
     160                 :            : 
     161                 :            : 
     162                 :         35 : static int eap_tls_init_connection(struct eap_sm *sm,
     163                 :            :                                    struct eap_ssl_data *data,
     164                 :            :                                    struct eap_peer_config *config,
     165                 :            :                                    struct tls_connection_params *params)
     166                 :            : {
     167                 :            :         int res;
     168                 :            : 
     169         [ -  + ]:         35 :         if (config->ocsp)
     170                 :          0 :                 params->flags |= TLS_CONN_REQUEST_OCSP;
     171         [ -  + ]:         35 :         if (config->ocsp == 2)
     172                 :          0 :                 params->flags |= TLS_CONN_REQUIRE_OCSP;
     173                 :         35 :         data->conn = tls_connection_init(data->ssl_ctx);
     174         [ -  + ]:         35 :         if (data->conn == NULL) {
     175                 :          0 :                 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
     176                 :            :                            "connection");
     177                 :          0 :                 return -1;
     178                 :            :         }
     179                 :            : 
     180                 :         35 :         res = tls_connection_set_params(data->ssl_ctx, data->conn, params);
     181         [ -  + ]:         35 :         if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
     182                 :            :                 /*
     183                 :            :                  * At this point with the pkcs11 engine the PIN might be wrong.
     184                 :            :                  * We reset the PIN in the configuration to be sure to not use
     185                 :            :                  * it again and the calling function must request a new one.
     186                 :            :                  */
     187                 :          0 :                 os_free(config->pin);
     188                 :          0 :                 config->pin = NULL;
     189         [ -  + ]:         35 :         } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
     190                 :          0 :                 wpa_printf(MSG_INFO, "TLS: Failed to load private key");
     191                 :            :                 /*
     192                 :            :                  * We do not know exactly but maybe the PIN was wrong,
     193                 :            :                  * so ask for a new one.
     194                 :            :                  */
     195                 :          0 :                 os_free(config->pin);
     196                 :          0 :                 config->pin = NULL;
     197                 :          0 :                 eap_sm_request_pin(sm);
     198                 :          0 :                 sm->ignore = TRUE;
     199                 :          0 :                 tls_connection_deinit(data->ssl_ctx, data->conn);
     200                 :          0 :                 data->conn = NULL;
     201                 :          0 :                 return -1;
     202         [ -  + ]:         35 :         } else if (res) {
     203                 :          0 :                 wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
     204                 :            :                            "parameters");
     205                 :          0 :                 tls_connection_deinit(data->ssl_ctx, data->conn);
     206                 :          0 :                 data->conn = NULL;
     207                 :          0 :                 return -1;
     208                 :            :         }
     209                 :            : 
     210                 :         35 :         return 0;
     211                 :            : }
     212                 :            : 
     213                 :            : 
     214                 :            : /**
     215                 :            :  * eap_peer_tls_ssl_init - Initialize shared TLS functionality
     216                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     217                 :            :  * @data: Data for TLS processing
     218                 :            :  * @config: Pointer to the network configuration
     219                 :            :  * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
     220                 :            :  * Returns: 0 on success, -1 on failure
     221                 :            :  *
     222                 :            :  * This function is used to initialize shared TLS functionality for EAP-TLS,
     223                 :            :  * EAP-PEAP, EAP-TTLS, and EAP-FAST.
     224                 :            :  */
     225                 :         35 : int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
     226                 :            :                           struct eap_peer_config *config, u8 eap_type)
     227                 :            : {
     228                 :            :         struct tls_connection_params params;
     229                 :            : 
     230         [ -  + ]:         35 :         if (config == NULL)
     231                 :          0 :                 return -1;
     232                 :            : 
     233                 :         35 :         data->eap = sm;
     234                 :         35 :         data->eap_type = eap_type;
     235                 :         35 :         data->phase2 = sm->init_phase2;
     236 [ -  + ][ #  # ]:         35 :         data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
     237                 :            :                 sm->ssl_ctx;
     238         [ -  + ]:         35 :         if (eap_tls_params_from_conf(sm, data, &params, config, data->phase2) <
     239                 :            :             0)
     240                 :          0 :                 return -1;
     241                 :            : 
     242         [ -  + ]:         35 :         if (eap_tls_init_connection(sm, data, config, &params) < 0)
     243                 :          0 :                 return -1;
     244                 :            : 
     245                 :         35 :         data->tls_out_limit = config->fragment_size;
     246         [ -  + ]:         35 :         if (data->phase2) {
     247                 :            :                 /* Limit the fragment size in the inner TLS authentication
     248                 :            :                  * since the outer authentication with EAP-PEAP does not yet
     249                 :            :                  * support fragmentation */
     250         [ #  # ]:          0 :                 if (data->tls_out_limit > 100)
     251                 :          0 :                         data->tls_out_limit -= 100;
     252                 :            :         }
     253                 :            : 
     254 [ +  + ][ -  + ]:         35 :         if (config->phase1 &&
     255                 :          5 :             os_strstr(config->phase1, "include_tls_length=1")) {
     256                 :          0 :                 wpa_printf(MSG_DEBUG, "TLS: Include TLS Message Length in "
     257                 :            :                            "unfragmented packets");
     258                 :          0 :                 data->include_tls_length = 1;
     259                 :            :         }
     260                 :            : 
     261                 :         35 :         return 0;
     262                 :            : }
     263                 :            : 
     264                 :            : 
     265                 :            : /**
     266                 :            :  * eap_peer_tls_ssl_deinit - Deinitialize shared TLS functionality
     267                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     268                 :            :  * @data: Data for TLS processing
     269                 :            :  *
     270                 :            :  * This function deinitializes shared TLS functionality that was initialized
     271                 :            :  * with eap_peer_tls_ssl_init().
     272                 :            :  */
     273                 :         35 : void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
     274                 :            : {
     275                 :         35 :         tls_connection_deinit(data->ssl_ctx, data->conn);
     276                 :         35 :         eap_peer_tls_reset_input(data);
     277                 :         35 :         eap_peer_tls_reset_output(data);
     278                 :         35 : }
     279                 :            : 
     280                 :            : 
     281                 :            : /**
     282                 :            :  * eap_peer_tls_derive_key - Derive a key based on TLS session data
     283                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     284                 :            :  * @data: Data for TLS processing
     285                 :            :  * @label: Label string for deriving the keys, e.g., "client EAP encryption"
     286                 :            :  * @len: Length of the key material to generate (usually 64 for MSK)
     287                 :            :  * Returns: Pointer to allocated key on success or %NULL on failure
     288                 :            :  *
     289                 :            :  * This function uses TLS-PRF to generate pseudo-random data based on the TLS
     290                 :            :  * session data (client/server random and master key). Each key type may use a
     291                 :            :  * different label to bind the key usage into the generated material.
     292                 :            :  *
     293                 :            :  * The caller is responsible for freeing the returned buffer.
     294                 :            :  */
     295                 :         64 : u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
     296                 :            :                              const char *label, size_t len)
     297                 :            : {
     298                 :            : #ifndef CONFIG_FIPS
     299                 :            :         struct tls_keys keys;
     300                 :            : #endif /* CONFIG_FIPS */
     301                 :         64 :         u8 *rnd = NULL, *out;
     302                 :            : 
     303                 :         64 :         out = os_malloc(len);
     304         [ -  + ]:         64 :         if (out == NULL)
     305                 :          0 :                 return NULL;
     306                 :            : 
     307                 :            :         /* First, try to use TLS library function for PRF, if available. */
     308         [ +  - ]:         64 :         if (tls_connection_prf(data->ssl_ctx, data->conn, label, 0, out, len)
     309                 :            :             == 0)
     310                 :         64 :                 return out;
     311                 :            : 
     312                 :            : #ifndef CONFIG_FIPS
     313                 :            :         /*
     314                 :            :          * TLS library did not support key generation, so get the needed TLS
     315                 :            :          * session parameters and use an internal implementation of TLS PRF to
     316                 :            :          * derive the key.
     317                 :            :          */
     318         [ #  # ]:          0 :         if (tls_connection_get_keys(data->ssl_ctx, data->conn, &keys))
     319                 :          0 :                 goto fail;
     320                 :            : 
     321 [ #  # ][ #  # ]:          0 :         if (keys.client_random == NULL || keys.server_random == NULL ||
                 [ #  # ]
     322                 :          0 :             keys.master_key == NULL)
     323                 :            :                 goto fail;
     324                 :            : 
     325                 :          0 :         rnd = os_malloc(keys.client_random_len + keys.server_random_len);
     326         [ #  # ]:          0 :         if (rnd == NULL)
     327                 :          0 :                 goto fail;
     328                 :          0 :         os_memcpy(rnd, keys.client_random, keys.client_random_len);
     329                 :          0 :         os_memcpy(rnd + keys.client_random_len, keys.server_random,
     330                 :            :                   keys.server_random_len);
     331                 :            : 
     332         [ #  # ]:          0 :         if (tls_prf_sha1_md5(keys.master_key, keys.master_key_len,
     333                 :          0 :                              label, rnd, keys.client_random_len +
     334                 :          0 :                              keys.server_random_len, out, len))
     335                 :          0 :                 goto fail;
     336                 :            : 
     337                 :          0 :         os_free(rnd);
     338                 :          0 :         return out;
     339                 :            : 
     340                 :            : fail:
     341                 :            : #endif /* CONFIG_FIPS */
     342                 :          0 :         os_free(out);
     343                 :          0 :         os_free(rnd);
     344                 :         64 :         return NULL;
     345                 :            : }
     346                 :            : 
     347                 :            : 
     348                 :            : /**
     349                 :            :  * eap_peer_tls_derive_session_id - Derive a Session-Id based on TLS data
     350                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     351                 :            :  * @data: Data for TLS processing
     352                 :            :  * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
     353                 :            :  * @len: Pointer to length of the session ID generated
     354                 :            :  * Returns: Pointer to allocated Session-Id on success or %NULL on failure
     355                 :            :  *
     356                 :            :  * This function derive the Session-Id based on the TLS session data
     357                 :            :  * (client/server random and method type).
     358                 :            :  *
     359                 :            :  * The caller is responsible for freeing the returned buffer.
     360                 :            :  */
     361                 :         45 : u8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
     362                 :            :                                     struct eap_ssl_data *data, u8 eap_type,
     363                 :            :                                     size_t *len)
     364                 :            : {
     365                 :            :         struct tls_keys keys;
     366                 :            :         u8 *out;
     367                 :            : 
     368                 :            :         /*
     369                 :            :          * TLS library did not support session ID generation,
     370                 :            :          * so get the needed TLS session parameters
     371                 :            :          */
     372         [ -  + ]:         45 :         if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
     373                 :          0 :                 return NULL;
     374                 :            : 
     375 [ +  - ][ +  - ]:         45 :         if (keys.client_random == NULL || keys.server_random == NULL ||
                 [ -  + ]
     376                 :         45 :             keys.master_key == NULL)
     377                 :          0 :                 return NULL;
     378                 :            : 
     379                 :         45 :         *len = 1 + keys.client_random_len + keys.server_random_len;
     380                 :         45 :         out = os_malloc(*len);
     381         [ -  + ]:         45 :         if (out == NULL)
     382                 :          0 :                 return NULL;
     383                 :            : 
     384                 :            :         /* Session-Id = EAP type || client.random || server.random */
     385                 :         45 :         out[0] = eap_type;
     386                 :         45 :         os_memcpy(out + 1, keys.client_random, keys.client_random_len);
     387                 :         45 :         os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
     388                 :            :                   keys.server_random_len);
     389                 :            : 
     390                 :         45 :         return out;
     391                 :            : }
     392                 :            : 
     393                 :            : 
     394                 :            : /**
     395                 :            :  * eap_peer_tls_reassemble_fragment - Reassemble a received fragment
     396                 :            :  * @data: Data for TLS processing
     397                 :            :  * @in_data: Next incoming TLS segment
     398                 :            :  * Returns: 0 on success, 1 if more data is needed for the full message, or
     399                 :            :  * -1 on error
     400                 :            :  */
     401                 :         92 : static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
     402                 :            :                                             const struct wpabuf *in_data)
     403                 :            : {
     404                 :            :         size_t tls_in_len, in_len;
     405                 :            : 
     406         [ +  + ]:         92 :         tls_in_len = data->tls_in ? wpabuf_len(data->tls_in) : 0;
     407         [ +  - ]:         92 :         in_len = in_data ? wpabuf_len(in_data) : 0;
     408                 :            : 
     409         [ -  + ]:         92 :         if (tls_in_len + in_len == 0) {
     410                 :            :                 /* No message data received?! */
     411                 :          0 :                 wpa_printf(MSG_WARNING, "SSL: Invalid reassembly state: "
     412                 :            :                            "tls_in_left=%lu tls_in_len=%lu in_len=%lu",
     413                 :            :                            (unsigned long) data->tls_in_left,
     414                 :            :                            (unsigned long) tls_in_len,
     415                 :            :                            (unsigned long) in_len);
     416                 :          0 :                 eap_peer_tls_reset_input(data);
     417                 :          0 :                 return -1;
     418                 :            :         }
     419                 :            : 
     420         [ -  + ]:         92 :         if (tls_in_len + in_len > 65536) {
     421                 :            :                 /*
     422                 :            :                  * Limit length to avoid rogue servers from causing large
     423                 :            :                  * memory allocations.
     424                 :            :                  */
     425                 :          0 :                 wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size over "
     426                 :            :                            "64 kB)");
     427                 :          0 :                 eap_peer_tls_reset_input(data);
     428                 :          0 :                 return -1;
     429                 :            :         }
     430                 :            : 
     431         [ -  + ]:         92 :         if (in_len > data->tls_in_left) {
     432                 :            :                 /* Sender is doing something odd - reject message */
     433                 :          0 :                 wpa_printf(MSG_INFO, "SSL: more data than TLS message length "
     434                 :            :                            "indicated");
     435                 :          0 :                 eap_peer_tls_reset_input(data);
     436                 :          0 :                 return -1;
     437                 :            :         }
     438                 :            : 
     439         [ -  + ]:         92 :         if (wpabuf_resize(&data->tls_in, in_len) < 0) {
     440                 :          0 :                 wpa_printf(MSG_INFO, "SSL: Could not allocate memory for TLS "
     441                 :            :                            "data");
     442                 :          0 :                 eap_peer_tls_reset_input(data);
     443                 :          0 :                 return -1;
     444                 :            :         }
     445         [ +  - ]:         92 :         if (in_data)
     446                 :         92 :                 wpabuf_put_buf(data->tls_in, in_data);
     447                 :         92 :         data->tls_in_left -= in_len;
     448                 :            : 
     449         [ +  + ]:         92 :         if (data->tls_in_left > 0) {
     450                 :         46 :                 wpa_printf(MSG_DEBUG, "SSL: Need %lu bytes more input "
     451                 :            :                            "data", (unsigned long) data->tls_in_left);
     452                 :         46 :                 return 1;
     453                 :            :         }
     454                 :            : 
     455                 :         92 :         return 0;
     456                 :            : }
     457                 :            : 
     458                 :            : 
     459                 :            : /**
     460                 :            :  * eap_peer_tls_data_reassemble - Reassemble TLS data
     461                 :            :  * @data: Data for TLS processing
     462                 :            :  * @in_data: Next incoming TLS segment
     463                 :            :  * @need_more_input: Variable for returning whether more input data is needed
     464                 :            :  * to reassemble this TLS packet
     465                 :            :  * Returns: Pointer to output data, %NULL on error or when more data is needed
     466                 :            :  * for the full message (in which case, *need_more_input is also set to 1).
     467                 :            :  *
     468                 :            :  * This function reassembles TLS fragments. Caller must not free the returned
     469                 :            :  * data buffer since an internal pointer to it is maintained.
     470                 :            :  */
     471                 :        256 : static const struct wpabuf * eap_peer_tls_data_reassemble(
     472                 :            :         struct eap_ssl_data *data, const struct wpabuf *in_data,
     473                 :            :         int *need_more_input)
     474                 :            : {
     475                 :        256 :         *need_more_input = 0;
     476                 :            : 
     477 [ +  + ][ +  + ]:        302 :         if (data->tls_in_left > wpabuf_len(in_data) || data->tls_in) {
     478                 :            :                 /* Message has fragments */
     479                 :         92 :                 int res = eap_peer_tls_reassemble_fragment(data, in_data);
     480         [ +  + ]:         92 :                 if (res) {
     481         [ +  - ]:         46 :                         if (res == 1)
     482                 :         46 :                                 *need_more_input = 1;
     483                 :         46 :                         return NULL;
     484                 :            :                 }
     485                 :            : 
     486                 :            :                 /* Message is now fully reassembled. */
     487                 :            :         } else {
     488                 :            :                 /* No fragments in this message, so just make a copy of it. */
     489                 :        164 :                 data->tls_in_left = 0;
     490                 :        164 :                 data->tls_in = wpabuf_dup(in_data);
     491         [ -  + ]:        164 :                 if (data->tls_in == NULL)
     492                 :          0 :                         return NULL;
     493                 :            :         }
     494                 :            : 
     495                 :        256 :         return data->tls_in;
     496                 :            : }
     497                 :            : 
     498                 :            : 
     499                 :            : /**
     500                 :            :  * eap_tls_process_input - Process incoming TLS message
     501                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     502                 :            :  * @data: Data for TLS processing
     503                 :            :  * @in_data: Message received from the server
     504                 :            :  * @in_len: Length of in_data
     505                 :            :  * @out_data: Buffer for returning a pointer to application data (if available)
     506                 :            :  * Returns: 0 on success, 1 if more input data is needed, 2 if application data
     507                 :            :  * is available, -1 on failure
     508                 :            :  */
     509                 :        184 : static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
     510                 :            :                                  const u8 *in_data, size_t in_len,
     511                 :            :                                  struct wpabuf **out_data)
     512                 :            : {
     513                 :            :         const struct wpabuf *msg;
     514                 :            :         int need_more_input;
     515                 :            :         struct wpabuf *appl_data;
     516                 :            :         struct wpabuf buf;
     517                 :            : 
     518                 :        184 :         wpabuf_set(&buf, in_data, in_len);
     519                 :        184 :         msg = eap_peer_tls_data_reassemble(data, &buf, &need_more_input);
     520         [ +  + ]:        184 :         if (msg == NULL)
     521         [ +  - ]:         46 :                 return need_more_input ? 1 : -1;
     522                 :            : 
     523                 :            :         /* Full TLS message reassembled - continue handshake processing */
     524         [ -  + ]:        138 :         if (data->tls_out) {
     525                 :            :                 /* This should not happen.. */
     526                 :          0 :                 wpa_printf(MSG_INFO, "SSL: eap_tls_process_input - pending "
     527                 :            :                            "tls_out data even though tls_out_len = 0");
     528                 :          0 :                 wpabuf_free(data->tls_out);
     529                 :            :                 WPA_ASSERT(data->tls_out == NULL);
     530                 :            :         }
     531                 :        138 :         appl_data = NULL;
     532                 :        138 :         data->tls_out = tls_connection_handshake(data->ssl_ctx, data->conn,
     533                 :            :                                                  msg, &appl_data);
     534                 :            : 
     535                 :        138 :         eap_peer_tls_reset_input(data);
     536                 :            : 
     537   [ +  +  +  - ]:        139 :         if (appl_data &&
     538         [ +  - ]:          2 :             tls_connection_established(data->ssl_ctx, data->conn) &&
     539                 :          1 :             !tls_connection_get_failed(data->ssl_ctx, data->conn)) {
     540                 :          1 :                 wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
     541                 :            :                                     appl_data);
     542                 :          1 :                 *out_data = appl_data;
     543                 :          1 :                 return 2;
     544                 :            :         }
     545                 :            : 
     546                 :        137 :         wpabuf_free(appl_data);
     547                 :            : 
     548                 :        184 :         return 0;
     549                 :            : }
     550                 :            : 
     551                 :            : 
     552                 :            : /**
     553                 :            :  * eap_tls_process_output - Process outgoing TLS message
     554                 :            :  * @data: Data for TLS processing
     555                 :            :  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
     556                 :            :  * @peap_version: Version number for EAP-PEAP/TTLS
     557                 :            :  * @id: EAP identifier for the response
     558                 :            :  * @ret: Return value to use on success
     559                 :            :  * @out_data: Buffer for returning the allocated output buffer
     560                 :            :  * Returns: ret (0 or 1) on success, -1 on failure
     561                 :            :  */
     562                 :        179 : static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
     563                 :            :                                   int peap_version, u8 id, int ret,
     564                 :            :                                   struct wpabuf **out_data)
     565                 :            : {
     566                 :            :         size_t len;
     567                 :            :         u8 *flags;
     568                 :            :         int more_fragments, length_included;
     569                 :            : 
     570         [ -  + ]:        179 :         if (data->tls_out == NULL)
     571                 :          0 :                 return -1;
     572                 :        179 :         len = wpabuf_len(data->tls_out) - data->tls_out_pos;
     573                 :        179 :         wpa_printf(MSG_DEBUG, "SSL: %lu bytes left to be sent out (of total "
     574                 :            :                    "%lu bytes)",
     575                 :            :                    (unsigned long) len,
     576                 :        179 :                    (unsigned long) wpabuf_len(data->tls_out));
     577                 :            : 
     578                 :            :         /*
     579                 :            :          * Limit outgoing message to the configured maximum size. Fragment
     580                 :            :          * message if needed.
     581                 :            :          */
     582         [ +  + ]:        179 :         if (len > data->tls_out_limit) {
     583                 :          3 :                 more_fragments = 1;
     584                 :          3 :                 len = data->tls_out_limit;
     585                 :          3 :                 wpa_printf(MSG_DEBUG, "SSL: sending %lu bytes, more fragments "
     586                 :            :                            "will follow", (unsigned long) len);
     587                 :            :         } else
     588                 :        176 :                 more_fragments = 0;
     589                 :            : 
     590   [ +  +  +  + ]:        528 :         length_included = data->tls_out_pos == 0 &&
     591         [ -  + ]:        349 :                 (wpabuf_len(data->tls_out) > data->tls_out_limit ||
     592                 :        173 :                  data->include_tls_length);
     593 [ +  + ][ +  + ]:        179 :         if (!length_included &&
     594   [ +  +  +  + ]:         59 :             eap_type == EAP_TYPE_PEAP && peap_version == 0 &&
     595                 :         12 :             !tls_connection_established(data->eap->ssl_ctx, data->conn)) {
     596                 :            :                 /*
     597                 :            :                  * Windows Server 2008 NPS really wants to have the TLS Message
     598                 :            :                  * length included in phase 0 even for unfragmented frames or
     599                 :            :                  * it will get very confused with Compound MAC calculation and
     600                 :            :                  * Outer TLVs.
     601                 :            :                  */
     602                 :          4 :                 length_included = 1;
     603                 :            :         }
     604                 :            : 
     605                 :        179 :         *out_data = eap_tls_msg_alloc(eap_type, 1 + length_included * 4 + len,
     606                 :            :                                       EAP_CODE_RESPONSE, id);
     607         [ -  + ]:        179 :         if (*out_data == NULL)
     608                 :          0 :                 return -1;
     609                 :            : 
     610                 :        179 :         flags = wpabuf_put(*out_data, 1);
     611                 :        179 :         *flags = peap_version;
     612         [ +  + ]:        179 :         if (more_fragments)
     613                 :          3 :                 *flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
     614         [ +  + ]:        179 :         if (length_included) {
     615                 :          7 :                 *flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
     616                 :          7 :                 wpabuf_put_be32(*out_data, wpabuf_len(data->tls_out));
     617                 :            :         }
     618                 :            : 
     619                 :        179 :         wpabuf_put_data(*out_data,
     620                 :        179 :                         wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
     621                 :            :                         len);
     622                 :        179 :         data->tls_out_pos += len;
     623                 :            : 
     624         [ +  + ]:        179 :         if (!more_fragments)
     625                 :        176 :                 eap_peer_tls_reset_output(data);
     626                 :            : 
     627                 :        179 :         return ret;
     628                 :            : }
     629                 :            : 
     630                 :            : 
     631                 :            : /**
     632                 :            :  * eap_peer_tls_process_helper - Process TLS handshake message
     633                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     634                 :            :  * @data: Data for TLS processing
     635                 :            :  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
     636                 :            :  * @peap_version: Version number for EAP-PEAP/TTLS
     637                 :            :  * @id: EAP identifier for the response
     638                 :            :  * @in_data: Message received from the server
     639                 :            :  * @in_len: Length of in_data
     640                 :            :  * @out_data: Buffer for returning a pointer to the response message
     641                 :            :  * Returns: 0 on success, 1 if more input data is needed, 2 if application data
     642                 :            :  * is available, or -1 on failure
     643                 :            :  *
     644                 :            :  * This function can be used to process TLS handshake messages. It reassembles
     645                 :            :  * the received fragments and uses a TLS library to process the messages. The
     646                 :            :  * response data from the TLS library is fragmented to suitable output messages
     647                 :            :  * that the caller can send out.
     648                 :            :  *
     649                 :            :  * out_data is used to return the response message if the return value of this
     650                 :            :  * function is 0, 2, or -1. In case of failure, the message is likely a TLS
     651                 :            :  * alarm message. The caller is responsible for freeing the allocated buffer if
     652                 :            :  * *out_data is not %NULL.
     653                 :            :  *
     654                 :            :  * This function is called for each received TLS message during the TLS
     655                 :            :  * handshake after eap_peer_tls_process_init() call and possible processing of
     656                 :            :  * TLS Flags field. Once the handshake has been completed, i.e., when
     657                 :            :  * tls_connection_established() returns 1, EAP method specific decrypting of
     658                 :            :  * the tunneled data is used.
     659                 :            :  */
     660                 :        187 : int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
     661                 :            :                                 EapType eap_type, int peap_version,
     662                 :            :                                 u8 id, const u8 *in_data, size_t in_len,
     663                 :            :                                 struct wpabuf **out_data)
     664                 :            : {
     665                 :        187 :         int ret = 0;
     666                 :            : 
     667                 :        187 :         *out_data = NULL;
     668                 :            : 
     669 [ +  + ][ +  - ]:        187 :         if (data->tls_out && wpabuf_len(data->tls_out) > 0 && in_len > 0) {
                 [ -  + ]
     670                 :          0 :                 wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
     671                 :            :                            "fragments are waiting to be sent out");
     672                 :          0 :                 return -1;
     673                 :            :         }
     674                 :            : 
     675 [ +  + ][ -  + ]:        187 :         if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
     676                 :            :                 /*
     677                 :            :                  * No more data to send out - expect to receive more data from
     678                 :            :                  * the AS.
     679                 :            :                  */
     680                 :        184 :                 int res = eap_tls_process_input(sm, data, in_data, in_len,
     681                 :            :                                                 out_data);
     682         [ +  + ]:        184 :                 if (res) {
     683                 :            :                         /*
     684                 :            :                          * Input processing failed (res = -1) or more data is
     685                 :            :                          * needed (res = 1).
     686                 :            :                          */
     687                 :         47 :                         return res;
     688                 :            :                 }
     689                 :            : 
     690                 :            :                 /*
     691                 :            :                  * The incoming message has been reassembled and processed. The
     692                 :            :                  * response was allocated into data->tls_out buffer.
     693                 :            :                  */
     694                 :            :         }
     695                 :            : 
     696         [ -  + ]:        140 :         if (data->tls_out == NULL) {
     697                 :            :                 /*
     698                 :            :                  * No outgoing fragments remaining from the previous message
     699                 :            :                  * and no new message generated. This indicates an error in TLS
     700                 :            :                  * processing.
     701                 :            :                  */
     702                 :          0 :                 eap_peer_tls_reset_output(data);
     703                 :          0 :                 return -1;
     704                 :            :         }
     705                 :            : 
     706         [ +  + ]:        140 :         if (tls_connection_get_failed(data->ssl_ctx, data->conn)) {
     707                 :            :                 /* TLS processing has failed - return error */
     708                 :          2 :                 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
     709                 :            :                            "report error");
     710                 :          2 :                 ret = -1;
     711                 :            :                 /* TODO: clean pin if engine used? */
     712                 :            :         }
     713                 :            : 
     714 [ +  - ][ +  + ]:        140 :         if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
     715                 :            :                 /*
     716                 :            :                  * TLS negotiation should now be complete since all other cases
     717                 :            :                  * needing more data should have been caught above based on
     718                 :            :                  * the TLS Message Length field.
     719                 :            :                  */
     720                 :         43 :                 wpa_printf(MSG_DEBUG, "SSL: No data to be sent out");
     721                 :         43 :                 wpabuf_free(data->tls_out);
     722                 :         43 :                 data->tls_out = NULL;
     723                 :         43 :                 return 1;
     724                 :            :         }
     725                 :            : 
     726                 :            :         /* Send the pending message (in fragments, if needed). */
     727                 :        187 :         return eap_tls_process_output(data, eap_type, peap_version, id, ret,
     728                 :            :                                       out_data);
     729                 :            : }
     730                 :            : 
     731                 :            : 
     732                 :            : /**
     733                 :            :  * eap_peer_tls_build_ack - Build a TLS ACK frame
     734                 :            :  * @id: EAP identifier for the response
     735                 :            :  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
     736                 :            :  * @peap_version: Version number for EAP-PEAP/TTLS
     737                 :            :  * Returns: Pointer to the allocated ACK frame or %NULL on failure
     738                 :            :  */
     739                 :         80 : struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
     740                 :            :                                        int peap_version)
     741                 :            : {
     742                 :            :         struct wpabuf *resp;
     743                 :            : 
     744                 :         80 :         resp = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_RESPONSE, id);
     745         [ -  + ]:         80 :         if (resp == NULL)
     746                 :          0 :                 return NULL;
     747                 :         80 :         wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
     748                 :            :                    (int) eap_type, id, peap_version);
     749                 :         80 :         wpabuf_put_u8(resp, peap_version); /* Flags */
     750                 :         80 :         return resp;
     751                 :            : }
     752                 :            : 
     753                 :            : 
     754                 :            : /**
     755                 :            :  * eap_peer_tls_reauth_init - Re-initialize shared TLS for session resumption
     756                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     757                 :            :  * @data: Data for TLS processing
     758                 :            :  * Returns: 0 on success, -1 on failure
     759                 :            :  */
     760                 :         12 : int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
     761                 :            : {
     762                 :         12 :         eap_peer_tls_reset_input(data);
     763                 :         12 :         eap_peer_tls_reset_output(data);
     764                 :         12 :         return tls_connection_shutdown(data->ssl_ctx, data->conn);
     765                 :            : }
     766                 :            : 
     767                 :            : 
     768                 :            : /**
     769                 :            :  * eap_peer_tls_status - Get TLS status
     770                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     771                 :            :  * @data: Data for TLS processing
     772                 :            :  * @buf: Buffer for status information
     773                 :            :  * @buflen: Maximum buffer length
     774                 :            :  * @verbose: Whether to include verbose status information
     775                 :            :  * Returns: Number of bytes written to buf.
     776                 :            :  */
     777                 :         32 : int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
     778                 :            :                         char *buf, size_t buflen, int verbose)
     779                 :            : {
     780                 :            :         char name[128];
     781                 :         32 :         int len = 0, ret;
     782                 :            : 
     783         [ +  - ]:         32 :         if (tls_get_cipher(data->ssl_ctx, data->conn, name, sizeof(name)) == 0)
     784                 :            :         {
     785                 :         32 :                 ret = os_snprintf(buf + len, buflen - len,
     786                 :            :                                   "EAP TLS cipher=%s\n", name);
     787 [ +  - ][ -  + ]:         32 :                 if (ret < 0 || (size_t) ret >= buflen - len)
     788                 :          0 :                         return len;
     789                 :         32 :                 len += ret;
     790                 :            :         }
     791                 :            : 
     792                 :         32 :         return len;
     793                 :            : }
     794                 :            : 
     795                 :            : 
     796                 :            : /**
     797                 :            :  * eap_peer_tls_process_init - Initial validation/processing of EAP requests
     798                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     799                 :            :  * @data: Data for TLS processing
     800                 :            :  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
     801                 :            :  * @ret: Return values from EAP request validation and processing
     802                 :            :  * @reqData: EAP request to be processed (eapReqData)
     803                 :            :  * @len: Buffer for returning length of the remaining payload
     804                 :            :  * @flags: Buffer for returning TLS flags
     805                 :            :  * Returns: Pointer to payload after TLS flags and length or %NULL on failure
     806                 :            :  *
     807                 :            :  * This function validates the EAP header and processes the optional TLS
     808                 :            :  * Message Length field. If this is the first fragment of a TLS message, the
     809                 :            :  * TLS reassembly code is initialized to receive the indicated number of bytes.
     810                 :            :  *
     811                 :            :  * EAP-TLS, EAP-PEAP, EAP-TTLS, and EAP-FAST methods are expected to use this
     812                 :            :  * function as the first step in processing received messages. They will need
     813                 :            :  * to process the flags (apart from Message Length Included) that are returned
     814                 :            :  * through the flags pointer and the message payload that will be returned (and
     815                 :            :  * the length is returned through the len pointer). Return values (ret) are set
     816                 :            :  * for continuation of EAP method processing. The caller is responsible for
     817                 :            :  * setting these to indicate completion (either success or failure) based on
     818                 :            :  * the authentication result.
     819                 :            :  */
     820                 :        259 : const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
     821                 :            :                                      struct eap_ssl_data *data,
     822                 :            :                                      EapType eap_type,
     823                 :            :                                      struct eap_method_ret *ret,
     824                 :            :                                      const struct wpabuf *reqData,
     825                 :            :                                      size_t *len, u8 *flags)
     826                 :            : {
     827                 :            :         const u8 *pos;
     828                 :            :         size_t left;
     829                 :            :         unsigned int tls_msg_len;
     830                 :            : 
     831         [ -  + ]:        259 :         if (tls_get_errors(data->ssl_ctx)) {
     832                 :          0 :                 wpa_printf(MSG_INFO, "SSL: TLS errors detected");
     833                 :          0 :                 ret->ignore = TRUE;
     834                 :          0 :                 return NULL;
     835                 :            :         }
     836                 :            : 
     837         [ -  + ]:        259 :         if (eap_type == EAP_UNAUTH_TLS_TYPE)
     838                 :          0 :                 pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
     839                 :            :                                        EAP_VENDOR_TYPE_UNAUTH_TLS, reqData,
     840                 :            :                                        &left);
     841                 :            :         else
     842                 :        259 :                 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData,
     843                 :            :                                        &left);
     844         [ -  + ]:        259 :         if (pos == NULL) {
     845                 :          0 :                 ret->ignore = TRUE;
     846                 :          0 :                 return NULL;
     847                 :            :         }
     848         [ -  + ]:        259 :         if (left == 0) {
     849                 :          0 :                 wpa_printf(MSG_DEBUG, "SSL: Invalid TLS message: no Flags "
     850                 :            :                            "octet included");
     851         [ #  # ]:          0 :                 if (!sm->workaround) {
     852                 :          0 :                         ret->ignore = TRUE;
     853                 :          0 :                         return NULL;
     854                 :            :                 }
     855                 :            : 
     856                 :          0 :                 wpa_printf(MSG_DEBUG, "SSL: Workaround - assume no Flags "
     857                 :            :                            "indicates ACK frame");
     858                 :          0 :                 *flags = 0;
     859                 :            :         } else {
     860                 :        259 :                 *flags = *pos++;
     861                 :        259 :                 left--;
     862                 :            :         }
     863                 :        259 :         wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - "
     864                 :            :                    "Flags 0x%02x", (unsigned long) wpabuf_len(reqData),
     865                 :        259 :                    *flags);
     866         [ +  + ]:        259 :         if (*flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
     867         [ -  + ]:         46 :                 if (left < 4) {
     868                 :          0 :                         wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
     869                 :            :                                    "length");
     870                 :          0 :                         ret->ignore = TRUE;
     871                 :          0 :                         return NULL;
     872                 :            :                 }
     873                 :         46 :                 tls_msg_len = WPA_GET_BE32(pos);
     874                 :         46 :                 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
     875                 :            :                            tls_msg_len);
     876         [ +  - ]:         46 :                 if (data->tls_in_left == 0) {
     877                 :         46 :                         data->tls_in_total = tls_msg_len;
     878                 :         46 :                         data->tls_in_left = tls_msg_len;
     879                 :         46 :                         wpabuf_free(data->tls_in);
     880                 :         46 :                         data->tls_in = NULL;
     881                 :            :                 }
     882                 :         46 :                 pos += 4;
     883                 :         46 :                 left -= 4;
     884                 :            : 
     885         [ -  + ]:         46 :                 if (left > tls_msg_len) {
     886                 :          0 :                         wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
     887                 :            :                                    "bytes) smaller than this fragment (%d "
     888                 :            :                                    "bytes)", (int) tls_msg_len, (int) left);
     889                 :          0 :                         ret->ignore = TRUE;
     890                 :          0 :                         return NULL;
     891                 :            :                 }
     892                 :            :         }
     893                 :            : 
     894                 :        259 :         ret->ignore = FALSE;
     895                 :        259 :         ret->methodState = METHOD_MAY_CONT;
     896                 :        259 :         ret->decision = DECISION_FAIL;
     897                 :        259 :         ret->allowNotifications = TRUE;
     898                 :            : 
     899                 :        259 :         *len = left;
     900                 :        259 :         return pos;
     901                 :            : }
     902                 :            : 
     903                 :            : 
     904                 :            : /**
     905                 :            :  * eap_peer_tls_reset_input - Reset input buffers
     906                 :            :  * @data: Data for TLS processing
     907                 :            :  *
     908                 :            :  * This function frees any allocated memory for input buffers and resets input
     909                 :            :  * state.
     910                 :            :  */
     911                 :        258 : void eap_peer_tls_reset_input(struct eap_ssl_data *data)
     912                 :            : {
     913                 :        258 :         data->tls_in_left = data->tls_in_total = 0;
     914                 :        258 :         wpabuf_free(data->tls_in);
     915                 :        258 :         data->tls_in = NULL;
     916                 :        258 : }
     917                 :            : 
     918                 :            : 
     919                 :            : /**
     920                 :            :  * eap_peer_tls_reset_output - Reset output buffers
     921                 :            :  * @data: Data for TLS processing
     922                 :            :  *
     923                 :            :  * This function frees any allocated memory for output buffers and resets
     924                 :            :  * output state.
     925                 :            :  */
     926                 :        305 : void eap_peer_tls_reset_output(struct eap_ssl_data *data)
     927                 :            : {
     928                 :        305 :         data->tls_out_pos = 0;
     929                 :        305 :         wpabuf_free(data->tls_out);
     930                 :        305 :         data->tls_out = NULL;
     931                 :        305 : }
     932                 :            : 
     933                 :            : 
     934                 :            : /**
     935                 :            :  * eap_peer_tls_decrypt - Decrypt received phase 2 TLS message
     936                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     937                 :            :  * @data: Data for TLS processing
     938                 :            :  * @in_data: Message received from the server
     939                 :            :  * @in_decrypted: Buffer for returning a pointer to the decrypted message
     940                 :            :  * Returns: 0 on success, 1 if more input data is needed, or -1 on failure
     941                 :            :  */
     942                 :         72 : int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
     943                 :            :                          const struct wpabuf *in_data,
     944                 :            :                          struct wpabuf **in_decrypted)
     945                 :            : {
     946                 :            :         const struct wpabuf *msg;
     947                 :            :         int need_more_input;
     948                 :            : 
     949                 :         72 :         msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
     950         [ -  + ]:         72 :         if (msg == NULL)
     951         [ #  # ]:          0 :                 return need_more_input ? 1 : -1;
     952                 :            : 
     953                 :         72 :         *in_decrypted = tls_connection_decrypt(data->ssl_ctx, data->conn, msg);
     954                 :         72 :         eap_peer_tls_reset_input(data);
     955         [ -  + ]:         72 :         if (*in_decrypted == NULL) {
     956                 :          0 :                 wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
     957                 :          0 :                 return -1;
     958                 :            :         }
     959                 :         72 :         return 0;
     960                 :            : }
     961                 :            : 
     962                 :            : 
     963                 :            : /**
     964                 :            :  * eap_peer_tls_encrypt - Encrypt phase 2 TLS message
     965                 :            :  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
     966                 :            :  * @data: Data for TLS processing
     967                 :            :  * @eap_type: EAP type (EAP_TYPE_TLS, EAP_TYPE_PEAP, ...)
     968                 :            :  * @peap_version: Version number for EAP-PEAP/TTLS
     969                 :            :  * @id: EAP identifier for the response
     970                 :            :  * @in_data: Plaintext phase 2 data to encrypt or %NULL to continue fragments
     971                 :            :  * @out_data: Buffer for returning a pointer to the encrypted response message
     972                 :            :  * Returns: 0 on success, -1 on failure
     973                 :            :  */
     974                 :         82 : int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
     975                 :            :                          EapType eap_type, int peap_version, u8 id,
     976                 :            :                          const struct wpabuf *in_data,
     977                 :            :                          struct wpabuf **out_data)
     978                 :            : {
     979         [ +  - ]:         82 :         if (in_data) {
     980                 :         82 :                 eap_peer_tls_reset_output(data);
     981                 :         82 :                 data->tls_out = tls_connection_encrypt(data->ssl_ctx,
     982                 :            :                                                        data->conn, in_data);
     983         [ -  + ]:         82 :                 if (data->tls_out == NULL) {
     984                 :          0 :                         wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
     985                 :            :                                    "data (in_len=%lu)",
     986                 :            :                                    (unsigned long) wpabuf_len(in_data));
     987                 :          0 :                         eap_peer_tls_reset_output(data);
     988                 :          0 :                         return -1;
     989                 :            :                 }
     990                 :            :         }
     991                 :            : 
     992                 :         82 :         return eap_tls_process_output(data, eap_type, peap_version, id, 0,
     993                 :            :                                       out_data);
     994                 :            : }
     995                 :            : 
     996                 :            : 
     997                 :            : /**
     998                 :            :  * eap_peer_select_phase2_methods - Select phase 2 EAP method
     999                 :            :  * @config: Pointer to the network configuration
    1000                 :            :  * @prefix: 'phase2' configuration prefix, e.g., "auth="
    1001                 :            :  * @types: Buffer for returning allocated list of allowed EAP methods
    1002                 :            :  * @num_types: Buffer for returning number of allocated EAP methods
    1003                 :            :  * Returns: 0 on success, -1 on failure
    1004                 :            :  *
    1005                 :            :  * This function is used to parse EAP method list and select allowed methods
    1006                 :            :  * for Phase2 authentication.
    1007                 :            :  */
    1008                 :         12 : int eap_peer_select_phase2_methods(struct eap_peer_config *config,
    1009                 :            :                                    const char *prefix,
    1010                 :            :                                    struct eap_method_type **types,
    1011                 :            :                                    size_t *num_types)
    1012                 :            : {
    1013                 :            :         char *start, *pos, *buf;
    1014                 :         12 :         struct eap_method_type *methods = NULL, *_methods;
    1015                 :            :         u8 method;
    1016                 :         12 :         size_t num_methods = 0, prefix_len;
    1017                 :            : 
    1018 [ +  - ][ +  + ]:         12 :         if (config == NULL || config->phase2 == NULL)
    1019                 :            :                 goto get_defaults;
    1020                 :            : 
    1021                 :         11 :         start = buf = os_strdup(config->phase2);
    1022         [ -  + ]:         11 :         if (buf == NULL)
    1023                 :          0 :                 return -1;
    1024                 :            : 
    1025                 :         11 :         prefix_len = os_strlen(prefix);
    1026                 :            : 
    1027 [ +  + ][ +  - ]:         22 :         while (start && *start != '\0') {
    1028                 :            :                 int vendor;
    1029                 :         11 :                 pos = os_strstr(start, prefix);
    1030         [ -  + ]:         11 :                 if (pos == NULL)
    1031                 :          0 :                         break;
    1032 [ -  + ][ #  # ]:         11 :                 if (start != pos && *(pos - 1) != ' ') {
    1033                 :          0 :                         start = pos + prefix_len;
    1034                 :          0 :                         continue;
    1035                 :            :                 }
    1036                 :            : 
    1037                 :         11 :                 start = pos + prefix_len;
    1038                 :         11 :                 pos = os_strchr(start, ' ');
    1039         [ -  + ]:         11 :                 if (pos)
    1040                 :          0 :                         *pos++ = '\0';
    1041                 :         11 :                 method = eap_get_phase2_type(start, &vendor);
    1042 [ +  - ][ -  + ]:         11 :                 if (vendor == EAP_VENDOR_IETF && method == EAP_TYPE_NONE) {
    1043                 :          0 :                         wpa_printf(MSG_ERROR, "TLS: Unsupported Phase2 EAP "
    1044                 :            :                                    "method '%s'", start);
    1045                 :            :                 } else {
    1046                 :         11 :                         num_methods++;
    1047                 :         11 :                         _methods = os_realloc_array(methods, num_methods,
    1048                 :            :                                                     sizeof(*methods));
    1049         [ -  + ]:         11 :                         if (_methods == NULL) {
    1050                 :          0 :                                 os_free(methods);
    1051                 :          0 :                                 os_free(buf);
    1052                 :          0 :                                 return -1;
    1053                 :            :                         }
    1054                 :         11 :                         methods = _methods;
    1055                 :         11 :                         methods[num_methods - 1].vendor = vendor;
    1056                 :         11 :                         methods[num_methods - 1].method = method;
    1057                 :            :                 }
    1058                 :            : 
    1059                 :         11 :                 start = pos;
    1060                 :            :         }
    1061                 :            : 
    1062                 :         11 :         os_free(buf);
    1063                 :            : 
    1064                 :            : get_defaults:
    1065         [ +  + ]:         12 :         if (methods == NULL)
    1066                 :          1 :                 methods = eap_get_phase2_types(config, &num_methods);
    1067                 :            : 
    1068         [ -  + ]:         12 :         if (methods == NULL) {
    1069                 :          0 :                 wpa_printf(MSG_ERROR, "TLS: No Phase2 EAP methods available");
    1070                 :          0 :                 return -1;
    1071                 :            :         }
    1072                 :         12 :         wpa_hexdump(MSG_DEBUG, "TLS: Phase2 EAP types",
    1073                 :            :                     (u8 *) methods,
    1074                 :            :                     num_methods * sizeof(struct eap_method_type));
    1075                 :            : 
    1076                 :         12 :         *types = methods;
    1077                 :         12 :         *num_types = num_methods;
    1078                 :            : 
    1079                 :         12 :         return 0;
    1080                 :            : }
    1081                 :            : 
    1082                 :            : 
    1083                 :            : /**
    1084                 :            :  * eap_peer_tls_phase2_nak - Generate EAP-Nak for Phase 2
    1085                 :            :  * @types: Buffer for returning allocated list of allowed EAP methods
    1086                 :            :  * @num_types: Buffer for returning number of allocated EAP methods
    1087                 :            :  * @hdr: EAP-Request header (and the following EAP type octet)
    1088                 :            :  * @resp: Buffer for returning the EAP-Nak message
    1089                 :            :  * Returns: 0 on success, -1 on failure
    1090                 :            :  */
    1091                 :          6 : int eap_peer_tls_phase2_nak(struct eap_method_type *types, size_t num_types,
    1092                 :            :                             struct eap_hdr *hdr, struct wpabuf **resp)
    1093                 :            : {
    1094                 :          6 :         u8 *pos = (u8 *) (hdr + 1);
    1095                 :            :         size_t i;
    1096                 :            : 
    1097                 :            :         /* TODO: add support for expanded Nak */
    1098                 :          6 :         wpa_printf(MSG_DEBUG, "TLS: Phase 2 Request: Nak type=%d", *pos);
    1099                 :          6 :         wpa_hexdump(MSG_DEBUG, "TLS: Allowed Phase2 EAP types",
    1100                 :            :                     (u8 *) types, num_types * sizeof(struct eap_method_type));
    1101                 :          6 :         *resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, num_types,
    1102                 :          6 :                               EAP_CODE_RESPONSE, hdr->identifier);
    1103         [ -  + ]:          6 :         if (*resp == NULL)
    1104                 :          0 :                 return -1;
    1105                 :            : 
    1106         [ +  + ]:         12 :         for (i = 0; i < num_types; i++) {
    1107 [ +  - ][ +  - ]:          6 :                 if (types[i].vendor == EAP_VENDOR_IETF &&
    1108                 :          6 :                     types[i].method < 256)
    1109                 :          6 :                         wpabuf_put_u8(*resp, types[i].method);
    1110                 :            :         }
    1111                 :            : 
    1112                 :          6 :         eap_update_len(*resp);
    1113                 :            : 
    1114                 :          6 :         return 0;
    1115                 :            : }

Generated by: LCOV version 1.9