LCOV - code coverage report
Current view: top level - src/eap_server - eap_server_vendor_test.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1422976643 Lines: 70 84 83.3 %
Date: 2015-02-03 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /*
       2             :  * hostapd / Test method for vendor specific (expanded) EAP type
       3             :  * Copyright (c) 2005-2007, 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             : #define EAP_VENDOR_ID EAP_VENDOR_HOSTAP
      16             : #define EAP_VENDOR_TYPE 0xfcfbfaf9
      17             : 
      18             : 
      19             : struct eap_vendor_test_data {
      20             :         enum { INIT, CONFIRM, SUCCESS, FAILURE } state;
      21             : };
      22             : 
      23             : 
      24          12 : static const char * eap_vendor_test_state_txt(int state)
      25             : {
      26          12 :         switch (state) {
      27             :         case INIT:
      28           3 :                 return "INIT";
      29             :         case CONFIRM:
      30           6 :                 return "CONFIRM";
      31             :         case SUCCESS:
      32           3 :                 return "SUCCESS";
      33             :         case FAILURE:
      34           0 :                 return "FAILURE";
      35             :         default:
      36           0 :                 return "?";
      37             :         }
      38             : }
      39             : 
      40             : 
      41           6 : static void eap_vendor_test_state(struct eap_vendor_test_data *data,
      42             :                                   int state)
      43             : {
      44          12 :         wpa_printf(MSG_DEBUG, "EAP-VENDOR-TEST: %s -> %s",
      45           6 :                    eap_vendor_test_state_txt(data->state),
      46             :                    eap_vendor_test_state_txt(state));
      47           6 :         data->state = state;
      48           6 : }
      49             : 
      50             : 
      51           4 : static void * eap_vendor_test_init(struct eap_sm *sm)
      52             : {
      53             :         struct eap_vendor_test_data *data;
      54             : 
      55           4 :         data = os_zalloc(sizeof(*data));
      56           4 :         if (data == NULL)
      57           0 :                 return NULL;
      58           4 :         data->state = INIT;
      59             : 
      60           4 :         return data;
      61             : }
      62             : 
      63             : 
      64           4 : static void eap_vendor_test_reset(struct eap_sm *sm, void *priv)
      65             : {
      66           4 :         struct eap_vendor_test_data *data = priv;
      67           4 :         os_free(data);
      68           4 : }
      69             : 
      70             : 
      71           7 : static struct wpabuf * eap_vendor_test_buildReq(struct eap_sm *sm, void *priv,
      72             :                                                 u8 id)
      73             : {
      74           7 :         struct eap_vendor_test_data *data = priv;
      75             :         struct wpabuf *req;
      76             : 
      77           7 :         req = eap_msg_alloc(EAP_VENDOR_ID, EAP_VENDOR_TYPE, 1,
      78             :                             EAP_CODE_REQUEST, id);
      79           7 :         if (req == NULL) {
      80           0 :                 wpa_printf(MSG_ERROR, "EAP-VENDOR-TEST: Failed to allocate "
      81             :                            "memory for request");
      82           0 :                 return NULL;
      83             :         }
      84             : 
      85           7 :         wpabuf_put_u8(req, data->state == INIT ? 1 : 3);
      86             : 
      87           7 :         return req;
      88             : }
      89             : 
      90             : 
      91           6 : static Boolean eap_vendor_test_check(struct eap_sm *sm, void *priv,
      92             :                                      struct wpabuf *respData)
      93             : {
      94             :         const u8 *pos;
      95             :         size_t len;
      96             : 
      97           6 :         pos = eap_hdr_validate(EAP_VENDOR_ID, EAP_VENDOR_TYPE, respData, &len);
      98           6 :         if (pos == NULL || len < 1) {
      99           0 :                 wpa_printf(MSG_INFO, "EAP-VENDOR-TEST: Invalid frame");
     100           0 :                 return TRUE;
     101             :         }
     102             : 
     103           6 :         return FALSE;
     104             : }
     105             : 
     106             : 
     107           6 : static void eap_vendor_test_process(struct eap_sm *sm, void *priv,
     108             :                                     struct wpabuf *respData)
     109             : {
     110           6 :         struct eap_vendor_test_data *data = priv;
     111             :         const u8 *pos;
     112             :         size_t len;
     113             : 
     114           6 :         pos = eap_hdr_validate(EAP_VENDOR_ID, EAP_VENDOR_TYPE, respData, &len);
     115           6 :         if (pos == NULL || len < 1)
     116           6 :                 return;
     117             : 
     118           6 :         if (data->state == INIT) {
     119           3 :                 if (*pos == 2)
     120           3 :                         eap_vendor_test_state(data, CONFIRM);
     121             :                 else
     122           0 :                         eap_vendor_test_state(data, FAILURE);
     123           3 :         } else if (data->state == CONFIRM) {
     124           3 :                 if (*pos == 4)
     125           3 :                         eap_vendor_test_state(data, SUCCESS);
     126             :                 else
     127           0 :                         eap_vendor_test_state(data, FAILURE);
     128             :         } else
     129           0 :                 eap_vendor_test_state(data, FAILURE);
     130             : }
     131             : 
     132             : 
     133           6 : static Boolean eap_vendor_test_isDone(struct eap_sm *sm, void *priv)
     134             : {
     135           6 :         struct eap_vendor_test_data *data = priv;
     136           6 :         return data->state == SUCCESS;
     137             : }
     138             : 
     139             : 
     140           3 : static u8 * eap_vendor_test_getKey(struct eap_sm *sm, void *priv, size_t *len)
     141             : {
     142           3 :         struct eap_vendor_test_data *data = priv;
     143             :         u8 *key;
     144           3 :         const int key_len = 64;
     145             : 
     146           3 :         if (data->state != SUCCESS)
     147           0 :                 return NULL;
     148             : 
     149           3 :         key = os_malloc(key_len);
     150           3 :         if (key == NULL)
     151           0 :                 return NULL;
     152             : 
     153           3 :         os_memset(key, 0x11, key_len / 2);
     154           3 :         os_memset(key + key_len / 2, 0x22, key_len / 2);
     155           3 :         *len = key_len;
     156             : 
     157           3 :         return key;
     158             : }
     159             : 
     160             : 
     161           3 : static Boolean eap_vendor_test_isSuccess(struct eap_sm *sm, void *priv)
     162             : {
     163           3 :         struct eap_vendor_test_data *data = priv;
     164           3 :         return data->state == SUCCESS;
     165             : }
     166             : 
     167             : 
     168          10 : int eap_server_vendor_test_register(void)
     169             : {
     170             :         struct eap_method *eap;
     171             :         int ret;
     172             : 
     173          10 :         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
     174             :                                       EAP_VENDOR_ID, EAP_VENDOR_TYPE,
     175             :                                       "VENDOR-TEST");
     176          10 :         if (eap == NULL)
     177           0 :                 return -1;
     178             : 
     179          10 :         eap->init = eap_vendor_test_init;
     180          10 :         eap->reset = eap_vendor_test_reset;
     181          10 :         eap->buildReq = eap_vendor_test_buildReq;
     182          10 :         eap->check = eap_vendor_test_check;
     183          10 :         eap->process = eap_vendor_test_process;
     184          10 :         eap->isDone = eap_vendor_test_isDone;
     185          10 :         eap->getKey = eap_vendor_test_getKey;
     186          10 :         eap->isSuccess = eap_vendor_test_isSuccess;
     187             : 
     188          10 :         ret = eap_server_method_register(eap);
     189          10 :         if (ret)
     190           0 :                 eap_server_method_free(eap);
     191          10 :         return ret;
     192             : }

Generated by: LCOV version 1.10