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
26 USES_APPLE_DEPRECATED_API /* OpenSSL API has been deprecated by Apple */
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(char const *msg, uint8_t const *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 SSL_shutdown(sock->ssn->ssl);
77 listener->status = RAD_LISTEN_STATUS_EOL;
78 listener->tls = NULL; /* parent owns this! */
82 * Decrement the number of connections.
84 if (sock->parent->limit.num_connections > 0) {
85 sock->parent->limit.num_connections--;
87 if (sock->client->limit.num_connections > 0) {
88 sock->client->limit.num_connections--;
93 * Tell the event handler that an FD has disappeared.
95 event_new_fd(listener);
98 * Do NOT free the listener here. It's in use by
99 * a request, and will need to hang around until
100 * all of the requests are done.
102 * It is instead free'd in remove_from_request_hash()
106 static int CC_HINT(nonnull) tls_socket_write(rad_listen_t *listener, REQUEST *request)
110 listen_socket_t *sock = listener->data;
112 p = sock->ssn->dirty_out.data;
114 while (p < (sock->ssn->dirty_out.data + sock->ssn->dirty_out.used)) {
115 RDEBUG3("Writing to socket %d", request->packet->sockfd);
116 rcode = write(request->packet->sockfd, p,
117 (sock->ssn->dirty_out.data + sock->ssn->dirty_out.used) - p);
119 RDEBUG("Error writing to TLS socket: %s", fr_syserror(errno));
121 tls_socket_close(listener);
127 sock->ssn->dirty_out.used = 0;
133 static int tls_socket_recv(rad_listen_t *listener)
135 bool doing_init = false;
137 RADIUS_PACKET *packet;
139 listen_socket_t *sock = listener->data;
140 fr_tls_status_t status;
141 RADCLIENT *client = sock->client;
144 sock->packet = rad_alloc(sock, 0);
145 if (!sock->packet) return 0;
147 sock->packet->sockfd = listener->fd;
148 sock->packet->src_ipaddr = sock->other_ipaddr;
149 sock->packet->src_port = sock->other_port;
150 sock->packet->dst_ipaddr = sock->my_ipaddr;
151 sock->packet->dst_port = sock->my_port;
154 (void) talloc_steal(sock->request, sock->packet);
155 sock->request->packet = sock->packet;
160 * Allocate a REQUEST for debugging.
162 if (!sock->request) {
163 sock->request = request = request_alloc(NULL);
164 if (!sock->request) {
165 ERROR("Out of memory");
169 rad_assert(request->packet == NULL);
170 rad_assert(sock->packet != NULL);
171 request->packet = sock->packet;
173 request->component = "<core>";
174 request->component = "<tls-connect>";
177 * Not sure if we should do this on every packet...
179 request->reply = rad_alloc(request, 0);
180 if (!request->reply) return 0;
182 rad_assert(sock->ssn == NULL);
184 sock->ssn = tls_new_session(listener->tls, sock->request,
185 listener->tls->require_client_cert);
187 request_free(&sock->request);
192 (void) talloc_steal(sock, sock->ssn);
193 SSL_set_ex_data(sock->ssn->ssl, FR_TLS_EX_INDEX_REQUEST, (void *)request);
194 SSL_set_ex_data(sock->ssn->ssl, FR_TLS_EX_INDEX_CERTS, (void *)&request->packet->vps);
195 SSL_set_ex_data(sock->ssn->ssl, FR_TLS_EX_INDEX_TALLOC, sock->parent);
200 rad_assert(sock->request != NULL);
201 rad_assert(sock->request->packet != NULL);
202 rad_assert(sock->packet != NULL);
203 rad_assert(sock->ssn != NULL);
205 request = sock->request;
207 RDEBUG3("Reading from socket %d", request->packet->sockfd);
208 PTHREAD_MUTEX_LOCK(&sock->mutex);
209 rcode = read(request->packet->sockfd,
210 sock->ssn->dirty_in.data,
211 sizeof(sock->ssn->dirty_in.data));
212 if ((rcode < 0) && (errno == ECONNRESET)) {
214 DEBUG("Closing TLS socket from client port %u", sock->other_port);
215 tls_socket_close(listener);
216 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
221 RDEBUG("Error reading TLS socket: %s", fr_syserror(errno));
226 * Normal socket close.
228 if (rcode == 0) goto do_close;
230 sock->ssn->dirty_in.used = rcode;
232 dump_hex("READ FROM SSL", sock->ssn->dirty_in.data, sock->ssn->dirty_in.used);
235 * Catch attempts to use non-SSL.
237 if (doing_init && (sock->ssn->dirty_in.data[0] != handshake)) {
238 RDEBUG("Non-TLS data sent to TLS socket: closing");
243 * Skip ahead to reading application data.
245 if (SSL_is_init_finished(sock->ssn->ssl)) goto app;
247 if (!tls_handshake_recv(request, sock->ssn)) {
248 RDEBUG("FAILED in TLS handshake receive");
252 if (sock->ssn->dirty_out.used > 0) {
253 tls_socket_write(listener, request);
254 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
260 * FIXME: Run the packet through a virtual server in
261 * order to see if we like the certificate presented by
265 status = tls_application_data(sock->ssn, request);
266 RDEBUG("Application data status %d", status);
268 if (status == FR_TLS_MORE_FRAGMENTS) {
269 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
273 if (sock->ssn->clean_out.used == 0) {
274 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
278 dump_hex("TUNNELED DATA", sock->ssn->clean_out.data, sock->ssn->clean_out.used);
281 * If the packet is a complete RADIUS packet, return it to
282 * the caller. Otherwise...
284 if ((sock->ssn->clean_out.used < 20) ||
285 (((sock->ssn->clean_out.data[2] << 8) | sock->ssn->clean_out.data[3]) != (int) sock->ssn->clean_out.used)) {
286 RDEBUG("Received bad packet: Length %d contents %d",
287 sock->ssn->clean_out.used,
288 (sock->ssn->clean_out.data[2] << 8) | sock->ssn->clean_out.data[3]);
292 packet = sock->packet;
293 packet->data = talloc_array(packet, uint8_t, sock->ssn->clean_out.used);
294 packet->data_len = sock->ssn->clean_out.used;
295 sock->ssn->record_minus(&sock->ssn->clean_out, packet->data, packet->data_len);
297 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
299 if (!rad_packet_ok(packet, 0, NULL)) {
300 RDEBUG("Received bad packet: %s", fr_strerror());
301 DEBUG("Closing TLS socket from client");
302 PTHREAD_MUTEX_LOCK(&sock->mutex);
303 tls_socket_close(listener);
304 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
305 return 0; /* do_close unlocks the mutex */
309 * Copied from src/lib/radius.c, rad_recv();
312 char host_ipaddr[128];
314 if (is_radius_code(packet->code)) {
315 RDEBUG("tls_recv: %s packet from host %s port %d, id=%d, length=%d",
316 fr_packet_codes[packet->code],
317 inet_ntop(packet->src_ipaddr.af,
318 &packet->src_ipaddr.ipaddr,
319 host_ipaddr, sizeof(host_ipaddr)),
321 packet->id, (int) packet->data_len);
323 RDEBUG("tls_recv: Packet from host %s port %d code=%d, id=%d, length=%d",
324 inet_ntop(packet->src_ipaddr.af,
325 &packet->src_ipaddr.ipaddr,
326 host_ipaddr, sizeof(host_ipaddr)),
329 packet->id, (int) packet->data_len);
333 FR_STATS_INC(auth, total_requests);
339 int dual_tls_recv(rad_listen_t *listener)
341 RADIUS_PACKET *packet;
343 RAD_REQUEST_FUNP fun = NULL;
344 listen_socket_t *sock = listener->data;
345 RADCLIENT *client = sock->client;
347 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
349 if (!tls_socket_recv(listener)) {
353 rad_assert(sock->request != NULL);
354 rad_assert(sock->request->packet != NULL);
355 rad_assert(sock->packet != NULL);
356 rad_assert(sock->ssn != NULL);
357 rad_assert(client != NULL);
359 request = sock->request;
360 packet = sock->packet;
363 * Some sanity checks, based on the packet code.
365 * "auth+acct" are marked as "auth", with the "dual" flag
368 switch(packet->code) {
369 case PW_CODE_AUTHENTICATION_REQUEST:
370 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
371 FR_STATS_INC(auth, total_requests);
372 fun = rad_authenticate;
375 #ifdef WITH_ACCOUNTING
376 case PW_CODE_ACCOUNTING_REQUEST:
377 if (listener->type != RAD_LISTEN_ACCT) {
379 * Allow auth + dual. Disallow
382 if (!((listener->type == RAD_LISTEN_AUTH) &&
387 FR_STATS_INC(acct, total_requests);
388 fun = rad_accounting;
392 case PW_CODE_STATUS_SERVER:
393 if (!mainconfig.status_server) {
394 FR_STATS_INC(auth, total_unknown_types);
395 WDEBUG("Ignoring Status-Server request due to security configuration");
396 rad_free(&sock->packet);
397 request->packet = NULL;
400 fun = rad_status_server;
405 FR_STATS_INC(auth, total_unknown_types);
407 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
408 packet->code, client->shortname, packet->src_port);
409 rad_free(&sock->packet);
410 request->packet = NULL;
412 } /* switch over packet types */
414 if (!request_receive(listener, packet, client, fun)) {
415 FR_STATS_INC(auth, total_packets_dropped);
416 rad_free(&sock->packet);
417 request->packet = NULL;
421 sock->packet = NULL; /* we have no need for more partial reads */
422 request->packet = NULL;
429 * Send a response packet
431 int dual_tls_send(rad_listen_t *listener, REQUEST *request)
433 listen_socket_t *sock = listener->data;
435 VERIFY_REQUEST(request);
437 rad_assert(request->listener == listener);
438 rad_assert(listener->send == dual_tls_send);
440 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
443 * Accounting reject's are silently dropped.
445 * We do it here to avoid polluting the rest of the
446 * code with this knowledge
448 if (request->reply->code == 0) return 0;
453 if (rad_encode(request->reply, request->packet,
454 request->client->secret) < 0) {
455 RERROR("Failed encoding packet: %s", fr_strerror());
462 if (rad_sign(request->reply, request->packet,
463 request->client->secret) < 0) {
464 RERROR("Failed signing packet: %s", fr_strerror());
468 PTHREAD_MUTEX_LOCK(&sock->mutex);
470 * Write the packet to the SSL buffers.
472 sock->ssn->record_plus(&sock->ssn->clean_in,
473 request->reply->data, request->reply->data_len);
476 * Do SSL magic to get encrypted data.
478 tls_handshake_send(request, sock->ssn);
481 * And finally write the data to the socket.
483 if (sock->ssn->dirty_out.used > 0) {
484 dump_hex("WRITE TO SSL", sock->ssn->dirty_out.data, sock->ssn->dirty_out.used);
486 tls_socket_write(listener, request);
488 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
496 * Read from the SSL socket. Safe with either blocking or
497 * non-blocking IO. This level of complexity is probably not
498 * necessary, as each packet gets put into one SSL application
499 * record. When SSL has a full record, we should be able to read
500 * the entire packet via one SSL_read().
502 * When SSL has a partial record, SSL_read() will return
503 * WANT_READ or WANT_WRITE, and zero application data.
505 * Called with the mutex held.
507 static ssize_t proxy_tls_read(rad_listen_t *listener)
512 listen_socket_t *sock = listener->data;
515 * Get the maximum size of data to receive.
517 if (!sock->data) sock->data = talloc_array(sock, uint8_t,
522 if (sock->partial < 4) {
523 rcode = SSL_read(sock->ssn->ssl, data + sock->partial,
526 int err = SSL_get_error(sock->ssn->ssl, rcode);
528 case SSL_ERROR_WANT_READ:
529 case SSL_ERROR_WANT_WRITE:
530 return 0; /* do some more work later */
532 case SSL_ERROR_ZERO_RETURN:
533 /* remote end sent close_notify, send one back */
534 SSL_shutdown(sock->ssn->ssl);
536 case SSL_ERROR_SYSCALL:
541 while ((err = ERR_get_error())) {
542 DEBUG("proxy recv says %s",
543 ERR_error_string(err, NULL));
550 sock->partial = rcode;
551 } /* try reading the packet header */
553 if (sock->partial < 4) return 0; /* read more data */
555 length = (data[2] << 8) | data[3];
558 * Do these checks only once, when we read the header.
560 if (sock->partial == 4) {
561 DEBUG3("Proxy received header saying we have a packet of %u bytes",
562 (unsigned int) length);
565 * FIXME: allocate a RADIUS_PACKET, and set
566 * "data" to be as large as necessary.
568 if (length > sock->ssn->offset) {
569 INFO("Received packet will be too large! Set \"fragment_size = %u\"",
570 (data[2] << 8) | data[3]);
576 * Try to read some more.
578 if (sock->partial < length) {
579 rcode = SSL_read(sock->ssn->ssl, data + sock->partial,
580 length - sock->partial);
582 switch (SSL_get_error(sock->ssn->ssl, rcode)) {
583 case SSL_ERROR_WANT_READ:
584 case SSL_ERROR_WANT_WRITE:
587 case SSL_ERROR_ZERO_RETURN:
588 /* remote end sent close_notify, send one back */
589 SSL_shutdown(sock->ssn->ssl);
596 sock->partial += rcode;
600 * If we're not done, say so.
602 * Otherwise, reset the partially read data flag, and say
605 if (sock->partial < length) {
609 sock->partial = 0; /* we've now read the packet */
614 int proxy_tls_recv(rad_listen_t *listener)
616 listen_socket_t *sock = listener->data;
618 RADIUS_PACKET *packet;
622 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
624 DEBUG3("Proxy SSL socket has data to read");
625 PTHREAD_MUTEX_LOCK(&sock->mutex);
626 data_len = proxy_tls_read(listener);
627 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
630 DEBUG("Closing TLS socket to home server");
631 PTHREAD_MUTEX_LOCK(&sock->mutex);
632 tls_socket_close(listener);
633 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
637 if (data_len == 0) return 0; /* not done yet */
641 packet = rad_alloc(sock, 0);
642 packet->sockfd = listener->fd;
643 packet->src_ipaddr = sock->other_ipaddr;
644 packet->src_port = sock->other_port;
645 packet->dst_ipaddr = sock->my_ipaddr;
646 packet->dst_port = sock->my_port;
647 packet->code = data[0];
648 packet->id = data[1];
649 packet->data_len = data_len;
650 packet->data = talloc_array(packet, uint8_t, packet->data_len);
651 memcpy(packet->data, data, packet->data_len);
652 memcpy(packet->vector, packet->data + 4, 16);
655 * FIXME: Client MIB updates?
657 switch(packet->code) {
658 case PW_CODE_AUTHENTICATION_ACK:
659 case PW_CODE_ACCESS_CHALLENGE:
660 case PW_CODE_AUTHENTICATION_REJECT:
663 #ifdef WITH_ACCOUNTING
664 case PW_CODE_ACCOUNTING_RESPONSE:
670 * FIXME: Update MIB for packet types?
672 ERROR("Invalid packet code %d sent to a proxy port "
673 "from home server %s port %d - ID %d : IGNORED",
675 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
676 packet->src_port, packet->id);
681 if (!request_proxy_reply(packet)) {
690 int proxy_tls_send(rad_listen_t *listener, REQUEST *request)
693 listen_socket_t *sock = listener->data;
695 VERIFY_REQUEST(request);
697 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
700 * Normal proxying calls us with the data already
701 * encoded. The "ping home server" code does not. So,
702 * if there's no packet, encode it here.
704 if (!request->proxy->data) {
705 request->proxy_listener->encode(request->proxy_listener,
709 DEBUG3("Proxy is writing %u bytes to SSL",
710 (unsigned int) request->proxy->data_len);
711 PTHREAD_MUTEX_LOCK(&sock->mutex);
712 rcode = SSL_write(sock->ssn->ssl, request->proxy->data,
713 request->proxy->data_len);
717 err = ERR_get_error();
720 case SSL_ERROR_WANT_READ:
721 case SSL_ERROR_WANT_WRITE:
722 break; /* let someone else retry */
725 DEBUG("proxy SSL_write says %s",
726 ERR_error_string(err, NULL));
727 DEBUG("Closing TLS socket to home server");
728 tls_socket_close(listener);
729 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
733 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
737 #endif /* WITH_PROXY */
739 #endif /* WITH_TLS */