Merge from vendor branch LESS:
[dragonfly.git] / contrib / hostapd-0.4.9 / 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 #include "crypto.h"
25
26
27 #define CHALLENGE_LEN 16
28
29 struct eap_md5_data {
30         u8 challenge[CHALLENGE_LEN];
31         enum { CONTINUE, SUCCESS, FAILURE } state;
32 };
33
34
35 static void * eap_md5_init(struct eap_sm *sm)
36 {
37         struct eap_md5_data *data;
38
39         data = malloc(sizeof(*data));
40         if (data == NULL)
41                 return data;
42         memset(data, 0, sizeof(*data));
43         data->state = CONTINUE;
44
45         return data;
46 }
47
48
49 static void eap_md5_reset(struct eap_sm *sm, void *priv)
50 {
51         struct eap_md5_data *data = priv;
52         free(data);
53 }
54
55
56 static u8 * eap_md5_buildReq(struct eap_sm *sm, void *priv, int id,
57                              size_t *reqDataLen)
58 {
59         struct eap_md5_data *data = priv;
60         struct eap_hdr *req;
61         u8 *pos;
62
63         if (hostapd_get_rand(data->challenge, CHALLENGE_LEN)) {
64                 wpa_printf(MSG_ERROR, "EAP-MD5: Failed to get random data");
65                 data->state = FAILURE;
66                 return NULL;
67         }
68
69         *reqDataLen = sizeof(*req) + 2 + CHALLENGE_LEN;
70         req = malloc(*reqDataLen);
71         if (req == NULL) {
72                 wpa_printf(MSG_ERROR, "EAP-MD5: Failed to allocate memory for "
73                            "request");
74                 data->state = FAILURE;
75                 return NULL;
76         }
77
78         req->code = EAP_CODE_REQUEST;
79         req->identifier = id;
80         req->length = htons(*reqDataLen);
81         pos = (u8 *) (req + 1);
82         *pos++ = EAP_TYPE_MD5;
83         *pos++ = CHALLENGE_LEN;
84         memcpy(pos, data->challenge, CHALLENGE_LEN);
85         wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Challenge", pos, CHALLENGE_LEN);
86
87         data->state = CONTINUE;
88
89         return (u8 *) req;
90 }
91
92
93 static Boolean eap_md5_check(struct eap_sm *sm, void *priv,
94                              u8 *respData, size_t respDataLen)
95 {
96         struct eap_hdr *resp;
97         u8 *pos;
98         size_t len;
99
100         resp = (struct eap_hdr *) respData;
101         pos = (u8 *) (resp + 1);
102         if (respDataLen < sizeof(*resp) + 2 || *pos != EAP_TYPE_MD5 ||
103             (len = ntohs(resp->length)) > respDataLen) {
104                 wpa_printf(MSG_INFO, "EAP-MD5: Invalid frame");
105                 return TRUE;
106         }
107         pos++;
108         if (*pos != MD5_MAC_LEN ||
109             sizeof(*resp) + 2 + MD5_MAC_LEN > len) {
110                 wpa_printf(MSG_INFO, "EAP-MD5: Invalid response "
111                            "(response_len=%d respDataLen=%lu",
112                            *pos, (unsigned long) respDataLen);
113                 return TRUE;
114         }
115
116         return FALSE;
117 }
118
119
120 static void eap_md5_process(struct eap_sm *sm, void *priv,
121                             u8 *respData, size_t respDataLen)
122 {
123         struct eap_md5_data *data = priv;
124         struct eap_hdr *resp;
125         u8 *pos;
126         const u8 *addr[3];
127         size_t len[3];
128         u8 hash[MD5_MAC_LEN];
129
130         if (sm->user == NULL || sm->user->password == NULL) {
131                 wpa_printf(MSG_INFO, "EAP-MD5: Password not configured");
132                 data->state = FAILURE;
133                 return;
134         }
135
136         resp = (struct eap_hdr *) respData;
137         pos = (u8 *) (resp + 1);
138         pos += 2; /* Skip type and len */
139         wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Response", pos, MD5_MAC_LEN);
140
141         addr[0] = &resp->identifier;
142         len[0] = 1;
143         addr[1] = sm->user->password;
144         len[1] = sm->user->password_len;
145         addr[2] = data->challenge;
146         len[2] = CHALLENGE_LEN;
147         md5_vector(3, addr, len, hash);
148
149         if (memcmp(hash, pos, MD5_MAC_LEN) == 0) {
150                 wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Success");
151                 data->state = SUCCESS;
152         } else {
153                 wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Failure");
154                 data->state = FAILURE;
155         }
156 }
157
158
159 static Boolean eap_md5_isDone(struct eap_sm *sm, void *priv)
160 {
161         struct eap_md5_data *data = priv;
162         return data->state != CONTINUE;
163 }
164
165
166 static Boolean eap_md5_isSuccess(struct eap_sm *sm, void *priv)
167 {
168         struct eap_md5_data *data = priv;
169         return data->state == SUCCESS;
170 }
171
172
173 const struct eap_method eap_method_md5 =
174 {
175         .method = EAP_TYPE_MD5,
176         .name = "MD5",
177         .init = eap_md5_init,
178         .reset = eap_md5_reset,
179         .buildReq = eap_md5_buildReq,
180         .check = eap_md5_check,
181         .process = eap_md5_process,
182         .isDone = eap_md5_isDone,
183         .isSuccess = eap_md5_isSuccess,
184 };