7e2e667f9af28ce222c1f07eae51ce109444c834
[strongswan.git] / src / libcharon / plugins / tnc_pdp / tnc_pdp.c
1 /*
2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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 "tnc_pdp.h"
17 #include "tnc_pdp_connections.h"
18
19 #include <errno.h>
20 #include <unistd.h>
21
22 #include <radius_message.h>
23 #include <radius_mppe.h>
24
25 #include <daemon.h>
26 #include <debug.h>
27 #include <pen/pen.h>
28 #include <threading/thread.h>
29 #include <processing/jobs/callback_job.h>
30 #include <sa/eap/eap_method.h>
31
32 typedef struct private_tnc_pdp_t private_tnc_pdp_t;
33
34 /**
35 * Maximum size of a RADIUS IP packet
36 */
37 #define MAX_PACKET 4096
38
39 /**
40 * private data of tnc_pdp_t
41 */
42 struct private_tnc_pdp_t {
43
44 /**
45 * implements tnc_pdp_t interface
46 */
47 tnc_pdp_t public;
48
49 /**
50 * ID of the server
51 */
52 identification_t *server;
53
54 /**
55 * EAP method type to be used
56 */
57 eap_type_t type;
58
59 /**
60 * IPv4 RADIUS socket
61 */
62 int ipv4;
63
64 /**
65 * IPv6 RADIUS socket
66 */
67 int ipv6;
68
69 /**
70 * RADIUS shared secret
71 */
72 chunk_t secret;
73
74 /**
75 * MD5 hasher
76 */
77 hasher_t *hasher;
78
79 /**
80 * HMAC MD5 signer, with secret set
81 */
82 signer_t *signer;
83
84 /**
85 * Random number generator for MS-MPPE salt values
86 */
87 rng_t *rng;
88
89 /**
90 * List of registered TNC-PDP connections
91 */
92 tnc_pdp_connections_t *connections;
93 };
94
95
96 /**
97 * Open IPv4 or IPv6 UDP RADIUS socket
98 */
99 static int open_socket(int family, u_int16_t port)
100 {
101 int on = TRUE;
102 struct sockaddr_storage addr;
103 socklen_t addrlen;
104 int skt;
105
106 memset(&addr, 0, sizeof(addr));
107 addr.ss_family = family;
108
109 /* precalculate constants depending on address family */
110 switch (family)
111 {
112 case AF_INET:
113 {
114 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
115
116 htoun32(&sin->sin_addr.s_addr, INADDR_ANY);
117 htoun16(&sin->sin_port, port);
118 addrlen = sizeof(struct sockaddr_in);
119 break;
120 }
121 case AF_INET6:
122 {
123 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
124
125 memcpy(&sin6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
126 htoun16(&sin6->sin6_port, port);
127 addrlen = sizeof(struct sockaddr_in6);
128 break;
129 }
130 default:
131 return 0;
132 }
133
134 /* open the socket */
135 skt = socket(family, SOCK_DGRAM, IPPROTO_UDP);
136 if (skt < 0)
137 {
138 DBG1(DBG_CFG, "opening RADIUS socket failed: %s", strerror(errno));
139 return 0;
140 }
141 if (setsockopt(skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)) < 0)
142 {
143 DBG1(DBG_CFG, "unable to set SO_REUSEADDR on socket: %s", strerror(errno));
144 close(skt);
145 return 0;
146 }
147
148 /* bind the socket */
149 if (bind(skt, (struct sockaddr *)&addr, addrlen) < 0)
150 {
151 DBG1(DBG_CFG, "unable to bind RADIUS socket: %s", strerror(errno));
152 close(skt);
153 return 0;
154 }
155
156 return skt;
157 }
158
159 /**
160 * Send a RADIUS message to client
161 */
162 static void send_message(private_tnc_pdp_t *this, radius_message_t *message,
163 host_t *client)
164 {
165 int fd;
166 chunk_t data;
167
168 fd = (client->get_family(client) == AF_INET) ? this->ipv4 : this->ipv6;
169 data = message->get_encoding(message);
170
171 DBG2(DBG_CFG, "sending RADIUS packet to %#H", client);
172 DBG3(DBG_CFG, "%B", &data);
173
174 if (sendto(fd, data.ptr, data.len, 0, client->get_sockaddr(client),
175 *client->get_sockaddr_len(client)) != data.len)
176 {
177 DBG1(DBG_CFG, "sending RADIUS message failed: %s", strerror(errno));
178 }
179 }
180
181 /**
182 * Encrypt a MS-MPPE-Send/Recv-Key
183 */
184 static chunk_t encrypt_mppe_key(private_tnc_pdp_t *this, u_int8_t type,
185 chunk_t key, u_int16_t *salt,
186 radius_message_t *request)
187 {
188 chunk_t a, r, seed, data;
189 u_char b[HASH_SIZE_MD5], *c;
190 mppe_key_t *mppe_key;
191
192 /**
193 * From RFC2548 (encryption):
194 * b(1) = MD5(S + R + A) c(1) = p(1) xor b(1) C = c(1)
195 * b(2) = MD5(S + c(1)) c(2) = p(2) xor b(2) C = C + c(2)
196 * . . .
197 * b(i) = MD5(S + c(i-1)) c(i) = p(i) xor b(i) C = C + c(i)
198 */
199
200 data = chunk_alloc(sizeof(mppe_key_t) +
201 HASH_SIZE_MD5 * (1 + key.len / HASH_SIZE_MD5));
202 memset(data.ptr, 0x00, data.len);
203
204 mppe_key = (mppe_key_t*)data.ptr;
205 mppe_key->id = htonl(PEN_MICROSOFT);
206 mppe_key->type = type;
207 mppe_key->length = data.len - sizeof(mppe_key->id);
208 mppe_key->key[0] = key.len;
209
210 memcpy(&mppe_key->key[1], key.ptr, key.len);
211
212 /**
213 * generate a 16 bit unique random salt value for the MPPE stream cipher
214 * the MSB of the salt MUST be set to 1
215 */
216 a = chunk_create((u_char*)&(mppe_key->salt), sizeof(mppe_key->salt));
217 do
218 {
219 this->rng->get_bytes(this->rng, a.len, a.ptr);
220 *a.ptr |= 0x80;
221 }
222 while (mppe_key->salt == *salt);
223
224 /* update the salt value */
225 *salt = mppe_key->salt;
226
227 r = chunk_create(request->get_authenticator(request), HASH_SIZE_MD5);
228 seed = chunk_cata("cc", r, a);
229
230 c = mppe_key->key;
231 while (c < data.ptr + data.len)
232 {
233 /* b(i) = MD5(S + c(i-1)) */
234 this->hasher->get_hash(this->hasher, this->secret, NULL);
235 this->hasher->get_hash(this->hasher, seed, b);
236
237 /* c(i) = b(i) xor p(1) */
238 memxor(c, b, HASH_SIZE_MD5);
239
240 /* prepare next round */
241 seed = chunk_create(c, HASH_SIZE_MD5);
242 c += HASH_SIZE_MD5;
243 }
244
245 return data;
246 }
247
248 /**
249 * Send a RADIUS response for a request
250 */
251 static void send_response(private_tnc_pdp_t *this, radius_message_t *request,
252 radius_message_code_t code, eap_payload_t *eap,
253 identification_t *group, chunk_t msk, host_t *client)
254 {
255 radius_message_t *response;
256 chunk_t data, recv, send;
257 u_int32_t tunnel_type;
258 u_int16_t salt = 0;
259
260 response = radius_message_create(code);
261 if (eap)
262 {
263 data = eap->get_data(eap);
264 DBG3(DBG_CFG, "%N payload %B", eap_type_names, this->type, &data);
265
266 /* fragment data suitable for RADIUS */
267 while (data.len > MAX_RADIUS_ATTRIBUTE_SIZE)
268 {
269 response->add(response, RAT_EAP_MESSAGE,
270 chunk_create(data.ptr, MAX_RADIUS_ATTRIBUTE_SIZE));
271 data = chunk_skip(data, MAX_RADIUS_ATTRIBUTE_SIZE);
272 }
273 response->add(response, RAT_EAP_MESSAGE, data);
274 }
275 if (group)
276 {
277 tunnel_type = RADIUS_TUNNEL_TYPE_ESP;
278 htoun32(data.ptr, tunnel_type);
279 data.len = sizeof(tunnel_type);
280 response->add(response, RAT_TUNNEL_TYPE, data);
281 response->add(response, RAT_FILTER_ID, group->get_encoding(group));
282 }
283 if (msk.len)
284 {
285 recv = chunk_create(msk.ptr, msk.len / 2);
286 data = encrypt_mppe_key(this, MS_MPPE_RECV_KEY, recv, &salt, request);
287 response->add(response, RAT_VENDOR_SPECIFIC, data);
288 chunk_free(&data);
289
290 send = chunk_create(msk.ptr + recv.len, msk.len - recv.len);
291 data = encrypt_mppe_key(this, MS_MPPE_SEND_KEY, send, &salt, request);
292 response->add(response, RAT_VENDOR_SPECIFIC, data);
293 chunk_free(&data);
294 }
295 response->set_identifier(response, request->get_identifier(request));
296 response->sign(response, request->get_authenticator(request),
297 this->secret, this->hasher, this->signer, NULL, TRUE);
298
299 DBG1(DBG_CFG, "sending RADIUS %N to client '%H'", radius_message_code_names,
300 code, client);
301 send_message(this, response, client);
302 response->destroy(response);
303 }
304
305 /**
306 * Process EAP message
307 */
308 static void process_eap(private_tnc_pdp_t *this, radius_message_t *request,
309 host_t *source)
310 {
311 enumerator_t *enumerator;
312 eap_payload_t *in, *out = NULL;
313 eap_method_t *method;
314 eap_type_t eap_type;
315 u_int32_t eap_vendor;
316 chunk_t data, message = chunk_empty, msk = chunk_empty;
317 chunk_t user_name = chunk_empty, nas_id = chunk_empty;
318 identification_t *group = NULL;
319 radius_message_code_t code = RMC_ACCESS_CHALLENGE;
320 int type;
321
322 enumerator = request->create_enumerator(request);
323 while (enumerator->enumerate(enumerator, &type, &data))
324 {
325 switch (type)
326 {
327 case RAT_USER_NAME:
328 user_name = data;
329 break;
330 case RAT_NAS_IDENTIFIER:
331 nas_id = data;
332 break;
333 case RAT_EAP_MESSAGE:
334 if (data.len)
335 {
336 message = chunk_cat("mc", message, data);
337 }
338 break;
339 default:
340 break;
341 }
342 }
343 enumerator->destroy(enumerator);
344
345 if (message.len)
346 {
347 in = eap_payload_create_data(message);
348
349 /* apply EAP method selected by RADIUS server */
350 eap_type = in->get_type(in, &eap_vendor);
351
352 DBG3(DBG_CFG, "%N payload %B", eap_type_names, eap_type, &message);
353
354 if (eap_type == EAP_IDENTITY)
355 {
356 identification_t *peer;
357 chunk_t eap_identity;
358
359 if (message.len < 5)
360 {
361 goto end;
362 }
363 eap_identity = chunk_create(message.ptr + 5, message.len - 5);
364 peer = identification_create_from_data(eap_identity);
365 method = charon->eap->create_instance(charon->eap, this->type,
366 0, EAP_SERVER, this->server, peer);
367 if (!method)
368 {
369 peer->destroy(peer);
370 goto end;
371 }
372 this->connections->add(this->connections, nas_id, user_name, peer,
373 method);
374 method->initiate(method, &out);
375 }
376 else
377 {
378 ike_sa_t *ike_sa;
379 auth_cfg_t *auth;
380 auth_rule_t type;
381 identification_t *data;
382 enumerator_t *e;
383
384 method = this->connections->get_state(this->connections, nas_id,
385 user_name, &ike_sa);
386 if (!method)
387 {
388 goto end;
389 }
390 charon->bus->set_sa(charon->bus, ike_sa);
391
392 switch (method->process(method, in, &out))
393 {
394 case NEED_MORE:
395 code = RMC_ACCESS_CHALLENGE;
396 break;
397 case SUCCESS:
398 code = RMC_ACCESS_ACCEPT;
399 method->get_msk(method, &msk);
400 auth = ike_sa->get_auth_cfg(ike_sa, FALSE);
401 e = auth->create_enumerator(auth);
402 while (e->enumerate(e, &type, &data))
403 {
404 /* look for group memberships */
405 if (type == AUTH_RULE_GROUP)
406 {
407 group = data;
408 }
409 }
410 e->destroy(e);
411
412 DESTROY_IF(out);
413 out = eap_payload_create_code(EAP_SUCCESS,
414 in->get_identifier(in));
415 break;
416 case FAILED:
417 default:
418 code = RMC_ACCESS_REJECT;
419 DESTROY_IF(out);
420 out = eap_payload_create_code(EAP_FAILURE,
421 in->get_identifier(in));
422 }
423 charon->bus->set_sa(charon->bus, NULL);
424 }
425
426 send_response(this, request, code, out, group, msk, source);
427 out->destroy(out);
428
429 if (code == RMC_ACCESS_ACCEPT || code == RMC_ACCESS_REJECT)
430 {
431 this->connections->remove(this->connections, nas_id, user_name);
432 }
433
434 end:
435 free(message.ptr);
436 in->destroy(in);
437 }
438 }
439
440 /**
441 * Process packets received on the RADIUS socket
442 */
443 static job_requeue_t receive(private_tnc_pdp_t *this)
444 {
445 while (TRUE)
446 {
447 radius_message_t *request;
448 char buffer[MAX_PACKET];
449 int max_fd = 0, selected = 0, bytes_read = 0;
450 fd_set rfds;
451 bool oldstate;
452 host_t *source;
453 struct msghdr msg;
454 struct iovec iov;
455 union {
456 struct sockaddr_in in4;
457 struct sockaddr_in6 in6;
458 } src;
459
460 FD_ZERO(&rfds);
461
462 if (this->ipv4)
463 {
464 FD_SET(this->ipv4, &rfds);
465 }
466 if (this->ipv6)
467 {
468 FD_SET(this->ipv6, &rfds);
469 }
470 max_fd = max(this->ipv4, this->ipv6);
471
472 DBG2(DBG_CFG, "waiting for data on RADIUS sockets");
473 oldstate = thread_cancelability(TRUE);
474 if (select(max_fd + 1, &rfds, NULL, NULL, NULL) <= 0)
475 {
476 thread_cancelability(oldstate);
477 continue;
478 }
479 thread_cancelability(oldstate);
480
481 if (FD_ISSET(this->ipv4, &rfds))
482 {
483 selected = this->ipv4;
484 }
485 else if (FD_ISSET(this->ipv6, &rfds))
486 {
487 selected = this->ipv6;
488 }
489 else
490 {
491 /* oops, shouldn't happen */
492 continue;
493 }
494
495 /* read received packet */
496 msg.msg_name = &src;
497 msg.msg_namelen = sizeof(src);
498 iov.iov_base = buffer;
499 iov.iov_len = MAX_PACKET;
500 msg.msg_iov = &iov;
501 msg.msg_iovlen = 1;
502 msg.msg_flags = 0;
503
504 bytes_read = recvmsg(selected, &msg, 0);
505 if (bytes_read < 0)
506 {
507 DBG1(DBG_CFG, "error reading RADIUS socket: %s", strerror(errno));
508 continue;
509 }
510 if (msg.msg_flags & MSG_TRUNC)
511 {
512 DBG1(DBG_CFG, "receive buffer too small, RADIUS packet discarded");
513 continue;
514 }
515 source = host_create_from_sockaddr((sockaddr_t*)&src);
516 DBG2(DBG_CFG, "received RADIUS packet from %#H", source);
517 DBG3(DBG_CFG, "%b", buffer, bytes_read);
518 request = radius_message_parse(chunk_create(buffer, bytes_read));
519 if (request)
520 {
521 DBG1(DBG_CFG, "received RADIUS %N from client '%H'",
522 radius_message_code_names, request->get_code(request), source);
523
524 if (request->verify(request, NULL, this->secret, this->hasher,
525 this->signer))
526 {
527 process_eap(this, request, source);
528 }
529 request->destroy(request);
530
531 }
532 else
533 {
534 DBG1(DBG_CFG, "received invalid RADIUS message, ignored");
535 }
536 source->destroy(source);
537 }
538 return JOB_REQUEUE_FAIR;
539 }
540
541 METHOD(tnc_pdp_t, destroy, void,
542 private_tnc_pdp_t *this)
543 {
544 if (this->ipv4)
545 {
546 close(this->ipv4);
547 }
548 if (this->ipv6)
549 {
550 close(this->ipv6);
551 }
552 DESTROY_IF(this->server);
553 DESTROY_IF(this->signer);
554 DESTROY_IF(this->hasher);
555 DESTROY_IF(this->rng);
556 DESTROY_IF(this->connections);
557 free(this);
558 }
559
560 /*
561 * see header file
562 */
563 tnc_pdp_t *tnc_pdp_create(u_int16_t port)
564 {
565 private_tnc_pdp_t *this;
566 char *secret, *server, *eap_type_str;
567
568 INIT(this,
569 .public = {
570 .destroy = _destroy,
571 },
572 .ipv4 = open_socket(AF_INET, port),
573 .ipv6 = open_socket(AF_INET6, port),
574 .hasher = lib->crypto->create_hasher(lib->crypto, HASH_MD5),
575 .signer = lib->crypto->create_signer(lib->crypto, AUTH_HMAC_MD5_128),
576 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
577 .connections = tnc_pdp_connections_create(),
578 );
579
580 if (!this->hasher || !this->signer || !this->rng)
581 {
582 DBG1(DBG_CFG, "RADIUS initialization failed, HMAC/MD5/RNG required");
583 destroy(this);
584 return NULL;
585 }
586 if (!this->ipv4 && !this->ipv6)
587 {
588 DBG1(DBG_NET, "could not create any RADIUS sockets");
589 destroy(this);
590 return NULL;
591 }
592 if (!this->ipv4)
593 {
594 DBG1(DBG_NET, "could not open IPv4 RADIUS socket, IPv4 disabled");
595 }
596 if (!this->ipv6)
597 {
598 DBG1(DBG_NET, "could not open IPv6 RADIUS socket, IPv6 disabled");
599 }
600
601 server = lib->settings->get_str(lib->settings,
602 "%s.plugins.tnc-pdp.server", NULL, charon->name);
603 if (!server)
604 {
605 DBG1(DBG_CFG, "missing PDP server name, PDP disabled");
606 destroy(this);
607 return NULL;
608 }
609 this->server = identification_create_from_string(server);
610
611 secret = lib->settings->get_str(lib->settings,
612 "%s.plugins.tnc-pdp.secret", NULL, charon->name);
613 if (!secret)
614 {
615 DBG1(DBG_CFG, "missing RADIUS secret, PDP disabled");
616 destroy(this);
617 return NULL;
618 }
619 this->secret = chunk_create(secret, strlen(secret));
620 this->signer->set_key(this->signer, this->secret);
621
622 eap_type_str = lib->settings->get_str(lib->settings,
623 "%s.plugins.tnc-pdp.method", "ttls", charon->name);
624 this->type = eap_type_from_string(eap_type_str);
625 if (this->type == 0)
626 {
627 DBG1(DBG_CFG, "unrecognized eap method \"%s\"", eap_type_str);
628 destroy(this);
629 return NULL;
630 }
631 DBG1(DBG_IKE, "eap method %N selected", eap_type_names, this->type);
632
633 lib->processor->queue_job(lib->processor,
634 (job_t*)callback_job_create_with_prio((callback_job_cb_t)receive, this,
635 NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
636
637 return &this->public;
638 }
639