LCOV - code coverage report
Current view: top level - src/eap_server - eap_server_identity.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1443382998 Lines: 72 82 87.8 %
Date: 2015-09-27 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :  * hostapd / EAP-Identity
       3             :  * Copyright (c) 2004-2006, 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 "eap_i.h"
      13             : 
      14             : 
      15             : struct eap_identity_data {
      16             :         enum { CONTINUE, SUCCESS, FAILURE } state;
      17             :         int pick_up;
      18             : };
      19             : 
      20             : 
      21        2560 : static void * eap_identity_init(struct eap_sm *sm)
      22             : {
      23             :         struct eap_identity_data *data;
      24             : 
      25        2560 :         data = os_zalloc(sizeof(*data));
      26        2560 :         if (data == NULL)
      27           2 :                 return NULL;
      28        2558 :         data->state = CONTINUE;
      29             : 
      30        2558 :         return data;
      31             : }
      32             : 
      33             : 
      34         791 : static void * eap_identity_initPickUp(struct eap_sm *sm)
      35             : {
      36             :         struct eap_identity_data *data;
      37         791 :         data = eap_identity_init(sm);
      38         791 :         if (data) {
      39         791 :                 data->pick_up = 1;
      40             :         }
      41         791 :         return data;
      42             : }
      43             : 
      44             : 
      45        2558 : static void eap_identity_reset(struct eap_sm *sm, void *priv)
      46             : {
      47        2558 :         struct eap_identity_data *data = priv;
      48        2558 :         os_free(data);
      49        2558 : }
      50             : 
      51             : 
      52        1738 : static struct wpabuf * eap_identity_buildReq(struct eap_sm *sm, void *priv,
      53             :                                              u8 id)
      54             : {
      55        1738 :         struct eap_identity_data *data = priv;
      56             :         struct wpabuf *req;
      57             :         const char *req_data;
      58             :         size_t req_data_len;
      59             : 
      60        1738 :         if (sm->eapol_cb->get_eap_req_id_text) {
      61        1738 :                 req_data = sm->eapol_cb->get_eap_req_id_text(sm->eapol_ctx,
      62             :                                                              &req_data_len);
      63             :         } else {
      64           0 :                 req_data = NULL;
      65           0 :                 req_data_len = 0;
      66             :         }
      67        1738 :         req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, req_data_len,
      68             :                             EAP_CODE_REQUEST, id);
      69        1738 :         if (req == NULL) {
      70           2 :                 wpa_printf(MSG_ERROR, "EAP-Identity: Failed to allocate "
      71             :                            "memory for request");
      72           2 :                 data->state = FAILURE;
      73           2 :                 return NULL;
      74             :         }
      75             : 
      76        1736 :         wpabuf_put_data(req, req_data, req_data_len);
      77             : 
      78        1736 :         return req;
      79             : }
      80             : 
      81             : 
      82        2541 : static Boolean eap_identity_check(struct eap_sm *sm, void *priv,
      83             :                                   struct wpabuf *respData)
      84             : {
      85             :         const u8 *pos;
      86             :         size_t len;
      87             : 
      88        2541 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
      89             :                                respData, &len);
      90        2541 :         if (pos == NULL) {
      91           0 :                 wpa_printf(MSG_INFO, "EAP-Identity: Invalid frame");
      92           0 :                 return TRUE;
      93             :         }
      94             : 
      95        2541 :         return FALSE;
      96             : }
      97             : 
      98             : 
      99        2541 : static void eap_identity_process(struct eap_sm *sm, void *priv,
     100             :                                  struct wpabuf *respData)
     101             : {
     102        2541 :         struct eap_identity_data *data = priv;
     103             :         const u8 *pos;
     104             :         size_t len;
     105             :         char *buf;
     106             : 
     107        2541 :         if (data->pick_up) {
     108         791 :                 if (eap_identity_check(sm, data, respData)) {
     109           0 :                         wpa_printf(MSG_DEBUG, "EAP-Identity: failed to pick "
     110             :                                    "up already started negotiation");
     111           0 :                         data->state = FAILURE;
     112           0 :                         return;
     113             :                 }
     114         791 :                 data->pick_up = 0;
     115             :         }
     116             : 
     117        2541 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
     118             :                                respData, &len);
     119        2541 :         if (pos == NULL)
     120           0 :                 return; /* Should not happen - frame already validated */
     121             : 
     122        2541 :         wpa_hexdump_ascii(MSG_DEBUG, "EAP-Identity: Peer identity", pos, len);
     123        2541 :         buf = os_malloc(len * 4 + 1);
     124        2541 :         if (buf) {
     125        2539 :                 printf_encode(buf, len * 4 + 1, pos, len);
     126        2539 :                 eap_log_msg(sm, "EAP-Response/Identity '%s'", buf);
     127        2539 :                 os_free(buf);
     128             :         }
     129        2541 :         if (sm->identity)
     130         125 :                 sm->update_user = TRUE;
     131        2541 :         os_free(sm->identity);
     132        2541 :         sm->identity = os_malloc(len ? len : 1);
     133        2541 :         if (sm->identity == NULL) {
     134           2 :                 data->state = FAILURE;
     135             :         } else {
     136        2539 :                 os_memcpy(sm->identity, pos, len);
     137        2539 :                 sm->identity_len = len;
     138        2539 :                 data->state = SUCCESS;
     139             :         }
     140             : }
     141             : 
     142             : 
     143        3946 : static Boolean eap_identity_isDone(struct eap_sm *sm, void *priv)
     144             : {
     145        3946 :         struct eap_identity_data *data = priv;
     146        3946 :         return data->state != CONTINUE;
     147             : }
     148             : 
     149             : 
     150        1489 : static Boolean eap_identity_isSuccess(struct eap_sm *sm, void *priv)
     151             : {
     152        1489 :         struct eap_identity_data *data = priv;
     153        1489 :         return data->state == SUCCESS;
     154             : }
     155             : 
     156             : 
     157          74 : int eap_server_identity_register(void)
     158             : {
     159             :         struct eap_method *eap;
     160             :         int ret;
     161             : 
     162          74 :         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
     163             :                                       EAP_VENDOR_IETF, EAP_TYPE_IDENTITY,
     164             :                                       "Identity");
     165          74 :         if (eap == NULL)
     166           0 :                 return -1;
     167             : 
     168          74 :         eap->init = eap_identity_init;
     169          74 :         eap->initPickUp = eap_identity_initPickUp;
     170          74 :         eap->reset = eap_identity_reset;
     171          74 :         eap->buildReq = eap_identity_buildReq;
     172          74 :         eap->check = eap_identity_check;
     173          74 :         eap->process = eap_identity_process;
     174          74 :         eap->isDone = eap_identity_isDone;
     175          74 :         eap->isSuccess = eap_identity_isSuccess;
     176             : 
     177          74 :         ret = eap_server_method_register(eap);
     178          74 :         if (ret)
     179           0 :                 eap_server_method_free(eap);
     180          74 :         return ret;
     181             : }

Generated by: LCOV version 1.10