00001
00016 #include "includes.h"
00017
00018 #include "common.h"
00019 #include "eap_i.h"
00020 #include "eap_common/chap.h"
00021
00022
00023 #define CHALLENGE_LEN 16
00024
00025 struct eap_md5_data {
00026 u8 challenge[CHALLENGE_LEN];
00027 enum { CONTINUE, SUCCESS, FAILURE } state;
00028 };
00029
00030
00031 static void * eap_md5_init(struct eap_sm *sm)
00032 {
00033 struct eap_md5_data *data;
00034
00035 data = os_zalloc(sizeof(*data));
00036 if (data == NULL)
00037 return NULL;
00038 data->state = CONTINUE;
00039
00040 return data;
00041 }
00042
00043
00044 static void eap_md5_reset(struct eap_sm *sm, void *priv)
00045 {
00046 struct eap_md5_data *data = priv;
00047 os_free(data);
00048 }
00049
00050
00051 static struct wpabuf * eap_md5_buildReq(struct eap_sm *sm, void *priv, u8 id)
00052 {
00053 struct eap_md5_data *data = priv;
00054 struct wpabuf *req;
00055
00056 if (os_get_random(data->challenge, CHALLENGE_LEN)) {
00057 wpa_printf(MSG_ERROR, "EAP-MD5: Failed to get random data");
00058 data->state = FAILURE;
00059 return NULL;
00060 }
00061
00062 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MD5, 1 + CHALLENGE_LEN,
00063 EAP_CODE_REQUEST, id);
00064 if (req == NULL) {
00065 wpa_printf(MSG_ERROR, "EAP-MD5: Failed to allocate memory for "
00066 "request");
00067 data->state = FAILURE;
00068 return NULL;
00069 }
00070
00071 wpabuf_put_u8(req, CHALLENGE_LEN);
00072 wpabuf_put_data(req, data->challenge, CHALLENGE_LEN);
00073 wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Challenge", data->challenge,
00074 CHALLENGE_LEN);
00075
00076 data->state = CONTINUE;
00077
00078 return req;
00079 }
00080
00081
00082 static Boolean eap_md5_check(struct eap_sm *sm, void *priv,
00083 struct wpabuf *respData)
00084 {
00085 const u8 *pos;
00086 size_t len;
00087
00088 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MD5, respData, &len);
00089 if (pos == NULL || len < 1) {
00090 wpa_printf(MSG_INFO, "EAP-MD5: Invalid frame");
00091 return TRUE;
00092 }
00093 if (*pos != CHAP_MD5_LEN || 1 + CHAP_MD5_LEN > len) {
00094 wpa_printf(MSG_INFO, "EAP-MD5: Invalid response "
00095 "(response_len=%d payload_len=%lu",
00096 *pos, (unsigned long) len);
00097 return TRUE;
00098 }
00099
00100 return FALSE;
00101 }
00102
00103
00104 static void eap_md5_process(struct eap_sm *sm, void *priv,
00105 struct wpabuf *respData)
00106 {
00107 struct eap_md5_data *data = priv;
00108 const u8 *pos;
00109 size_t plen;
00110 u8 hash[CHAP_MD5_LEN], id;
00111
00112 if (sm->user == NULL || sm->user->password == NULL ||
00113 sm->user->password_hash) {
00114 wpa_printf(MSG_INFO, "EAP-MD5: Plaintext password not "
00115 "configured");
00116 data->state = FAILURE;
00117 return;
00118 }
00119
00120 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MD5, respData, &plen);
00121 if (pos == NULL || *pos != CHAP_MD5_LEN || plen < 1 + CHAP_MD5_LEN)
00122 return;
00123
00124 pos++;
00125 wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Response", pos, CHAP_MD5_LEN);
00126
00127 id = eap_get_id(respData);
00128 chap_md5(id, sm->user->password, sm->user->password_len,
00129 data->challenge, CHALLENGE_LEN, hash);
00130
00131 if (os_memcmp(hash, pos, CHAP_MD5_LEN) == 0) {
00132 wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Success");
00133 data->state = SUCCESS;
00134 } else {
00135 wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Failure");
00136 data->state = FAILURE;
00137 }
00138 }
00139
00140
00141 static Boolean eap_md5_isDone(struct eap_sm *sm, void *priv)
00142 {
00143 struct eap_md5_data *data = priv;
00144 return data->state != CONTINUE;
00145 }
00146
00147
00148 static Boolean eap_md5_isSuccess(struct eap_sm *sm, void *priv)
00149 {
00150 struct eap_md5_data *data = priv;
00151 return data->state == SUCCESS;
00152 }
00153
00154
00155 int eap_server_md5_register(void)
00156 {
00157 struct eap_method *eap;
00158 int ret;
00159
00160 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
00161 EAP_VENDOR_IETF, EAP_TYPE_MD5, "MD5");
00162 if (eap == NULL)
00163 return -1;
00164
00165 eap->init = eap_md5_init;
00166 eap->reset = eap_md5_reset;
00167 eap->buildReq = eap_md5_buildReq;
00168 eap->check = eap_md5_check;
00169 eap->process = eap_md5_process;
00170 eap->isDone = eap_md5_isDone;
00171 eap->isSuccess = eap_md5_isSuccess;
00172
00173 ret = eap_server_method_register(eap);
00174 if (ret)
00175 eap_server_method_free(eap);
00176 return ret;
00177 }
00178