Free memory if out of memory
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2000,2001,2006  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 <freeradius-devel/ident.h>
39 RCSID("$Id$")
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include "eap.h"
44
45 #include "eap_md5.h"
46
47 /*
48  *      Allocate a new MD5_PACKET
49  */
50 MD5_PACKET *eapmd5_alloc(void)
51 {
52         MD5_PACKET   *rp;
53
54         if ((rp = malloc(sizeof(MD5_PACKET))) == NULL) {
55                 radlog(L_ERR, "rlm_eap_md5: out of memory");
56                 return NULL;
57         }
58         memset(rp, 0, sizeof(MD5_PACKET));
59         return rp;
60 }
61
62 /*
63  *      Free MD5_PACKET
64  */
65 void eapmd5_free(MD5_PACKET **md5_packet_ptr)
66 {
67         MD5_PACKET *md5_packet;
68
69         if (!md5_packet_ptr) return;
70         md5_packet = *md5_packet_ptr;
71         if (md5_packet == NULL) return;
72
73         if (md5_packet->value) free(md5_packet->value);
74         if (md5_packet->name) free(md5_packet->name);
75
76         free(md5_packet);
77
78         *md5_packet_ptr = NULL;
79 }
80
81 /*
82  *      We expect only RESPONSE for which SUCCESS or FAILURE is sent back
83  */
84 MD5_PACKET *eapmd5_extract(EAP_DS *eap_ds)
85 {
86         md5_packet_t    *data;
87         MD5_PACKET      *packet;
88         unsigned short  name_len;
89
90         /*
91          *      We need a response, of type EAP-MD5, with at least
92          *      one byte of type data (EAP-MD5) following the 4-byte
93          *      EAP-Packet header.
94          */
95         if (!eap_ds                                      ||
96             !eap_ds->response                            ||
97             (eap_ds->response->code != PW_MD5_RESPONSE)  ||
98             eap_ds->response->type.type != PW_EAP_MD5    ||
99             !eap_ds->response->type.data                 ||
100             (eap_ds->response->length <= MD5_HEADER_LEN) ||
101             (eap_ds->response->type.data[0] <= 0)) {
102                 radlog(L_ERR, "rlm_eap_md5: corrupted data");
103                 return NULL;
104         }
105
106         packet = eapmd5_alloc();
107         if (!packet) return NULL;
108
109         /*
110          *      Code & id for MD5 & EAP are same
111          *
112          *      but md5_length = length of the EAP-MD5 data, which
113          *      doesn't include the EAP header, or the octet saying
114          *      EAP-MD5.
115          */
116         packet->code = eap_ds->response->code;
117         packet->id = eap_ds->response->id;
118         packet->length = eap_ds->response->length - (MD5_HEADER_LEN + 1);
119
120         /*
121          *      Sanity check the EAP-MD5 packet sent to us
122          *      by the client.
123          */
124         data = (md5_packet_t *)eap_ds->response->type.data;
125
126         /*
127          *      Already checked the size above.
128          */
129         packet->value_size = data->value_size;
130
131         /*
132          *      Allocate room for the data, and copy over the data.
133          */
134         packet->value = malloc(packet->value_size);
135         if (packet->value == NULL) {
136                 radlog(L_ERR, "rlm_eap_md5: out of memory");
137                 eapmd5_free(&packet);
138                 return NULL;
139         }
140         memcpy(packet->value, data->value_name, packet->value_size);
141
142         /*
143          *      Name is optional and is present after Value, but we
144          *      need to check for it, as eapmd5_compose()
145          */
146         name_len =  packet->length - (packet->value_size + 1);
147         if (name_len) {
148                 packet->name = malloc(name_len + 1);
149                 if (!packet->name) {
150                         radlog(L_ERR, "rlm_eap_md5: out of memory");
151                         eapmd5_free(&packet);
152                         return NULL;
153                 }
154                 memcpy(packet->name, data->value_name + packet->value_size,
155                        name_len);
156                 packet->name[name_len] = 0;
157         }
158
159         return packet;
160 }
161
162
163 /*
164  * verify = MD5(id+password+challenge_sent)
165  */
166 int eapmd5_verify(MD5_PACKET *packet, VALUE_PAIR* password,
167                   uint8_t *challenge)
168 {
169         char    *ptr;
170         char    string[1 + MAX_STRING_LEN*2];
171         unsigned char output[MAX_STRING_LEN];
172         unsigned short len;
173
174         /*
175          *      Sanity check it.
176          */
177         if (packet->value_size != 16) {
178                 radlog(L_ERR, "rlm_eap_md5: Expected 16 bytes of response to challenge, got %d", packet->value_size);
179                 return 0;
180         }
181
182         len = 0;
183         ptr = string;
184
185         /*
186          *      This is really rad_chap_pwencode()...
187          */
188         *ptr++ = packet->id;
189         len++;
190         memcpy(ptr, password->vp_strvalue, password->length);
191         ptr += password->length;
192         len += password->length;
193
194         /*
195          *      The challenge size is hard-coded.
196          */
197         memcpy(ptr, challenge, MD5_CHALLENGE_LEN);
198         len += MD5_CHALLENGE_LEN;
199
200         fr_md5_calc((u_char *)output, (u_char *)string, len);
201
202         /*
203          *      The length of the response is always 16 for MD5.
204          */
205         if (memcmp(output, packet->value, 16) != 0) {
206                 return 0;
207         }
208         return 1;
209 }
210
211 /*
212  *      Compose the portions of the reply packet specific to the
213  *      EAP-MD5 protocol, in the EAP reply typedata
214  */
215 int eapmd5_compose(EAP_DS *eap_ds, MD5_PACKET *reply)
216 {
217         uint8_t *ptr;
218         unsigned short name_len;
219
220         /*
221          *      We really only send Challenge (EAP-Identity),
222          *      and EAP-Success, and EAP-Failure.
223          */
224         if (reply->code < 3) {
225                 eap_ds->request->type.type = PW_EAP_MD5;
226
227                 rad_assert(reply->length > 0);
228
229                 eap_ds->request->type.data = malloc(reply->length);
230                 if (eap_ds->request->type.data == NULL) {
231                         eapmd5_free(&reply);
232                         radlog(L_ERR, "rlm_eap_md5: out of memory");
233                         return 0;
234                 }
235                 ptr = eap_ds->request->type.data;
236                 *ptr++ = (uint8_t)(reply->value_size & 0xFF);
237                 memcpy(ptr, reply->value, reply->value_size);
238
239                 /* Just the Challenge length */
240                 eap_ds->request->type.length = reply->value_size + 1;
241
242                 /*
243                  *      Return the name, if necessary.
244                  *
245                  *      Don't see why this is *ever* necessary...
246                  */
247                 name_len = reply->length - (reply->value_size + 1);
248                 if (name_len && reply->name) {
249                         ptr += reply->value_size;
250                         memcpy(ptr, reply->name, name_len);
251                         /* Challenge length + Name length */
252                         eap_ds->request->type.length += name_len;
253                 }
254         } else {
255                 eap_ds->request->type.length = 0;
256                 /* TODO: In future we might add message here wrt rfc1994 */
257         }
258         eap_ds->request->code = reply->code;
259
260         eapmd5_free(&reply);
261
262         return 1;
263 }