6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
21 * Copyright 2003 Alan DeKok <aland@freeradius.org>
22 * Copyright 2006 The FreeRADIUS server project
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/process.h>
30 #include <freeradius-devel/rad_assert.h>
32 #ifdef HAVE_SYS_STAT_H
37 #ifdef HAVE_OPENSSL_RAND_H
38 #include <openssl/rand.h>
41 #ifdef HAVE_OPENSSL_OCSP_H
42 #include <openssl/ocsp.h>
46 #define PTHREAD_MUTEX_LOCK pthread_mutex_lock
47 #define PTHREAD_MUTEX_UNLOCK pthread_mutex_unlock
49 #define PTHREAD_MUTEX_LOCK(_x)
50 #define PTHREAD_MUTEX_UNLOCK(_x)
53 static void dump_hex(const char *msg, const uint8_t *data, size_t data_len)
57 if (debug_flag < 3) return;
59 printf("%s %d\n", msg, (int) data_len);
60 if (data_len > 256) data_len = 256;
62 for (i = 0; i < data_len; i++) {
63 if ((i & 0x0f) == 0x00) printf ("%02x: ", (unsigned int) i);
64 printf("%02x ", data[i]);
65 if ((i & 0x0f) == 0x0f) printf ("\n");
71 static void tls_socket_close(rad_listen_t *listener)
73 listen_socket_t *sock = listener->data;
75 listener->status = RAD_LISTEN_STATUS_REMOVE_FD;
76 listener->tls = NULL; /* parent owns this! */
80 * Decrement the number of connections.
82 if (sock->parent->limit.num_connections > 0) {
83 sock->parent->limit.num_connections--;
85 if (sock->client->limit.num_connections > 0) {
86 sock->client->limit.num_connections--;
91 * Tell the event handler that an FD has disappeared.
93 DEBUG("Client has closed connection");
94 event_new_fd(listener);
97 * Do NOT free the listener here. It's in use by
98 * a request, and will need to hang around until
99 * all of the requests are done.
101 * It is instead free'd in remove_from_request_hash()
105 static int tls_socket_write(rad_listen_t *listener, REQUEST *request)
109 listen_socket_t *sock = listener->data;
111 p = sock->ssn->dirty_out.data;
113 while (p < (sock->ssn->dirty_out.data + sock->ssn->dirty_out.used)) {
114 RDEBUG3("Writing to socket %d", request->packet->sockfd);
115 rcode = write(request->packet->sockfd, p,
116 (sock->ssn->dirty_out.data + sock->ssn->dirty_out.used) - p);
118 RDEBUG("Error writing to TLS socket: %s", strerror(errno));
120 tls_socket_close(listener);
126 sock->ssn->dirty_out.used = 0;
132 static int tls_socket_recv(rad_listen_t *listener)
134 int doing_init = FALSE;
136 RADIUS_PACKET *packet;
138 listen_socket_t *sock = listener->data;
139 fr_tls_status_t status;
140 RADCLIENT *client = sock->client;
143 sock->packet = rad_alloc(0);
144 if (!sock->packet) return 0;
146 sock->packet->sockfd = listener->fd;
147 sock->packet->src_ipaddr = sock->other_ipaddr;
148 sock->packet->src_port = sock->other_port;
149 sock->packet->dst_ipaddr = sock->my_ipaddr;
150 sock->packet->dst_port = sock->my_port;
152 if (sock->request) sock->request->packet = sock->packet;
156 * Allocate a REQUEST for debugging.
158 if (!sock->request) {
159 sock->request = request = request_alloc();
160 if (!sock->request) {
161 radlog(L_ERR, "Out of memory");
165 rad_assert(request->packet == NULL);
166 rad_assert(sock->packet != NULL);
167 request->packet = sock->packet;
169 request->component = "<core>";
170 request->component = "<tls-connect>";
173 * Not sure if we should do this on every packet...
175 request->reply = rad_alloc(0);
176 if (!request->reply) return 0;
178 request->options = RAD_REQUEST_OPTION_DEBUG2;
180 rad_assert(sock->ssn == NULL);
182 sock->ssn = tls_new_session(listener->tls, sock->request,
183 listener->tls->require_client_cert);
185 request_free(&sock->request);
190 SSL_set_ex_data(sock->ssn->ssl, FR_TLS_EX_INDEX_REQUEST, (void *)request);
191 SSL_set_ex_data(sock->ssn->ssl, FR_TLS_EX_INDEX_CERTS, (void *)&request->packet->vps);
196 rad_assert(sock->request != NULL);
197 rad_assert(sock->request->packet != NULL);
198 rad_assert(sock->packet != NULL);
199 rad_assert(sock->ssn != NULL);
201 request = sock->request;
203 RDEBUG3("Reading from socket %d", request->packet->sockfd);
204 PTHREAD_MUTEX_LOCK(&sock->mutex);
205 rcode = read(request->packet->sockfd,
206 sock->ssn->dirty_in.data,
207 sizeof(sock->ssn->dirty_in.data));
208 if ((rcode < 0) && (errno == ECONNRESET)) {
210 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
211 tls_socket_close(listener);
216 RDEBUG("Error reading TLS socket: %s", strerror(errno));
221 * Normal socket close.
223 if (rcode == 0) goto do_close;
225 sock->ssn->dirty_in.used = rcode;
226 memset(sock->ssn->dirty_in.data + sock->ssn->dirty_in.used,
229 dump_hex("READ FROM SSL", sock->ssn->dirty_in.data, sock->ssn->dirty_in.used);
232 * Catch attempts to use non-SSL.
234 if (doing_init && (sock->ssn->dirty_in.data[0] != handshake)) {
235 RDEBUG("Non-TLS data sent to TLS socket: closing");
240 * Skip ahead to reading application data.
242 if (SSL_is_init_finished(sock->ssn->ssl)) goto app;
244 if (!tls_handshake_recv(request, sock->ssn)) {
245 RDEBUG("FAILED in TLS handshake receive");
249 if (sock->ssn->dirty_out.used > 0) {
250 tls_socket_write(listener, request);
251 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
257 * FIXME: Run the packet through a virtual server in
258 * order to see if we like the certificate presented by
262 status = tls_application_data(sock->ssn, request);
263 RDEBUG("Application data status %d", status);
265 if (status == FR_TLS_MORE_FRAGMENTS) {
266 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
270 if (sock->ssn->clean_out.used == 0) {
271 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
275 dump_hex("TUNNELED DATA", sock->ssn->clean_out.data, sock->ssn->clean_out.used);
278 * If the packet is a complete RADIUS packet, return it to
279 * the caller. Otherwise...
281 if ((sock->ssn->clean_out.used < 20) ||
282 (((sock->ssn->clean_out.data[2] << 8) | sock->ssn->clean_out.data[3]) != (int) sock->ssn->clean_out.used)) {
283 RDEBUG("Received bad packet: Length %d contents %d",
284 sock->ssn->clean_out.used,
285 (sock->ssn->clean_out.data[2] << 8) | sock->ssn->clean_out.data[3]);
289 packet = sock->packet;
290 packet->data = rad_malloc(sock->ssn->clean_out.used);
291 packet->data_len = sock->ssn->clean_out.used;
292 sock->ssn->record_minus(&sock->ssn->clean_out, packet->data, packet->data_len);
294 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
296 if (!rad_packet_ok(packet, 0)) {
297 RDEBUG("Received bad packet: %s", fr_strerror());
298 tls_socket_close(listener);
299 return 0; /* do_close unlocks the mutex */
303 * Copied from src/lib/radius.c, rad_recv();
306 char host_ipaddr[128];
308 if ((packet->code > 0) && (packet->code < FR_MAX_PACKET_CODE)) {
309 RDEBUG("tls_recv: %s packet from host %s port %d, id=%d, length=%d",
310 fr_packet_codes[packet->code],
311 inet_ntop(packet->src_ipaddr.af,
312 &packet->src_ipaddr.ipaddr,
313 host_ipaddr, sizeof(host_ipaddr)),
315 packet->id, (int) packet->data_len);
317 RDEBUG("tls_recv: Packet from host %s port %d code=%d, id=%d, length=%d",
318 inet_ntop(packet->src_ipaddr.af,
319 &packet->src_ipaddr.ipaddr,
320 host_ipaddr, sizeof(host_ipaddr)),
323 packet->id, (int) packet->data_len);
327 FR_STATS_INC(auth, total_requests);
333 int dual_tls_recv(rad_listen_t *listener)
335 RADIUS_PACKET *packet;
337 RAD_REQUEST_FUNP fun = NULL;
338 listen_socket_t *sock = listener->data;
339 RADCLIENT *client = sock->client;
341 if (!tls_socket_recv(listener)) {
345 rad_assert(sock->request != NULL);
346 rad_assert(sock->request->packet != NULL);
347 rad_assert(sock->packet != NULL);
348 rad_assert(sock->ssn != NULL);
350 request = sock->request;
351 packet = sock->packet;
354 * Some sanity checks, based on the packet code.
356 switch(packet->code) {
357 case PW_AUTHENTICATION_REQUEST:
358 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
359 FR_STATS_INC(auth, total_requests);
360 fun = rad_authenticate;
363 case PW_ACCOUNTING_REQUEST:
364 if (listener->type != RAD_LISTEN_ACCT) goto bad_packet;
365 FR_STATS_INC(acct, total_requests);
366 fun = rad_accounting;
369 case PW_STATUS_SERVER:
370 if (!mainconfig.status_server) {
371 FR_STATS_INC(auth, total_unknown_types);
372 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
373 rad_free(&sock->packet);
374 request->packet = NULL;
377 fun = rad_status_server;
382 FR_STATS_INC(auth, total_unknown_types);
384 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
385 packet->code, client->shortname, packet->src_port);
386 rad_free(&sock->packet);
387 request->packet = NULL;
389 } /* switch over packet types */
391 if (!request_receive(listener, packet, client, fun)) {
392 FR_STATS_INC(auth, total_packets_dropped);
393 rad_free(&sock->packet);
394 request->packet = NULL;
398 sock->packet = NULL; /* we have no need for more partial reads */
399 request->packet = NULL;
406 * Send a response packet
408 int dual_tls_send(rad_listen_t *listener, REQUEST *request)
410 listen_socket_t *sock = listener->data;
412 rad_assert(request->listener == listener);
413 rad_assert(listener->send == dual_tls_send);
416 * Accounting reject's are silently dropped.
418 * We do it here to avoid polluting the rest of the
419 * code with this knowledge
421 if (request->reply->code == 0) return 0;
426 if (rad_encode(request->reply, request->packet,
427 request->client->secret) < 0) {
428 RDEBUG("Failed encoding packet: %s", fr_strerror());
435 if (rad_sign(request->reply, request->packet,
436 request->client->secret) < 0) {
437 RDEBUG("Failed signing packet: %s", fr_strerror());
441 PTHREAD_MUTEX_LOCK(&sock->mutex);
443 * Write the packet to the SSL buffers.
445 sock->ssn->record_plus(&sock->ssn->clean_in,
446 request->reply->data, request->reply->data_len);
449 * Do SSL magic to get encrypted data.
451 tls_handshake_send(request, sock->ssn);
454 * And finally write the data to the socket.
456 if (sock->ssn->dirty_out.used > 0) {
457 dump_hex("WRITE TO SSL", sock->ssn->dirty_out.data, sock->ssn->dirty_out.used);
459 tls_socket_write(listener, request);
461 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
467 int proxy_tls_recv(rad_listen_t *listener)
471 listen_socket_t *sock = listener->data;
473 RADIUS_PACKET *packet;
476 if (!sock->data) sock->data = rad_malloc(sock->ssn->offset);
479 DEBUG3("Proxy SSL socket has data to read");
480 PTHREAD_MUTEX_LOCK(&sock->mutex);
482 rcode = SSL_read(sock->ssn->ssl, data, 4);
484 int err = SSL_get_error(sock->ssn->ssl, rcode);
486 case SSL_ERROR_WANT_READ:
487 case SSL_ERROR_WANT_WRITE:
490 case SSL_ERROR_ZERO_RETURN:
491 /* remote end sent close_notify, send one back */
492 SSL_shutdown(sock->ssn->ssl);
494 case SSL_ERROR_SYSCALL:
496 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
497 tls_socket_close(listener);
501 while ((err = ERR_get_error())) {
502 DEBUG("proxy recv says %s",
503 ERR_error_string(err, NULL));
510 length = (data[2] << 8) | data[3];
511 DEBUG3("Proxy received header saying we have a packet of %u bytes",
512 (unsigned int) length);
514 if (length > sock->ssn->offset) {
516 "Received packet will be too large! Set \"fragment_size=%u\"",
517 (data[2] << 8) | data[3]);
521 rcode = SSL_read(sock->ssn->ssl, data + 4, length);
523 switch (SSL_get_error(sock->ssn->ssl, rcode)) {
524 case SSL_ERROR_WANT_READ:
525 case SSL_ERROR_WANT_WRITE:
528 case SSL_ERROR_ZERO_RETURN:
529 /* remote end sent close_notify, send one back */
530 SSL_shutdown(sock->ssn->ssl);
536 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
538 packet = rad_alloc(0);
539 packet->sockfd = listener->fd;
540 packet->src_ipaddr = sock->other_ipaddr;
541 packet->src_port = sock->other_port;
542 packet->dst_ipaddr = sock->my_ipaddr;
543 packet->dst_port = sock->my_port;
544 packet->code = data[0];
545 packet->id = data[1];
546 packet->data_len = length;
547 packet->data = rad_malloc(packet->data_len);
548 memcpy(packet->data, data, packet->data_len);
549 memcpy(packet->vector, packet->data + 4, 16);
552 * FIXME: Client MIB updates?
554 switch(packet->code) {
555 case PW_AUTHENTICATION_ACK:
556 case PW_ACCESS_CHALLENGE:
557 case PW_AUTHENTICATION_REJECT:
560 #ifdef WITH_ACCOUNTING
561 case PW_ACCOUNTING_RESPONSE:
567 * FIXME: Update MIB for packet types?
569 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
570 "from home server %s port %d - ID %d : IGNORED",
572 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
573 packet->src_port, packet->id);
578 if (!request_proxy_reply(packet)) {
586 int proxy_tls_send(rad_listen_t *listener, REQUEST *request)
589 listen_socket_t *sock = listener->data;
592 * Normal proxying calls us with the data already
593 * encoded. The "ping home server" code does not. So,
594 * if there's no packet, encode it here.
596 if (!request->proxy->data) {
597 request->proxy_listener->encode(request->proxy_listener,
601 DEBUG3("Proxy is writing %u bytes to SSL",
602 (unsigned int) request->proxy->data_len);
603 PTHREAD_MUTEX_LOCK(&sock->mutex);
604 while ((rcode = SSL_write(sock->ssn->ssl, request->proxy->data,
605 request->proxy->data_len)) < 0) {
607 while ((err = ERR_get_error())) {
608 DEBUG("proxy SSL_write says %s",
609 ERR_error_string(err, NULL));
611 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
612 tls_socket_close(listener);
615 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
620 #endif /* WITH_TLS */