LCOV - code coverage report
Current view: top level - src/eap_peer - eap_tls.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 166 214 77.6 %
Date: 2014-03-02 Functions: 16 18 88.9 %
Branches: 43 92 46.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * EAP peer method: EAP-TLS (RFC 2716)
       3                 :            :  * Copyright (c) 2004-2008, 2012, 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/tls.h"
      13                 :            : #include "eap_i.h"
      14                 :            : #include "eap_tls_common.h"
      15                 :            : #include "eap_config.h"
      16                 :            : 
      17                 :            : 
      18                 :            : static void eap_tls_deinit(struct eap_sm *sm, void *priv);
      19                 :            : 
      20                 :            : 
      21                 :            : struct eap_tls_data {
      22                 :            :         struct eap_ssl_data ssl;
      23                 :            :         u8 *key_data;
      24                 :            :         u8 *session_id;
      25                 :            :         size_t id_len;
      26                 :            :         void *ssl_ctx;
      27                 :            :         u8 eap_type;
      28                 :            : };
      29                 :            : 
      30                 :            : 
      31                 :         11 : static void * eap_tls_init(struct eap_sm *sm)
      32                 :            : {
      33                 :            :         struct eap_tls_data *data;
      34                 :         11 :         struct eap_peer_config *config = eap_get_config(sm);
      35 [ +  - ][ +  + ]:         11 :         if (config == NULL ||
         [ -  + ][ #  # ]
      36                 :         22 :             ((sm->init_phase2 ? config->private_key2 : config->private_key)
      37 [ -  + ][ #  # ]:         10 :              == NULL &&
      38         [ #  # ]:          0 :              (sm->init_phase2 ? config->engine2 : config->engine) == 0)) {
      39                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TLS: Private key not configured");
      40                 :          0 :                 return NULL;
      41                 :            :         }
      42                 :            : 
      43                 :         11 :         data = os_zalloc(sizeof(*data));
      44         [ -  + ]:         11 :         if (data == NULL)
      45                 :          0 :                 return NULL;
      46                 :            : 
      47 [ +  + ][ +  - ]:         11 :         data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
      48                 :            :                 sm->ssl_ctx;
      49                 :            : 
      50         [ +  + ]:         11 :         if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TLS)) {
      51                 :          1 :                 wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
      52                 :          1 :                 eap_tls_deinit(sm, data);
      53         [ -  + ]:          1 :                 if (config->engine) {
      54                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting Smartcard "
      55                 :            :                                    "PIN");
      56                 :          0 :                         eap_sm_request_pin(sm);
      57                 :          0 :                         sm->ignore = TRUE;
      58 [ +  - ][ +  - ]:          1 :                 } else if (config->private_key && !config->private_key_passwd)
      59                 :            :                 {
      60                 :          1 :                         wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting private "
      61                 :            :                                    "key passphrase");
      62                 :          1 :                         eap_sm_request_passphrase(sm);
      63                 :          1 :                         sm->ignore = TRUE;
      64                 :            :                 }
      65                 :          1 :                 return NULL;
      66                 :            :         }
      67                 :            : 
      68                 :         10 :         data->eap_type = EAP_TYPE_TLS;
      69                 :            : 
      70                 :         11 :         return data;
      71                 :            : }
      72                 :            : 
      73                 :            : 
      74                 :            : #ifdef EAP_UNAUTH_TLS
      75                 :          0 : static void * eap_unauth_tls_init(struct eap_sm *sm)
      76                 :            : {
      77                 :            :         struct eap_tls_data *data;
      78                 :          0 :         struct eap_peer_config *config = eap_get_config(sm);
      79                 :            : 
      80                 :          0 :         data = os_zalloc(sizeof(*data));
      81         [ #  # ]:          0 :         if (data == NULL)
      82                 :          0 :                 return NULL;
      83                 :            : 
      84 [ #  # ][ #  # ]:          0 :         data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
      85                 :            :                 sm->ssl_ctx;
      86                 :            : 
      87         [ #  # ]:          0 :         if (eap_peer_tls_ssl_init(sm, &data->ssl, config,
      88                 :            :                                   EAP_UNAUTH_TLS_TYPE)) {
      89                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
      90                 :          0 :                 eap_tls_deinit(sm, data);
      91                 :          0 :                 return NULL;
      92                 :            :         }
      93                 :            : 
      94                 :          0 :         data->eap_type = EAP_UNAUTH_TLS_TYPE;
      95                 :            : 
      96                 :          0 :         return data;
      97                 :            : }
      98                 :            : #endif /* EAP_UNAUTH_TLS */
      99                 :            : 
     100                 :            : 
     101                 :            : #ifdef CONFIG_HS20
     102                 :          1 : static void * eap_wfa_unauth_tls_init(struct eap_sm *sm)
     103                 :            : {
     104                 :            :         struct eap_tls_data *data;
     105                 :          1 :         struct eap_peer_config *config = eap_get_config(sm);
     106                 :            : 
     107                 :          1 :         data = os_zalloc(sizeof(*data));
     108         [ -  + ]:          1 :         if (data == NULL)
     109                 :          0 :                 return NULL;
     110                 :            : 
     111 [ -  + ][ #  # ]:          1 :         data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
     112                 :            :                 sm->ssl_ctx;
     113                 :            : 
     114         [ -  + ]:          1 :         if (eap_peer_tls_ssl_init(sm, &data->ssl, config,
     115                 :            :                                   EAP_WFA_UNAUTH_TLS_TYPE)) {
     116                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
     117                 :          0 :                 eap_tls_deinit(sm, data);
     118                 :          0 :                 return NULL;
     119                 :            :         }
     120                 :            : 
     121                 :          1 :         data->eap_type = EAP_WFA_UNAUTH_TLS_TYPE;
     122                 :            : 
     123                 :          1 :         return data;
     124                 :            : }
     125                 :            : #endif /* CONFIG_HS20 */
     126                 :            : 
     127                 :            : 
     128                 :         12 : static void eap_tls_deinit(struct eap_sm *sm, void *priv)
     129                 :            : {
     130                 :         12 :         struct eap_tls_data *data = priv;
     131         [ -  + ]:         12 :         if (data == NULL)
     132                 :         12 :                 return;
     133                 :         12 :         eap_peer_tls_ssl_deinit(sm, &data->ssl);
     134                 :         12 :         os_free(data->key_data);
     135                 :         12 :         os_free(data->session_id);
     136                 :         12 :         os_free(data);
     137                 :            : }
     138                 :            : 
     139                 :            : 
     140                 :          2 : static struct wpabuf * eap_tls_failure(struct eap_sm *sm,
     141                 :            :                                        struct eap_tls_data *data,
     142                 :            :                                        struct eap_method_ret *ret, int res,
     143                 :            :                                        struct wpabuf *resp, u8 id)
     144                 :            : {
     145                 :          2 :         wpa_printf(MSG_DEBUG, "EAP-TLS: TLS processing failed");
     146                 :            : 
     147                 :          2 :         ret->methodState = METHOD_DONE;
     148                 :          2 :         ret->decision = DECISION_FAIL;
     149                 :            : 
     150         [ +  - ]:          2 :         if (res == -1) {
     151                 :          2 :                 struct eap_peer_config *config = eap_get_config(sm);
     152         [ +  - ]:          2 :                 if (config) {
     153                 :            :                         /*
     154                 :            :                          * The TLS handshake failed. So better forget the old
     155                 :            :                          * PIN. It may be wrong, we cannot be sure but trying
     156                 :            :                          * the wrong one again might block it on the card--so
     157                 :            :                          * better ask the user again.
     158                 :            :                          */
     159                 :          2 :                         os_free(config->pin);
     160                 :          2 :                         config->pin = NULL;
     161                 :            :                 }
     162                 :            :         }
     163                 :            : 
     164         [ +  - ]:          2 :         if (resp) {
     165                 :            :                 /*
     166                 :            :                  * This is likely an alert message, so send it instead of just
     167                 :            :                  * ACKing the error.
     168                 :            :                  */
     169                 :          2 :                 return resp;
     170                 :            :         }
     171                 :            : 
     172                 :          2 :         return eap_peer_tls_build_ack(id, data->eap_type, 0);
     173                 :            : }
     174                 :            : 
     175                 :            : 
     176                 :         11 : static void eap_tls_success(struct eap_sm *sm, struct eap_tls_data *data,
     177                 :            :                             struct eap_method_ret *ret)
     178                 :            : {
     179                 :         11 :         wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
     180                 :            : 
     181                 :         11 :         ret->methodState = METHOD_DONE;
     182                 :         11 :         ret->decision = DECISION_UNCOND_SUCC;
     183                 :            : 
     184                 :         11 :         os_free(data->key_data);
     185                 :         11 :         data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
     186                 :            :                                                  "client EAP encryption",
     187                 :            :                                                  EAP_TLS_KEY_LEN +
     188                 :            :                                                  EAP_EMSK_LEN);
     189         [ +  - ]:         11 :         if (data->key_data) {
     190                 :         11 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived key",
     191                 :         11 :                                 data->key_data, EAP_TLS_KEY_LEN);
     192                 :         11 :                 wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived EMSK",
     193                 :         11 :                                 data->key_data + EAP_TLS_KEY_LEN,
     194                 :            :                                 EAP_EMSK_LEN);
     195                 :            :         } else {
     196                 :          0 :                 wpa_printf(MSG_INFO, "EAP-TLS: Failed to derive key");
     197                 :            :         }
     198                 :            : 
     199                 :         11 :         os_free(data->session_id);
     200                 :         11 :         data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
     201                 :            :                                                           EAP_TYPE_TLS,
     202                 :            :                                                           &data->id_len);
     203         [ +  - ]:         11 :         if (data->session_id) {
     204                 :         11 :                 wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived Session-Id",
     205                 :         11 :                             data->session_id, data->id_len);
     206                 :            :         } else {
     207                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-TLS: Failed to derive Session-Id");
     208                 :            :         }
     209                 :         11 : }
     210                 :            : 
     211                 :            : 
     212                 :         57 : static struct wpabuf * eap_tls_process(struct eap_sm *sm, void *priv,
     213                 :            :                                        struct eap_method_ret *ret,
     214                 :            :                                        const struct wpabuf *reqData)
     215                 :            : {
     216                 :            :         size_t left;
     217                 :            :         int res;
     218                 :            :         struct wpabuf *resp;
     219                 :            :         u8 flags, id;
     220                 :            :         const u8 *pos;
     221                 :         57 :         struct eap_tls_data *data = priv;
     222                 :            : 
     223                 :         57 :         pos = eap_peer_tls_process_init(sm, &data->ssl, data->eap_type, ret,
     224                 :            :                                         reqData, &left, &flags);
     225         [ -  + ]:         57 :         if (pos == NULL)
     226                 :          0 :                 return NULL;
     227                 :         57 :         id = eap_get_id(reqData);
     228                 :            : 
     229         [ +  + ]:         57 :         if (flags & EAP_TLS_FLAGS_START) {
     230                 :         13 :                 wpa_printf(MSG_DEBUG, "EAP-TLS: Start");
     231                 :         13 :                 left = 0; /* make sure that this frame is empty, even though it
     232                 :            :                            * should always be, anyway */
     233                 :            :         }
     234                 :            : 
     235                 :         57 :         resp = NULL;
     236                 :         57 :         res = eap_peer_tls_process_helper(sm, &data->ssl, data->eap_type, 0,
     237                 :            :                                           id, pos, left, &resp);
     238                 :            : 
     239         [ +  + ]:         57 :         if (res < 0) {
     240                 :          2 :                 return eap_tls_failure(sm, data, ret, res, resp, id);
     241                 :            :         }
     242                 :            : 
     243         [ +  + ]:         55 :         if (tls_connection_established(data->ssl_ctx, data->ssl.conn))
     244                 :         11 :                 eap_tls_success(sm, data, ret);
     245                 :            : 
     246         [ +  + ]:         55 :         if (res == 1) {
     247                 :         21 :                 wpabuf_free(resp);
     248                 :         21 :                 return eap_peer_tls_build_ack(id, data->eap_type, 0);
     249                 :            :         }
     250                 :            : 
     251                 :         57 :         return resp;
     252                 :            : }
     253                 :            : 
     254                 :            : 
     255                 :          2 : static Boolean eap_tls_has_reauth_data(struct eap_sm *sm, void *priv)
     256                 :            : {
     257                 :          2 :         struct eap_tls_data *data = priv;
     258                 :          2 :         return tls_connection_established(data->ssl_ctx, data->ssl.conn);
     259                 :            : }
     260                 :            : 
     261                 :            : 
     262                 :          1 : static void eap_tls_deinit_for_reauth(struct eap_sm *sm, void *priv)
     263                 :            : {
     264                 :          1 : }
     265                 :            : 
     266                 :            : 
     267                 :          2 : static void * eap_tls_init_for_reauth(struct eap_sm *sm, void *priv)
     268                 :            : {
     269                 :          2 :         struct eap_tls_data *data = priv;
     270                 :          2 :         os_free(data->key_data);
     271                 :          2 :         data->key_data = NULL;
     272                 :          2 :         os_free(data->session_id);
     273                 :          2 :         data->session_id = NULL;
     274         [ -  + ]:          2 :         if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
     275                 :          0 :                 os_free(data);
     276                 :          0 :                 return NULL;
     277                 :            :         }
     278                 :          2 :         return priv;
     279                 :            : }
     280                 :            : 
     281                 :            : 
     282                 :          4 : static int eap_tls_get_status(struct eap_sm *sm, void *priv, char *buf,
     283                 :            :                               size_t buflen, int verbose)
     284                 :            : {
     285                 :          4 :         struct eap_tls_data *data = priv;
     286                 :          4 :         return eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
     287                 :            : }
     288                 :            : 
     289                 :            : 
     290                 :         47 : static Boolean eap_tls_isKeyAvailable(struct eap_sm *sm, void *priv)
     291                 :            : {
     292                 :         47 :         struct eap_tls_data *data = priv;
     293                 :         47 :         return data->key_data != NULL;
     294                 :            : }
     295                 :            : 
     296                 :            : 
     297                 :          9 : static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
     298                 :            : {
     299                 :          9 :         struct eap_tls_data *data = priv;
     300                 :            :         u8 *key;
     301                 :            : 
     302         [ -  + ]:          9 :         if (data->key_data == NULL)
     303                 :          0 :                 return NULL;
     304                 :            : 
     305                 :          9 :         key = os_malloc(EAP_TLS_KEY_LEN);
     306         [ -  + ]:          9 :         if (key == NULL)
     307                 :          0 :                 return NULL;
     308                 :            : 
     309                 :          9 :         *len = EAP_TLS_KEY_LEN;
     310                 :          9 :         os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
     311                 :            : 
     312                 :          9 :         return key;
     313                 :            : }
     314                 :            : 
     315                 :            : 
     316                 :          0 : static u8 * eap_tls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
     317                 :            : {
     318                 :          0 :         struct eap_tls_data *data = priv;
     319                 :            :         u8 *key;
     320                 :            : 
     321         [ #  # ]:          0 :         if (data->key_data == NULL)
     322                 :          0 :                 return NULL;
     323                 :            : 
     324                 :          0 :         key = os_malloc(EAP_EMSK_LEN);
     325         [ #  # ]:          0 :         if (key == NULL)
     326                 :          0 :                 return NULL;
     327                 :            : 
     328                 :          0 :         *len = EAP_EMSK_LEN;
     329                 :          0 :         os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
     330                 :            : 
     331                 :          0 :         return key;
     332                 :            : }
     333                 :            : 
     334                 :            : 
     335                 :          8 : static u8 * eap_tls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
     336                 :            : {
     337                 :          8 :         struct eap_tls_data *data = priv;
     338                 :            :         u8 *id;
     339                 :            : 
     340         [ -  + ]:          8 :         if (data->session_id == NULL)
     341                 :          0 :                 return NULL;
     342                 :            : 
     343                 :          8 :         id = os_malloc(data->id_len);
     344         [ -  + ]:          8 :         if (id == NULL)
     345                 :          0 :                 return NULL;
     346                 :            : 
     347                 :          8 :         *len = data->id_len;
     348                 :          8 :         os_memcpy(id, data->session_id, data->id_len);
     349                 :            : 
     350                 :          8 :         return id;
     351                 :            : }
     352                 :            : 
     353                 :            : 
     354                 :          4 : int eap_peer_tls_register(void)
     355                 :            : {
     356                 :            :         struct eap_method *eap;
     357                 :            :         int ret;
     358                 :            : 
     359                 :          4 :         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
     360                 :            :                                     EAP_VENDOR_IETF, EAP_TYPE_TLS, "TLS");
     361         [ -  + ]:          4 :         if (eap == NULL)
     362                 :          0 :                 return -1;
     363                 :            : 
     364                 :          4 :         eap->init = eap_tls_init;
     365                 :          4 :         eap->deinit = eap_tls_deinit;
     366                 :          4 :         eap->process = eap_tls_process;
     367                 :          4 :         eap->isKeyAvailable = eap_tls_isKeyAvailable;
     368                 :          4 :         eap->getKey = eap_tls_getKey;
     369                 :          4 :         eap->getSessionId = eap_tls_get_session_id;
     370                 :          4 :         eap->get_status = eap_tls_get_status;
     371                 :          4 :         eap->has_reauth_data = eap_tls_has_reauth_data;
     372                 :          4 :         eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
     373                 :          4 :         eap->init_for_reauth = eap_tls_init_for_reauth;
     374                 :          4 :         eap->get_emsk = eap_tls_get_emsk;
     375                 :            : 
     376                 :          4 :         ret = eap_peer_method_register(eap);
     377         [ -  + ]:          4 :         if (ret)
     378                 :          0 :                 eap_peer_method_free(eap);
     379                 :          4 :         return ret;
     380                 :            : }
     381                 :            : 
     382                 :            : 
     383                 :            : #ifdef EAP_UNAUTH_TLS
     384                 :          4 : int eap_peer_unauth_tls_register(void)
     385                 :            : {
     386                 :            :         struct eap_method *eap;
     387                 :            :         int ret;
     388                 :            : 
     389                 :          4 :         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
     390                 :            :                                     EAP_VENDOR_UNAUTH_TLS,
     391                 :            :                                     EAP_VENDOR_TYPE_UNAUTH_TLS, "UNAUTH-TLS");
     392         [ -  + ]:          4 :         if (eap == NULL)
     393                 :          0 :                 return -1;
     394                 :            : 
     395                 :          4 :         eap->init = eap_unauth_tls_init;
     396                 :          4 :         eap->deinit = eap_tls_deinit;
     397                 :          4 :         eap->process = eap_tls_process;
     398                 :          4 :         eap->isKeyAvailable = eap_tls_isKeyAvailable;
     399                 :          4 :         eap->getKey = eap_tls_getKey;
     400                 :          4 :         eap->get_status = eap_tls_get_status;
     401                 :          4 :         eap->has_reauth_data = eap_tls_has_reauth_data;
     402                 :          4 :         eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
     403                 :          4 :         eap->init_for_reauth = eap_tls_init_for_reauth;
     404                 :          4 :         eap->get_emsk = eap_tls_get_emsk;
     405                 :            : 
     406                 :          4 :         ret = eap_peer_method_register(eap);
     407         [ -  + ]:          4 :         if (ret)
     408                 :          0 :                 eap_peer_method_free(eap);
     409                 :          4 :         return ret;
     410                 :            : }
     411                 :            : #endif /* EAP_UNAUTH_TLS */
     412                 :            : 
     413                 :            : 
     414                 :            : #ifdef CONFIG_HS20
     415                 :          4 : int eap_peer_wfa_unauth_tls_register(void)
     416                 :            : {
     417                 :            :         struct eap_method *eap;
     418                 :            :         int ret;
     419                 :            : 
     420                 :          4 :         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
     421                 :            :                                     EAP_VENDOR_WFA_NEW,
     422                 :            :                                     EAP_VENDOR_WFA_UNAUTH_TLS,
     423                 :            :                                     "WFA-UNAUTH-TLS");
     424         [ -  + ]:          4 :         if (eap == NULL)
     425                 :          0 :                 return -1;
     426                 :            : 
     427                 :          4 :         eap->init = eap_wfa_unauth_tls_init;
     428                 :          4 :         eap->deinit = eap_tls_deinit;
     429                 :          4 :         eap->process = eap_tls_process;
     430                 :          4 :         eap->isKeyAvailable = eap_tls_isKeyAvailable;
     431                 :          4 :         eap->getKey = eap_tls_getKey;
     432                 :          4 :         eap->get_status = eap_tls_get_status;
     433                 :          4 :         eap->has_reauth_data = eap_tls_has_reauth_data;
     434                 :          4 :         eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
     435                 :          4 :         eap->init_for_reauth = eap_tls_init_for_reauth;
     436                 :          4 :         eap->get_emsk = eap_tls_get_emsk;
     437                 :            : 
     438                 :          4 :         ret = eap_peer_method_register(eap);
     439         [ -  + ]:          4 :         if (ret)
     440                 :          0 :                 eap_peer_method_free(eap);
     441                 :          4 :         return ret;
     442                 :            : }
     443                 :            : #endif /* CONFIG_HS20 */

Generated by: LCOV version 1.9