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