]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/hostapd/eap_md5.c
This commit was generated by cvs2svn to compensate for changes in r151513,
[FreeBSD/FreeBSD.git] / contrib / hostapd / eap_md5.c
1 /*
2  * hostapd / EAP-MD5 server
3  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <netinet/in.h>
19
20 #include "hostapd.h"
21 #include "common.h"
22 #include "eap_i.h"
23 #include "md5.h"
24
25
26 #define CHALLENGE_LEN 16
27
28 struct eap_md5_data {
29         u8 challenge[CHALLENGE_LEN];
30         enum { CONTINUE, SUCCESS, FAILURE } state;
31 };
32
33
34 static void * eap_md5_init(struct eap_sm *sm)
35 {
36         struct eap_md5_data *data;
37
38         data = malloc(sizeof(*data));
39         if (data == NULL)
40                 return data;
41         memset(data, 0, sizeof(*data));
42         data->state = CONTINUE;
43
44         return data;
45 }
46
47
48 static void eap_md5_reset(struct eap_sm *sm, void *priv)
49 {
50         struct eap_md5_data *data = priv;
51         free(data);
52 }
53
54
55 static u8 * eap_md5_buildReq(struct eap_sm *sm, void *priv, int id,
56                              size_t *reqDataLen)
57 {
58         struct eap_md5_data *data = priv;
59         struct eap_hdr *req;
60         u8 *pos;
61
62         if (hostapd_get_rand(data->challenge, CHALLENGE_LEN)) {
63                 wpa_printf(MSG_ERROR, "EAP-MD5: Failed to get random data");
64                 data->state = FAILURE;
65                 return NULL;
66         }
67
68         *reqDataLen = sizeof(*req) + 2 + CHALLENGE_LEN;
69         req = malloc(*reqDataLen);
70         if (req == NULL) {
71                 wpa_printf(MSG_ERROR, "EAP-MD5: Failed to allocate memory for "
72                            "request");
73                 data->state = FAILURE;
74                 return NULL;
75         }
76
77         req->code = EAP_CODE_REQUEST;
78         req->identifier = id;
79         req->length = htons(*reqDataLen);
80         pos = (u8 *) (req + 1);
81         *pos++ = EAP_TYPE_MD5;
82         *pos++ = CHALLENGE_LEN;
83         memcpy(pos, data->challenge, CHALLENGE_LEN);
84         wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Challenge", pos, CHALLENGE_LEN);
85
86         data->state = CONTINUE;
87
88         return (u8 *) req;
89 }
90
91
92 static Boolean eap_md5_check(struct eap_sm *sm, void *priv,
93                              u8 *respData, size_t respDataLen)
94 {
95         struct eap_hdr *resp;
96         u8 *pos;
97         size_t len;
98
99         resp = (struct eap_hdr *) respData;
100         pos = (u8 *) (resp + 1);
101         if (respDataLen < sizeof(*resp) + 2 || *pos != EAP_TYPE_MD5 ||
102             (len = ntohs(resp->length)) > respDataLen) {
103                 wpa_printf(MSG_INFO, "EAP-MD5: Invalid frame");
104                 return TRUE;
105         }
106         pos++;
107         if (*pos != MD5_MAC_LEN ||
108             sizeof(*resp) + 2 + MD5_MAC_LEN > len) {
109                 wpa_printf(MSG_INFO, "EAP-MD5: Invalid response "
110                            "(response_len=%d respDataLen=%lu",
111                            *pos, (unsigned long) respDataLen);
112                 return TRUE;
113         }
114
115         return FALSE;
116 }
117
118
119 static void eap_md5_process(struct eap_sm *sm, void *priv,
120                             u8 *respData, size_t respDataLen)
121 {
122         struct eap_md5_data *data = priv;
123         struct eap_hdr *resp;
124         u8 *pos;
125         MD5_CTX context;
126         u8 hash[MD5_MAC_LEN];
127
128         if (sm->user == NULL || sm->user->password == NULL) {
129                 wpa_printf(MSG_INFO, "EAP-MD5: Password not configured");
130                 data->state = FAILURE;
131                 return;
132         }
133
134         resp = (struct eap_hdr *) respData;
135         pos = (u8 *) (resp + 1);
136         pos += 2; /* Skip type and len */
137         wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Response", pos, MD5_MAC_LEN);
138
139         MD5Init(&context);
140         MD5Update(&context, &resp->identifier, 1);
141         MD5Update(&context, sm->user->password, sm->user->password_len);
142         MD5Update(&context, data->challenge, CHALLENGE_LEN);
143         MD5Final(hash, &context);
144
145         if (memcmp(hash, pos, MD5_MAC_LEN) == 0) {
146                 wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Success");
147                 data->state = SUCCESS;
148         } else {
149                 wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Failure");
150                 data->state = FAILURE;
151         }
152 }
153
154
155 static Boolean eap_md5_isDone(struct eap_sm *sm, void *priv)
156 {
157         struct eap_md5_data *data = priv;
158         return data->state != CONTINUE;
159 }
160
161
162 static Boolean eap_md5_isSuccess(struct eap_sm *sm, void *priv)
163 {
164         struct eap_md5_data *data = priv;
165         return data->state == SUCCESS;
166 }
167
168
169 const struct eap_method eap_method_md5 =
170 {
171         .method = EAP_TYPE_MD5,
172         .name = "MD5",
173         .init = eap_md5_init,
174         .reset = eap_md5_reset,
175         .buildReq = eap_md5_buildReq,
176         .check = eap_md5_check,
177         .process = eap_md5_process,
178         .isDone = eap_md5_isDone,
179         .isSuccess = eap_md5_isSuccess,
180 };