Debian Codegraph @

extensions/app_diameap/plugins/eap_md5/eap_md5.c

raw
1 /*****************************************************************************************************
2 * Software License Agreement (BSD License)
3 * Author : Souheil Ben Ayed <[email protected]>
4 *
5 * Copyright (c) 2009-2010, Souheil Ben Ayed, Teraoka Laboratory of Keio University, and the WIDE Project
6 * All rights reserved.
7 *
8 * Redistribution and use of this software in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Souheil Ben Ayed <[email protected]>.
21 *
22 * 4. Neither the name of Souheil Ben Ayed, Teraoka Laboratory of Keio University or the WIDE Project nor the
23 * names of its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
30 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *****************************************************************************************************/
37
38
39 #include "../../plugins.h"
40 GCC_DIAG_OFF("-Wdeprecated-declarations")
41 #include <gcrypt.h>
42 GCC_DIAG_ON("-Wdeprecated-declarations")
43
44 #define CHALLENGE_LEN 16
45
46 struct eap_md5_data
47 {
48 enum
49 {
50 EAP_MD5_CONTINUE, EAP_MD5_SUCCESS, EAP_MD5_FAILURE
51 } state;
52 u8 * challenge;
53 };
54
55 __attribute__((visibility("default")))
56 int eap_md5_configure(char * configfile);
57
58 __attribute__((visibility("default")))
59 int eap_md5_init(struct eap_state_machine *smd);
60
61 __attribute__((visibility("default")))
62 int eap_md5_initPickUp(struct eap_state_machine *smd);
63
64 __attribute__((visibility("default")))
65 int eap_md5_buildReq(struct eap_state_machine *smd, u8 eap_md5,
66 struct eap_packet * eapPacket);
67
68 __attribute__((visibility("default")))
69 boolean eap_md5_check(struct eap_state_machine *smd, struct eap_packet *eapRespData);
70
71 __attribute__((visibility("default")))
72 int eap_md5_process(struct eap_state_machine *smd, struct eap_packet *eapRespData);
73
74 __attribute__((visibility("default")))
75 boolean eap_md5_isDone(struct eap_state_machine *smd);
76
77 __attribute__((visibility("default")))
78 void eap_md5_free(void * data);
79
80 REGISTER_METHOD("eap_md5", "eap_md5_configure", "eap_md5_init", "eap_md5_initPickUp", "eap_md5_buildReq", NULL, "eap_md5_check", "eap_md5_process", "eap_md5_isDone", NULL, NULL, "eap_md5_free")
81 ;
82
83 int eap_md5_configure(char * configfile)
84 {
85 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
86
87 return 0;
88 }
89
90 int eap_md5_init(struct eap_state_machine *smd)
91 {
92 struct eap_md5_data *data = NULL;
93 CHECK_MALLOC(data = malloc(sizeof(struct eap_md5_data)));
94 memset(data, 0, sizeof(struct eap_md5_data));
95 data->state = EAP_MD5_CONTINUE;
96 data->challenge = NULL;
97 smd->methodData = (struct eap_md5_data*) data;
98 return 0;
99 }
100
101 int eap_md5_initPickUp(struct eap_state_machine *smd)
102 {
103 return 0;
104 }
105
106
107 int eap_md5_buildReq(struct eap_state_machine *smd, u8 id,
108 struct eap_packet * eapPacket)
109 {
110 struct eap_md5_data * data;
111 u8 *payload, *challenge;
112
113 data = (struct eap_md5_data *) smd->methodData;
114 CHECK_MALLOC( challenge=malloc(sizeof(u8)*CHALLENGE_LEN));
115 CHECK_MALLOC( payload=malloc(sizeof(u8)*(CHALLENGE_LEN+1)));
116 memset(payload, 0, sizeof(u8) * (CHALLENGE_LEN + 1));
117
118 gcry_create_nonce(challenge,CHALLENGE_LEN);
119 U8COPY(payload, 1, CHALLENGE_LEN, challenge);
120 payload[0] = (u8) CHALLENGE_LEN;
121 CHECK_FCT(diameap_eap_new(EAP_REQUEST, id, TYPE_EAP_MD5, payload, CHALLENGE_LEN
122 + 1,eapPacket));
123 CHECK_MALLOC(data->challenge=realloc(data->challenge,sizeof(u8)*CHALLENGE_LEN));
124 U8COPY(data->challenge,0,CHALLENGE_LEN,challenge);
125
126 smd->methodData = data;
127
128 free(challenge);
129 challenge=NULL;
130 free(payload);
131 payload=NULL;
132
133 return 0;
134 }
135
136
137 boolean eap_md5_check(struct eap_state_machine *smd, struct eap_packet *eapRespData)
138 {
139 eap_type type;
140 if(diameap_eap_get_type(eapRespData,&type)!=0){
141 return FALSE;
142 }
143 if (type == TYPE_EAP_MD5)
144 {
145 u16 length;
146 CHECK_FCT(diameap_eap_get_length(eapRespData,&length));
147 if ((int) length < 6)
148 {
149 TRACE_DEBUG(INFO,"%s[EAP MD5 plugin] Incorrect EAP packet received (length = %d ).",DIAMEAP_EXTENSION,length);
150 return FALSE;
151 }
152 return TRUE;
153 }
154 return FALSE;
155 }
156
157
158
159 int eap_md5_process(struct eap_state_machine *smd, struct eap_packet *eapRespData)
160 {
161
162 struct eap_md5_data * data;
163 int wordlen = 0, i = 0;
164 u8 * word, *hash, id;
165 data = (struct eap_md5_data*) smd->methodData;
166 wordlen = 1 + smd->user.passwordLength + CHALLENGE_LEN;
167 CHECK_MALLOC(word = malloc(sizeof(u8)*wordlen));
168 memset(word, 0, sizeof(u8) * wordlen);
169 CHECK_FCT(diameap_eap_get_identifier(eapRespData,&id));
170 *word = id;
171 U8COPY(word,1,smd->user.passwordLength,smd->user.password);
172 U8COPY(word,1+smd->user.passwordLength,CHALLENGE_LEN,data->challenge);
173 CHECK_MALLOC(hash = malloc(sizeof(u8)*16));
174
175 md5hash(word, wordlen, hash);
176
177
178 for (i = 0; i < CHALLENGE_LEN; i++)
179 {
180 if (G8(hash + i) != G8(eapRespData->data + 6 + i))
181 {
182 data->state = EAP_MD5_FAILURE;
183 }
184 }
185
186 if (data->state != EAP_MD5_FAILURE)
187 {
188 data->state = EAP_MD5_SUCCESS;
189 smd->user.success = TRUE;
190 }
191
192 smd->methodData = data;
193 free(hash);
194 hash=NULL;
195 free(word);
196 word=NULL;
197 return 0;
198 }
199
200 boolean eap_md5_isDone(struct eap_state_machine *smd)
201 {
202 struct eap_md5_data *data;
203 data = (struct eap_md5_data*) smd->methodData;
204 if (data->state != EAP_MD5_CONTINUE)
205 {
206 return TRUE;
207 }
208 return FALSE;
209 }
210
211
212 void eap_md5_free(void * mdata)
213 {
214 struct eap_md5_data *data;
215 data = (struct eap_md5_data*) mdata;
216 free(data->challenge);
217 data->challenge=NULL;
218 free(data);
219 data=NULL;
220 }