allow debug of raw RADIUS data
[strongswan.git] / src / libradius / radius_socket.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "radius_socket.h"
17
18 #include <errno.h>
19 #include <unistd.h>
20
21 #include <debug.h>
22
23 /**
24 * Vendor-Id of Microsoft specific attributes
25 */
26 #define VENDOR_ID_MICROSOFT 311
27
28 /**
29 * Microsoft specific vendor attributes
30 */
31 #define MS_MPPE_SEND_KEY 16
32 #define MS_MPPE_RECV_KEY 17
33
34 typedef struct private_radius_socket_t private_radius_socket_t;
35
36 /**
37 * Private data of an radius_socket_t object.
38 */
39 struct private_radius_socket_t {
40
41 /**
42 * Public radius_socket_t interface.
43 */
44 radius_socket_t public;
45
46 /**
47 * Server port for authentication
48 */
49 u_int16_t auth_port;
50
51 /**
52 * socket file descriptor for authentication
53 */
54 int auth_fd;
55
56 /**
57 * Server port for accounting
58 */
59 u_int16_t acct_port;
60
61 /**
62 * socket file descriptor for accounting
63 */
64 int acct_fd;
65
66 /**
67 * Server address
68 */
69 char *address;
70
71 /**
72 * current RADIUS identifier
73 */
74 u_int8_t identifier;
75
76 /**
77 * hasher to use for response verification
78 */
79 hasher_t *hasher;
80
81 /**
82 * HMAC-MD5 signer to build Message-Authenticator attribute
83 */
84 signer_t *signer;
85
86 /**
87 * random number generator for RADIUS request authenticator
88 */
89 rng_t *rng;
90
91 /**
92 * RADIUS secret
93 */
94 chunk_t secret;
95 };
96
97 /**
98 * Check or establish RADIUS connection
99 */
100 static bool check_connection(private_radius_socket_t *this,
101 int *fd, u_int16_t port)
102 {
103 if (*fd == -1)
104 {
105 host_t *server;
106
107 server = host_create_from_dns(this->address, AF_UNSPEC, port);
108 if (!server)
109 {
110 DBG1(DBG_CFG, "resolving RADIUS server address '%s' failed",
111 this->address);
112 return FALSE;
113 }
114 *fd = socket(server->get_family(server), SOCK_DGRAM, IPPROTO_UDP);
115 if (*fd == -1)
116 {
117 DBG1(DBG_CFG, "opening RADIUS socket for %#H failed: %s",
118 server, strerror(errno));
119 server->destroy(server);
120 return FALSE;
121 }
122 if (connect(*fd, server->get_sockaddr(server),
123 *server->get_sockaddr_len(server)) < 0)
124 {
125 DBG1(DBG_CFG, "connecting RADIUS socket to %#H failed: %s",
126 server, strerror(errno));
127 server->destroy(server);
128 close(*fd);
129 *fd = -1;
130 return FALSE;
131 }
132 server->destroy(server);
133 }
134 return TRUE;
135 }
136
137 METHOD(radius_socket_t, request, radius_message_t*,
138 private_radius_socket_t *this, radius_message_t *request)
139 {
140 chunk_t data;
141 int i, *fd;
142 u_int16_t port;
143 rng_t *rng = NULL;
144
145 if (request->get_code(request) == RMC_ACCOUNTING_REQUEST)
146 {
147 fd = &this->acct_fd;
148 port = this->acct_port;
149 }
150 else
151 {
152 fd = &this->auth_fd;
153 port = this->auth_port;
154 rng = this->rng;
155 }
156
157 /* set Message Identifier */
158 request->set_identifier(request, this->identifier++);
159 /* sign the request */
160 request->sign(request, NULL, this->secret, this->hasher, this->signer, rng);
161
162 if (!check_connection(this, fd, port))
163 {
164 return NULL;
165 }
166
167 data = request->get_encoding(request);
168 DBG3(DBG_CFG, "%B", &data);
169
170 /* timeout after 2, 3, 4, 5 seconds */
171 for (i = 2; i <= 5; i++)
172 {
173 radius_message_t *response;
174 bool retransmit = FALSE;
175 struct timeval tv;
176 char buf[4096];
177 fd_set fds;
178 int res;
179
180 if (send(*fd, data.ptr, data.len, 0) != data.len)
181 {
182 DBG1(DBG_CFG, "sending RADIUS message failed: %s", strerror(errno));
183 return NULL;
184 }
185 tv.tv_sec = i;
186 tv.tv_usec = 0;
187
188 while (TRUE)
189 {
190 FD_ZERO(&fds);
191 FD_SET(*fd, &fds);
192 res = select((*fd) + 1, &fds, NULL, NULL, &tv);
193 /* TODO: updated tv to time not waited. Linux does this for us. */
194 if (res < 0)
195 { /* failed */
196 DBG1(DBG_CFG, "waiting for RADIUS message failed: %s",
197 strerror(errno));
198 break;
199 }
200 if (res == 0)
201 { /* timeout */
202 DBG1(DBG_CFG, "retransmitting RADIUS message");
203 retransmit = TRUE;
204 break;
205 }
206 res = recv(*fd, buf, sizeof(buf), MSG_DONTWAIT);
207 if (res <= 0)
208 {
209 DBG1(DBG_CFG, "receiving RADIUS message failed: %s",
210 strerror(errno));
211 break;
212 }
213 response = radius_message_parse(chunk_create(buf, res));
214 if (response)
215 {
216 if (response->verify(response,
217 request->get_authenticator(request), this->secret,
218 this->hasher, this->signer))
219 {
220 return response;
221 }
222 response->destroy(response);
223 }
224 DBG1(DBG_CFG, "received invalid RADIUS message, ignored");
225 }
226 if (!retransmit)
227 {
228 break;
229 }
230 }
231 DBG1(DBG_CFG, "RADIUS server is not responding");
232 return NULL;
233 }
234
235 /**
236 * Decrypt a MS-MPPE-Send/Recv-Key
237 */
238 static chunk_t decrypt_mppe_key(private_radius_socket_t *this, u_int16_t salt,
239 chunk_t C, radius_message_t *request)
240 {
241 chunk_t A, R, P, seed;
242 u_char *c, *p;
243
244 /**
245 * From RFC2548 (encryption):
246 * b(1) = MD5(S + R + A) c(1) = p(1) xor b(1) C = c(1)
247 * b(2) = MD5(S + c(1)) c(2) = p(2) xor b(2) C = C + c(2)
248 * . . .
249 * b(i) = MD5(S + c(i-1)) c(i) = p(i) xor b(i) C = C + c(i)
250 */
251
252 if (C.len % HASH_SIZE_MD5 || C.len < HASH_SIZE_MD5)
253 {
254 return chunk_empty;
255 }
256
257 A = chunk_create((u_char*)&salt, sizeof(salt));
258 R = chunk_create(request->get_authenticator(request), HASH_SIZE_MD5);
259 P = chunk_alloca(C.len);
260 p = P.ptr;
261 c = C.ptr;
262
263 seed = chunk_cata("cc", R, A);
264
265 while (c < C.ptr + C.len)
266 {
267 /* b(i) = MD5(S + c(i-1)) */
268 this->hasher->get_hash(this->hasher, this->secret, NULL);
269 this->hasher->get_hash(this->hasher, seed, p);
270
271 /* p(i) = b(i) xor c(1) */
272 memxor(p, c, HASH_SIZE_MD5);
273
274 /* prepare next round */
275 seed = chunk_create(c, HASH_SIZE_MD5);
276 c += HASH_SIZE_MD5;
277 p += HASH_SIZE_MD5;
278 }
279
280 /* remove truncation, first byte is key length */
281 if (*P.ptr >= P.len)
282 { /* decryption failed? */
283 return chunk_empty;
284 }
285 return chunk_clone(chunk_create(P.ptr + 1, *P.ptr));
286 }
287
288 METHOD(radius_socket_t, decrypt_msk, chunk_t,
289 private_radius_socket_t *this, radius_message_t *request,
290 radius_message_t *response)
291 {
292 struct {
293 u_int32_t id;
294 u_int8_t type;
295 u_int8_t length;
296 u_int16_t salt;
297 u_int8_t key[];
298 } __attribute__((packed)) *mppe_key;
299 enumerator_t *enumerator;
300 chunk_t data, send = chunk_empty, recv = chunk_empty;
301 int type;
302
303 enumerator = response->create_enumerator(response);
304 while (enumerator->enumerate(enumerator, &type, &data))
305 {
306 if (type == RAT_VENDOR_SPECIFIC &&
307 data.len > sizeof(*mppe_key))
308 {
309 mppe_key = (void*)data.ptr;
310 if (ntohl(mppe_key->id) == VENDOR_ID_MICROSOFT &&
311 mppe_key->length == data.len - sizeof(mppe_key->id))
312 {
313 data = chunk_create(mppe_key->key, data.len - sizeof(*mppe_key));
314 if (mppe_key->type == MS_MPPE_SEND_KEY)
315 {
316 send = decrypt_mppe_key(this, mppe_key->salt, data, request);
317 }
318 if (mppe_key->type == MS_MPPE_RECV_KEY)
319 {
320 recv = decrypt_mppe_key(this, mppe_key->salt, data, request);
321 }
322 }
323 }
324 }
325 enumerator->destroy(enumerator);
326 if (send.ptr && recv.ptr)
327 {
328 return chunk_cat("mm", recv, send);
329 }
330 chunk_clear(&send);
331 chunk_clear(&recv);
332 return chunk_empty;
333 }
334
335 METHOD(radius_socket_t, destroy, void,
336 private_radius_socket_t *this)
337 {
338 DESTROY_IF(this->hasher);
339 DESTROY_IF(this->signer);
340 DESTROY_IF(this->rng);
341 if (this->auth_fd != -1)
342 {
343 close(this->auth_fd);
344 };
345 if (this->acct_fd != -1)
346 {
347 close(this->acct_fd);
348 }
349 free(this);
350 }
351
352 /**
353 * See header
354 */
355 radius_socket_t *radius_socket_create(char *address, u_int16_t auth_port,
356 u_int16_t acct_port, chunk_t secret)
357 {
358 private_radius_socket_t *this;
359
360 INIT(this,
361 .public = {
362 .request = _request,
363 .decrypt_msk = _decrypt_msk,
364 .destroy = _destroy,
365 },
366 .address = address,
367 .auth_port = auth_port,
368 .auth_fd = -1,
369 .acct_port = acct_port,
370 .acct_fd = -1,
371 );
372
373 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_MD5);
374 this->signer = lib->crypto->create_signer(lib->crypto, AUTH_HMAC_MD5_128);
375 this->rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
376 if (!this->hasher || !this->signer || !this->rng)
377 {
378 DBG1(DBG_CFG, "RADIUS initialization failed, HMAC/MD5/RNG required");
379 destroy(this);
380 return NULL;
381 }
382 this->secret = secret;
383 this->signer->set_key(this->signer, secret);
384 /* we use a random identifier, helps if we restart often */
385 this->identifier = random();
386
387 return &this->public;
388 }