Add a return value to radius_message_t.sign()
[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 if (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'",
300 radius_message_code_names, code, client);
301 send_message(this, response, client);
302 }
303 response->destroy(response);
304 }
305
306 /**
307 * Process EAP message
308 */
309 static void process_eap(private_tnc_pdp_t *this, radius_message_t *request,
310 host_t *source)
311 {
312 enumerator_t *enumerator;
313 eap_payload_t *in, *out = NULL;
314 eap_method_t *method;
315 eap_type_t eap_type;
316 u_int32_t eap_vendor;
317 chunk_t data, message = chunk_empty, msk = chunk_empty;
318 chunk_t user_name = chunk_empty, nas_id = chunk_empty;
319 identification_t *group = NULL;
320 radius_message_code_t code = RMC_ACCESS_CHALLENGE;
321 int type;
322
323 enumerator = request->create_enumerator(request);
324 while (enumerator->enumerate(enumerator, &type, &data))
325 {
326 switch (type)
327 {
328 case RAT_USER_NAME:
329 user_name = data;
330 break;
331 case RAT_NAS_IDENTIFIER:
332 nas_id = data;
333 break;
334 case RAT_EAP_MESSAGE:
335 if (data.len)
336 {
337 message = chunk_cat("mc", message, data);
338 }
339 break;
340 default:
341 break;
342 }
343 }
344 enumerator->destroy(enumerator);
345
346 if (message.len)
347 {
348 in = eap_payload_create_data(message);
349
350 /* apply EAP method selected by RADIUS server */
351 eap_type = in->get_type(in, &eap_vendor);
352
353 DBG3(DBG_CFG, "%N payload %B", eap_type_names, eap_type, &message);
354
355 if (eap_type == EAP_IDENTITY)
356 {
357 identification_t *peer;
358 chunk_t eap_identity;
359
360 if (message.len < 5)
361 {
362 goto end;
363 }
364 eap_identity = chunk_create(message.ptr + 5, message.len - 5);
365 peer = identification_create_from_data(eap_identity);
366 method = charon->eap->create_instance(charon->eap, this->type,
367 0, EAP_SERVER, this->server, peer);
368 if (!method)
369 {
370 peer->destroy(peer);
371 goto end;
372 }
373 this->connections->add(this->connections, nas_id, user_name, peer,
374 method);
375 method->initiate(method, &out);
376 }
377 else
378 {
379 ike_sa_t *ike_sa;
380 auth_cfg_t *auth;
381 auth_rule_t type;
382 identification_t *data;
383 enumerator_t *e;
384
385 method = this->connections->get_state(this->connections, nas_id,
386 user_name, &ike_sa);
387 if (!method)
388 {
389 goto end;
390 }
391 charon->bus->set_sa(charon->bus, ike_sa);
392
393 switch (method->process(method, in, &out))
394 {
395 case NEED_MORE:
396 code = RMC_ACCESS_CHALLENGE;
397 break;
398 case SUCCESS:
399 code = RMC_ACCESS_ACCEPT;
400 method->get_msk(method, &msk);
401 auth = ike_sa->get_auth_cfg(ike_sa, FALSE);
402 e = auth->create_enumerator(auth);
403 while (e->enumerate(e, &type, &data))
404 {
405 /* look for group memberships */
406 if (type == AUTH_RULE_GROUP)
407 {
408 group = data;
409 }
410 }
411 e->destroy(e);
412
413 DESTROY_IF(out);
414 out = eap_payload_create_code(EAP_SUCCESS,
415 in->get_identifier(in));
416 break;
417 case FAILED:
418 default:
419 code = RMC_ACCESS_REJECT;
420 DESTROY_IF(out);
421 out = eap_payload_create_code(EAP_FAILURE,
422 in->get_identifier(in));
423 }
424 charon->bus->set_sa(charon->bus, NULL);
425 }
426
427 send_response(this, request, code, out, group, msk, source);
428 out->destroy(out);
429
430 if (code == RMC_ACCESS_ACCEPT || code == RMC_ACCESS_REJECT)
431 {
432 this->connections->remove(this->connections, nas_id, user_name);
433 }
434
435 end:
436 free(message.ptr);
437 in->destroy(in);
438 }
439 }
440
441 /**
442 * Process packets received on the RADIUS socket
443 */
444 static job_requeue_t receive(private_tnc_pdp_t *this)
445 {
446 while (TRUE)
447 {
448 radius_message_t *request;
449 char buffer[MAX_PACKET];
450 int max_fd = 0, selected = 0, bytes_read = 0;
451 fd_set rfds;
452 bool oldstate;
453 host_t *source;
454 struct msghdr msg;
455 struct iovec iov;
456 union {
457 struct sockaddr_in in4;
458 struct sockaddr_in6 in6;
459 } src;
460
461 FD_ZERO(&rfds);
462
463 if (this->ipv4)
464 {
465 FD_SET(this->ipv4, &rfds);
466 }
467 if (this->ipv6)
468 {
469 FD_SET(this->ipv6, &rfds);
470 }
471 max_fd = max(this->ipv4, this->ipv6);
472
473 DBG2(DBG_CFG, "waiting for data on RADIUS sockets");
474 oldstate = thread_cancelability(TRUE);
475 if (select(max_fd + 1, &rfds, NULL, NULL, NULL) <= 0)
476 {
477 thread_cancelability(oldstate);
478 continue;
479 }
480 thread_cancelability(oldstate);
481
482 if (FD_ISSET(this->ipv4, &rfds))
483 {
484 selected = this->ipv4;
485 }
486 else if (FD_ISSET(this->ipv6, &rfds))
487 {
488 selected = this->ipv6;
489 }
490 else
491 {
492 /* oops, shouldn't happen */
493 continue;
494 }
495
496 /* read received packet */
497 msg.msg_name = &src;
498 msg.msg_namelen = sizeof(src);
499 iov.iov_base = buffer;
500 iov.iov_len = MAX_PACKET;
501 msg.msg_iov = &iov;
502 msg.msg_iovlen = 1;
503 msg.msg_flags = 0;
504
505 bytes_read = recvmsg(selected, &msg, 0);
506 if (bytes_read < 0)
507 {
508 DBG1(DBG_CFG, "error reading RADIUS socket: %s", strerror(errno));
509 continue;
510 }
511 if (msg.msg_flags & MSG_TRUNC)
512 {
513 DBG1(DBG_CFG, "receive buffer too small, RADIUS packet discarded");
514 continue;
515 }
516 source = host_create_from_sockaddr((sockaddr_t*)&src);
517 DBG2(DBG_CFG, "received RADIUS packet from %#H", source);
518 DBG3(DBG_CFG, "%b", buffer, bytes_read);
519 request = radius_message_parse(chunk_create(buffer, bytes_read));
520 if (request)
521 {
522 DBG1(DBG_CFG, "received RADIUS %N from client '%H'",
523 radius_message_code_names, request->get_code(request), source);
524
525 if (request->verify(request, NULL, this->secret, this->hasher,
526 this->signer))
527 {
528 process_eap(this, request, source);
529 }
530 request->destroy(request);
531
532 }
533 else
534 {
535 DBG1(DBG_CFG, "received invalid RADIUS message, ignored");
536 }
537 source->destroy(source);
538 }
539 return JOB_REQUEUE_FAIR;
540 }
541
542 METHOD(tnc_pdp_t, destroy, void,
543 private_tnc_pdp_t *this)
544 {
545 if (this->ipv4)
546 {
547 close(this->ipv4);
548 }
549 if (this->ipv6)
550 {
551 close(this->ipv6);
552 }
553 DESTROY_IF(this->server);
554 DESTROY_IF(this->signer);
555 DESTROY_IF(this->hasher);
556 DESTROY_IF(this->rng);
557 DESTROY_IF(this->connections);
558 free(this);
559 }
560
561 /*
562 * see header file
563 */
564 tnc_pdp_t *tnc_pdp_create(u_int16_t port)
565 {
566 private_tnc_pdp_t *this;
567 char *secret, *server, *eap_type_str;
568
569 INIT(this,
570 .public = {
571 .destroy = _destroy,
572 },
573 .ipv4 = open_socket(AF_INET, port),
574 .ipv6 = open_socket(AF_INET6, port),
575 .hasher = lib->crypto->create_hasher(lib->crypto, HASH_MD5),
576 .signer = lib->crypto->create_signer(lib->crypto, AUTH_HMAC_MD5_128),
577 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
578 .connections = tnc_pdp_connections_create(),
579 );
580
581 if (!this->hasher || !this->signer || !this->rng)
582 {
583 DBG1(DBG_CFG, "RADIUS initialization failed, HMAC/MD5/RNG required");
584 destroy(this);
585 return NULL;
586 }
587 if (!this->ipv4 && !this->ipv6)
588 {
589 DBG1(DBG_NET, "could not create any RADIUS sockets");
590 destroy(this);
591 return NULL;
592 }
593 if (!this->ipv4)
594 {
595 DBG1(DBG_NET, "could not open IPv4 RADIUS socket, IPv4 disabled");
596 }
597 if (!this->ipv6)
598 {
599 DBG1(DBG_NET, "could not open IPv6 RADIUS socket, IPv6 disabled");
600 }
601
602 server = lib->settings->get_str(lib->settings,
603 "%s.plugins.tnc-pdp.server", NULL, charon->name);
604 if (!server)
605 {
606 DBG1(DBG_CFG, "missing PDP server name, PDP disabled");
607 destroy(this);
608 return NULL;
609 }
610 this->server = identification_create_from_string(server);
611
612 secret = lib->settings->get_str(lib->settings,
613 "%s.plugins.tnc-pdp.secret", NULL, charon->name);
614 if (!secret)
615 {
616 DBG1(DBG_CFG, "missing RADIUS secret, PDP disabled");
617 destroy(this);
618 return NULL;
619 }
620 this->secret = chunk_create(secret, strlen(secret));
621 this->signer->set_key(this->signer, this->secret);
622
623 eap_type_str = lib->settings->get_str(lib->settings,
624 "%s.plugins.tnc-pdp.method", "ttls", charon->name);
625 this->type = eap_type_from_string(eap_type_str);
626 if (this->type == 0)
627 {
628 DBG1(DBG_CFG, "unrecognized eap method \"%s\"", eap_type_str);
629 destroy(this);
630 return NULL;
631 }
632 DBG1(DBG_IKE, "eap method %N selected", eap_type_names, this->type);
633
634 lib->processor->queue_job(lib->processor,
635 (job_t*)callback_job_create_with_prio((callback_job_cb_t)receive, this,
636 NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
637
638 return &this->public;
639 }
640