2 * hostapd / EAP-MD5 server
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "crypto/random.h"
20 #include "eap_common/chap.h"
23 #define CHALLENGE_LEN 16
26 u8 challenge[CHALLENGE_LEN];
27 enum { CONTINUE, SUCCESS, FAILURE } state;
31 static void * eap_md5_init(struct eap_sm *sm)
33 struct eap_md5_data *data;
35 data = os_zalloc(sizeof(*data));
38 data->state = CONTINUE;
44 static void eap_md5_reset(struct eap_sm *sm, void *priv)
46 struct eap_md5_data *data = priv;
51 static struct wpabuf * eap_md5_buildReq(struct eap_sm *sm, void *priv, u8 id)
53 struct eap_md5_data *data = priv;
56 if (random_get_bytes(data->challenge, CHALLENGE_LEN)) {
57 wpa_printf(MSG_ERROR, "EAP-MD5: Failed to get random data");
58 data->state = FAILURE;
62 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MD5, 1 + CHALLENGE_LEN,
63 EAP_CODE_REQUEST, id);
65 wpa_printf(MSG_ERROR, "EAP-MD5: Failed to allocate memory for "
67 data->state = FAILURE;
71 wpabuf_put_u8(req, CHALLENGE_LEN);
72 wpabuf_put_data(req, data->challenge, CHALLENGE_LEN);
73 wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Challenge", data->challenge,
76 data->state = CONTINUE;
82 static Boolean eap_md5_check(struct eap_sm *sm, void *priv,
83 struct wpabuf *respData)
88 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MD5, respData, &len);
89 if (pos == NULL || len < 1) {
90 wpa_printf(MSG_INFO, "EAP-MD5: Invalid frame");
93 if (*pos != CHAP_MD5_LEN || 1 + CHAP_MD5_LEN > len) {
94 wpa_printf(MSG_INFO, "EAP-MD5: Invalid response "
95 "(response_len=%d payload_len=%lu",
96 *pos, (unsigned long) len);
104 static void eap_md5_process(struct eap_sm *sm, void *priv,
105 struct wpabuf *respData)
107 struct eap_md5_data *data = priv;
110 u8 hash[CHAP_MD5_LEN], id;
112 if (sm->user == NULL || sm->user->password == NULL ||
113 sm->user->password_hash) {
114 wpa_printf(MSG_INFO, "EAP-MD5: Plaintext password not "
116 data->state = FAILURE;
120 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MD5, respData, &plen);
121 if (pos == NULL || *pos != CHAP_MD5_LEN || plen < 1 + CHAP_MD5_LEN)
122 return; /* Should not happen - frame already validated */
124 pos++; /* Skip response len */
125 wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Response", pos, CHAP_MD5_LEN);
127 id = eap_get_id(respData);
128 chap_md5(id, sm->user->password, sm->user->password_len,
129 data->challenge, CHALLENGE_LEN, hash);
131 if (os_memcmp(hash, pos, CHAP_MD5_LEN) == 0) {
132 wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Success");
133 data->state = SUCCESS;
135 wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Failure");
136 data->state = FAILURE;
141 static Boolean eap_md5_isDone(struct eap_sm *sm, void *priv)
143 struct eap_md5_data *data = priv;
144 return data->state != CONTINUE;
148 static Boolean eap_md5_isSuccess(struct eap_sm *sm, void *priv)
150 struct eap_md5_data *data = priv;
151 return data->state == SUCCESS;
155 int eap_server_md5_register(void)
157 struct eap_method *eap;
160 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
161 EAP_VENDOR_IETF, EAP_TYPE_MD5, "MD5");
165 eap->init = eap_md5_init;
166 eap->reset = eap_md5_reset;
167 eap->buildReq = eap_md5_buildReq;
168 eap->check = eap_md5_check;
169 eap->process = eap_md5_process;
170 eap->isDone = eap_md5_isDone;
171 eap->isSuccess = eap_md5_isSuccess;
173 ret = eap_server_method_register(eap);
175 eap_server_method_free(eap);