eap_vendor_test.c
Go to the documentation of this file.00001
00016 #include "includes.h"
00017
00018 #include "common.h"
00019 #include "eap_i.h"
00020
00021
00022 #define EAP_VENDOR_ID 0xfffefd
00023 #define EAP_VENDOR_TYPE 0xfcfbfaf9
00024
00025
00026 struct eap_vendor_test_data {
00027 enum { INIT, CONFIRM, SUCCESS, FAILURE } state;
00028 };
00029
00030
00031 static const char * eap_vendor_test_state_txt(int state)
00032 {
00033 switch (state) {
00034 case INIT:
00035 return "INIT";
00036 case CONFIRM:
00037 return "CONFIRM";
00038 case SUCCESS:
00039 return "SUCCESS";
00040 case FAILURE:
00041 return "FAILURE";
00042 default:
00043 return "?";
00044 }
00045 }
00046
00047
00048 static void eap_vendor_test_state(struct eap_vendor_test_data *data,
00049 int state)
00050 {
00051 wpa_printf(MSG_DEBUG, "EAP-VENDOR-TEST: %s -> %s",
00052 eap_vendor_test_state_txt(data->state),
00053 eap_vendor_test_state_txt(state));
00054 data->state = state;
00055 }
00056
00057
00058 static void * eap_vendor_test_init(struct eap_sm *sm)
00059 {
00060 struct eap_vendor_test_data *data;
00061
00062 data = os_zalloc(sizeof(*data));
00063 if (data == NULL)
00064 return NULL;
00065 data->state = INIT;
00066
00067 return data;
00068 }
00069
00070
00071 static void eap_vendor_test_reset(struct eap_sm *sm, void *priv)
00072 {
00073 struct eap_vendor_test_data *data = priv;
00074 os_free(data);
00075 }
00076
00077
00078 static struct wpabuf * eap_vendor_test_buildReq(struct eap_sm *sm, void *priv,
00079 u8 id)
00080 {
00081 struct eap_vendor_test_data *data = priv;
00082 struct wpabuf *req;
00083
00084 req = eap_msg_alloc(EAP_VENDOR_ID, EAP_VENDOR_TYPE, 1,
00085 EAP_CODE_REQUEST, id);
00086 if (req == NULL) {
00087 wpa_printf(MSG_ERROR, "EAP-VENDOR-TEST: Failed to allocate "
00088 "memory for request");
00089 return NULL;
00090 }
00091
00092 wpabuf_put_u8(req, data->state == INIT ? 1 : 3);
00093
00094 return req;
00095 }
00096
00097
00098 static Boolean eap_vendor_test_check(struct eap_sm *sm, void *priv,
00099 struct wpabuf *respData)
00100 {
00101 const u8 *pos;
00102 size_t len;
00103
00104 pos = eap_hdr_validate(EAP_VENDOR_ID, EAP_VENDOR_TYPE, respData, &len);
00105 if (pos == NULL || len < 1) {
00106 wpa_printf(MSG_INFO, "EAP-VENDOR-TEST: Invalid frame");
00107 return TRUE;
00108 }
00109
00110 return FALSE;
00111 }
00112
00113
00114 static void eap_vendor_test_process(struct eap_sm *sm, void *priv,
00115 struct wpabuf *respData)
00116 {
00117 struct eap_vendor_test_data *data = priv;
00118 const u8 *pos;
00119 size_t len;
00120
00121 pos = eap_hdr_validate(EAP_VENDOR_ID, EAP_VENDOR_TYPE, respData, &len);
00122 if (pos == NULL || len < 1)
00123 return;
00124
00125 if (data->state == INIT) {
00126 if (*pos == 2)
00127 eap_vendor_test_state(data, CONFIRM);
00128 else
00129 eap_vendor_test_state(data, FAILURE);
00130 } else if (data->state == CONFIRM) {
00131 if (*pos == 4)
00132 eap_vendor_test_state(data, SUCCESS);
00133 else
00134 eap_vendor_test_state(data, FAILURE);
00135 } else
00136 eap_vendor_test_state(data, FAILURE);
00137 }
00138
00139
00140 static Boolean eap_vendor_test_isDone(struct eap_sm *sm, void *priv)
00141 {
00142 struct eap_vendor_test_data *data = priv;
00143 return data->state == SUCCESS;
00144 }
00145
00146
00147 static u8 * eap_vendor_test_getKey(struct eap_sm *sm, void *priv, size_t *len)
00148 {
00149 struct eap_vendor_test_data *data = priv;
00150 u8 *key;
00151 const int key_len = 64;
00152
00153 if (data->state != SUCCESS)
00154 return NULL;
00155
00156 key = os_malloc(key_len);
00157 if (key == NULL)
00158 return NULL;
00159
00160 os_memset(key, 0x11, key_len / 2);
00161 os_memset(key + key_len / 2, 0x22, key_len / 2);
00162 *len = key_len;
00163
00164 return key;
00165 }
00166
00167
00168 static Boolean eap_vendor_test_isSuccess(struct eap_sm *sm, void *priv)
00169 {
00170 struct eap_vendor_test_data *data = priv;
00171 return data->state == SUCCESS;
00172 }
00173
00174
00175 int eap_server_vendor_test_register(void)
00176 {
00177 struct eap_method *eap;
00178 int ret;
00179
00180 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
00181 EAP_VENDOR_ID, EAP_VENDOR_TYPE,
00182 "VENDOR-TEST");
00183 if (eap == NULL)
00184 return -1;
00185
00186 eap->init = eap_vendor_test_init;
00187 eap->reset = eap_vendor_test_reset;
00188 eap->buildReq = eap_vendor_test_buildReq;
00189 eap->check = eap_vendor_test_check;
00190 eap->process = eap_vendor_test_process;
00191 eap->isDone = eap_vendor_test_isDone;
00192 eap->getKey = eap_vendor_test_getKey;
00193 eap->isSuccess = eap_vendor_test_isSuccess;
00194
00195 ret = eap_server_method_register(eap);
00196 if (ret)
00197 eap_server_method_free(eap);
00198 return ret;
00199 }
00200