import from HEAD:
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_md5 / eap_md5.c
1 /*
2  * eap_md5.c  EAP MD5 functionality.
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Copyright 2000,2001  The FreeRADIUS server project
21  * Copyright 2001  hereUare Communications, Inc. <raghud@hereuare.com>
22  */
23
24 /*
25  *
26  *  MD5 Packet Format in EAP Type-Data
27  *  --- ------ ------ -- --- ---------
28  *  0                   1                   2                   3
29  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
30  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31  * |  Value-Size   |  Value ...
32  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33  * |  Name ...
34  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35  *
36  */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "eap.h"
41
42 #include "eap_md5.h"
43
44 /*
45  *      Allocate a new MD5_PACKET
46  */
47 MD5_PACKET *eapmd5_alloc(void)
48 {
49         MD5_PACKET   *rp;
50
51         if ((rp = malloc(sizeof(MD5_PACKET))) == NULL) {
52                 radlog(L_ERR, "rlm_eap_md5: out of memory");
53                 return NULL;
54         }
55         memset(rp, 0, sizeof(MD5_PACKET));
56         return rp;
57 }
58
59 /*
60  *      Free MD5_PACKET
61  */
62 void eapmd5_free(MD5_PACKET **md5_packet_ptr)
63 {
64         MD5_PACKET *md5_packet;
65
66         if (!md5_packet_ptr) return;
67         md5_packet = *md5_packet_ptr;
68         if (md5_packet == NULL) return;
69
70         if (md5_packet->value) free(md5_packet->value);
71         if (md5_packet->name) free(md5_packet->name);
72
73         free(md5_packet);
74
75         *md5_packet_ptr = NULL;
76 }
77
78 /*
79  *      We expect only RESPONSE for which SUCCESS or FAILURE is sent back
80  */
81 MD5_PACKET *eapmd5_extract(EAP_DS *eap_ds)
82 {
83         md5_packet_t    *data;
84         MD5_PACKET      *packet;
85         unsigned short  name_len;
86
87         /*
88          *      We need a response, of type EAP-MD5, with at least
89          *      one byte of type data (EAP-MD5) following the 4-byte
90          *      EAP-Packet header.
91          */
92         if (!eap_ds                                      ||
93             !eap_ds->response                            ||
94             (eap_ds->response->code != PW_MD5_RESPONSE)  ||
95             eap_ds->response->type.type != PW_EAP_MD5    ||
96             !eap_ds->response->type.data                 ||
97             (eap_ds->response->length <= MD5_HEADER_LEN) ||
98             (eap_ds->response->type.data[0] <= 0)) {
99                 radlog(L_ERR, "rlm_eap_md5: corrupted data");
100                 return NULL;
101         }
102
103         packet = eapmd5_alloc();
104         if (!packet) return NULL;
105
106         /*
107          *      Code & id for MD5 & EAP are same
108          *
109          *      but md5_length = length of the EAP-MD5 data, which
110          *      doesn't include the EAP header, or the octet saying
111          *      EAP-MD5.
112          */
113         packet->code = eap_ds->response->code;
114         packet->id = eap_ds->response->id;
115         packet->length = eap_ds->response->length - (MD5_HEADER_LEN + 1);
116
117         /*
118          *      Sanity check the EAP-MD5 packet sent to us
119          *      by the client.
120          */
121         data = (md5_packet_t *)eap_ds->response->type.data;
122
123         /*
124          *      Already checked the size above.
125          */
126         packet->value_size = data->value_size;
127
128         /*
129          *      Allocate room for the data, and copy over the data.
130          */
131         packet->value = malloc(packet->value_size);
132         if (packet->value == NULL) {
133                 radlog(L_ERR, "rlm_eap_md5: out of memory");
134                 eapmd5_free(&packet);
135                 return NULL;
136         }
137         memcpy(packet->value, data->value_name, packet->value_size);
138
139         /*
140          *      Name is optional and is present after Value, but we
141          *      need to check for it, as eapmd5_compose()
142          */
143         name_len =  packet->length - (packet->value_size + 1);
144         if (name_len) {
145                 packet->name = malloc(name_len + 1);
146                 if (!packet->name) {
147                         radlog(L_ERR, "rlm_eap_md5: out of memory");
148                         eapmd5_free(&packet);
149                         return NULL;
150                 }
151                 memcpy(packet->name, data->value_name + packet->value_size,
152                        name_len);
153                 packet->name[name_len] = 0;
154         }
155
156         return packet;
157 }
158
159
160 /*
161  * verify = MD5(id+password+challenge_sent)
162  */
163 int eapmd5_verify(MD5_PACKET *packet, VALUE_PAIR* password,
164                   uint8_t *challenge)
165 {
166         char    *ptr;
167         char    string[1 + MAX_STRING_LEN*2];
168         unsigned char output[MAX_STRING_LEN];
169         unsigned short len;
170
171         /*
172          *      Sanity check it.
173          */
174         if (packet->value_size != 16) {
175                 radlog(L_ERR, "rlm_eap_md5: Expected 16 bytes of response to challenge, got %d", packet->value_size);
176                 return 0;
177         }
178
179         len = 0;
180         ptr = string;
181
182         /*
183          *      This is really rad_chap_pwencode()...
184          */
185         *ptr++ = packet->id;
186         len++;
187         memcpy(ptr, password->strvalue, password->length);
188         ptr += password->length;
189         len += password->length;
190
191         /*
192          *      The challenge size is hard-coded.
193          */
194         memcpy(ptr, challenge, MD5_CHALLENGE_LEN);
195         len += MD5_CHALLENGE_LEN;
196
197         librad_md5_calc((u_char *)output, (u_char *)string, len);
198
199         /*
200          *      The length of the response is always 16 for MD5.
201          */
202         if (memcmp(output, packet->value, 16) != 0) {
203                 return 0;
204         }
205         return 1;
206 }
207
208 /*
209  *      Compose the portions of the reply packet specific to the
210  *      EAP-MD5 protocol, in the EAP reply typedata
211  */
212 int eapmd5_compose(EAP_DS *eap_ds, MD5_PACKET *reply)
213 {
214         uint8_t *ptr;
215         unsigned short name_len;
216
217         /*
218          *      We really only send Challenge (EAP-Identity),
219          *      and EAP-Success, and EAP-Failure.
220          */
221         if (reply->code < 3) {
222                 eap_ds->request->type.type = PW_EAP_MD5;
223
224                 rad_assert(reply->length > 0);
225                 rad_assert(reply->value_size < 256);
226
227                 eap_ds->request->type.data = malloc(reply->length);
228                 if (eap_ds->request->type.data == NULL) {
229                         radlog(L_ERR, "rlm_eap_md5: out of memory");
230                         return 0;
231                 }
232                 ptr = eap_ds->request->type.data;
233                 *ptr++ = (uint8_t)(reply->value_size & 0xFF);
234                 memcpy(ptr, reply->value, reply->value_size);
235
236                 /* Just the Challenge length */
237                 eap_ds->request->type.length = reply->value_size + 1;
238
239                 /*
240                  *      Return the name, if necessary.
241                  *
242                  *      Don't see why this is *ever* necessary...
243                  */
244                 name_len = reply->length - (reply->value_size + 1);
245                 if (name_len && reply->name) {
246                         ptr += reply->value_size;
247                         memcpy(ptr, reply->name, name_len);
248                         /* Challenge length + Name length */
249                         eap_ds->request->type.length += name_len;
250                 }
251         } else {
252                 eap_ds->request->type.length = 0;
253                 /* TODO: In future we might add message here wrt rfc1994 */
254         }
255         eap_ds->request->code = reply->code;
256
257         eapmd5_free(&reply);
258
259         return 1;
260 }