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
38 #ifdef HAVE_OPENSSL_RAND_H
39 #include <openssl/rand.h>
42 #ifdef HAVE_OPENSSL_OCSP_H
43 #include <openssl/ocsp.h>
47 #define PTHREAD_MUTEX_LOCK pthread_mutex_lock
48 #define PTHREAD_MUTEX_UNLOCK pthread_mutex_unlock
50 #define PTHREAD_MUTEX_LOCK(_x)
51 #define PTHREAD_MUTEX_UNLOCK(_x)
54 static void dump_hex(char const *msg, uint8_t const *data, size_t data_len)
58 if (rad_debug_lvl < 3) return;
60 printf("%s %d\n", msg, (int) data_len);
61 if (data_len > 256) data_len = 256;
63 for (i = 0; i < data_len; i++) {
64 if ((i & 0x0f) == 0x00) printf ("%02x: ", (unsigned int) i);
65 printf("%02x ", data[i]);
66 if ((i & 0x0f) == 0x0f) printf ("\n");
72 static void tls_socket_close(rad_listen_t *listener)
74 listen_socket_t *sock = listener->data;
76 SSL_shutdown(sock->ssn->ssl);
78 listener->status = RAD_LISTEN_STATUS_EOL;
79 listener->tls = NULL; /* parent owns this! */
82 * Tell the event handler that an FD has disappeared.
84 DEBUG("Client has closed connection");
85 radius_update_listener(listener);
88 * Do NOT free the listener here. It's in use by
89 * a request, and will need to hang around until
90 * all of the requests are done.
92 * It is instead free'd in remove_from_request_hash()
96 static int CC_HINT(nonnull) tls_socket_write(rad_listen_t *listener, REQUEST *request)
100 listen_socket_t *sock = listener->data;
102 p = sock->ssn->dirty_out.data;
104 while (p < (sock->ssn->dirty_out.data + sock->ssn->dirty_out.used)) {
105 RDEBUG3("Writing to socket %d", request->packet->sockfd);
106 rcode = write(request->packet->sockfd, p,
107 (sock->ssn->dirty_out.data + sock->ssn->dirty_out.used) - p);
109 RDEBUG("Error writing to TLS socket: %s", fr_syserror(errno));
111 tls_socket_close(listener);
117 sock->ssn->dirty_out.used = 0;
123 static int tls_socket_recv(rad_listen_t *listener)
125 bool doing_init = false;
127 RADIUS_PACKET *packet;
129 listen_socket_t *sock = listener->data;
130 fr_tls_status_t status;
131 RADCLIENT *client = sock->client;
134 sock->packet = rad_alloc(sock, false);
135 if (!sock->packet) return 0;
137 sock->packet->sockfd = listener->fd;
138 sock->packet->src_ipaddr = sock->other_ipaddr;
139 sock->packet->src_port = sock->other_port;
140 sock->packet->dst_ipaddr = sock->my_ipaddr;
141 sock->packet->dst_port = sock->my_port;
143 if (sock->request) sock->request->packet = talloc_steal(sock->request, sock->packet);
147 * Allocate a REQUEST for debugging, and initialize the TLS session.
149 if (!sock->request) {
150 sock->request = request = request_alloc(sock);
151 if (!sock->request) {
152 ERROR("Out of memory");
156 rad_assert(request->packet == NULL);
157 rad_assert(sock->packet != NULL);
158 request->packet = talloc_steal(request, sock->packet);
160 request->component = "<core>";
161 request->component = "<tls-connect>";
163 request->reply = rad_alloc(request, false);
164 if (!request->reply) return 0;
166 rad_assert(sock->ssn == NULL);
168 sock->ssn = tls_new_session(sock, listener->tls, sock->request,
169 listener->tls->require_client_cert);
171 TALLOC_FREE(sock->request);
176 SSL_set_ex_data(sock->ssn->ssl, FR_TLS_EX_INDEX_REQUEST, (void *)request);
177 SSL_set_ex_data(sock->ssn->ssl, fr_tls_ex_index_certs, (void *) &sock->certs);
178 SSL_set_ex_data(sock->ssn->ssl, FR_TLS_EX_INDEX_TALLOC, sock->parent);
183 rad_assert(sock->request != NULL);
184 rad_assert(sock->request->packet != NULL);
185 rad_assert(sock->packet != NULL);
186 rad_assert(sock->ssn != NULL);
188 request = sock->request;
190 RDEBUG3("Reading from socket %d", request->packet->sockfd);
191 PTHREAD_MUTEX_LOCK(&sock->mutex);
192 rcode = read(request->packet->sockfd,
193 sock->ssn->dirty_in.data,
194 sizeof(sock->ssn->dirty_in.data));
195 if ((rcode < 0) && (errno == ECONNRESET)) {
197 DEBUG("Closing TLS socket from client port %u", sock->other_port);
198 tls_socket_close(listener);
199 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
204 RDEBUG("Error reading TLS socket: %s", fr_syserror(errno));
209 * Normal socket close.
211 if (rcode == 0) goto do_close;
213 sock->ssn->dirty_in.used = rcode;
215 dump_hex("READ FROM SSL", sock->ssn->dirty_in.data, sock->ssn->dirty_in.used);
218 * Catch attempts to use non-SSL.
220 if (doing_init && (sock->ssn->dirty_in.data[0] != handshake)) {
221 RDEBUG("Non-TLS data sent to TLS socket: closing");
226 * If we need to do more initialization, do that here.
228 if (!SSL_is_init_finished(sock->ssn->ssl)) {
229 if (!tls_handshake_recv(request, sock->ssn)) {
230 RDEBUG("FAILED in TLS handshake receive");
235 * More ACK data to send. Do so.
237 if (sock->ssn->dirty_out.used > 0) {
238 tls_socket_write(listener, request);
239 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
244 * FIXME: Run the request through a virtual
245 * server in order to see if we like the
246 * certificate presented by the client.
251 * Try to get application data.
253 status = tls_application_data(sock->ssn, request);
254 RDEBUG("Application data status %d", status);
256 if (status == FR_TLS_MORE_FRAGMENTS) {
257 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
261 if (sock->ssn->clean_out.used == 0) {
262 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
267 * We now have a bunch of application data.
269 dump_hex("TUNNELED DATA > ", sock->ssn->clean_out.data, sock->ssn->clean_out.used);
272 * If the packet is a complete RADIUS packet, return it to
273 * the caller. Otherwise...
275 if ((sock->ssn->clean_out.used < 20) ||
276 (((sock->ssn->clean_out.data[2] << 8) | sock->ssn->clean_out.data[3]) != (int) sock->ssn->clean_out.used)) {
277 RDEBUG("Received bad packet: Length %zd contents %d",
278 sock->ssn->clean_out.used,
279 (sock->ssn->clean_out.data[2] << 8) | sock->ssn->clean_out.data[3]);
283 packet = sock->packet;
284 packet->data = talloc_array(packet, uint8_t, sock->ssn->clean_out.used);
285 packet->data_len = sock->ssn->clean_out.used;
286 sock->ssn->record_minus(&sock->ssn->clean_out, packet->data, packet->data_len);
288 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
290 if (!rad_packet_ok(packet, 0, NULL)) {
291 if (DEBUG_ENABLED) ERROR("Receive - %s", fr_strerror());
292 DEBUG("Closing TLS socket from client");
293 PTHREAD_MUTEX_LOCK(&sock->mutex);
294 tls_socket_close(listener);
295 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
296 return 0; /* do_close unlocks the mutex */
300 * Copied from src/lib/radius.c, rad_recv();
303 char host_ipaddr[128];
305 if (is_radius_code(packet->code)) {
306 RDEBUG("tls_recv: %s packet from host %s port %d, id=%d, length=%d",
307 fr_packet_codes[packet->code],
308 inet_ntop(packet->src_ipaddr.af,
309 &packet->src_ipaddr.ipaddr,
310 host_ipaddr, sizeof(host_ipaddr)),
312 packet->id, (int) packet->data_len);
314 RDEBUG("tls_recv: Packet from host %s port %d code=%d, id=%d, length=%d",
315 inet_ntop(packet->src_ipaddr.af,
316 &packet->src_ipaddr.ipaddr,
317 host_ipaddr, sizeof(host_ipaddr)),
320 packet->id, (int) packet->data_len);
324 FR_STATS_INC(auth, total_requests);
327 * Re-parent the packet to nothing.
329 (void) talloc_steal(NULL, packet);
335 int dual_tls_recv(rad_listen_t *listener)
337 RADIUS_PACKET *packet;
338 RAD_REQUEST_FUNP fun = NULL;
339 listen_socket_t *sock = listener->data;
340 RADCLIENT *client = sock->client;
342 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
344 if (!tls_socket_recv(listener)) {
348 rad_assert(sock->packet != NULL);
349 rad_assert(sock->ssn != NULL);
350 rad_assert(client != NULL);
352 packet = talloc_steal(NULL, sock->packet);
356 * Some sanity checks, based on the packet code.
358 * "auth+acct" are marked as "auth", with the "dual" flag
361 switch (packet->code) {
362 case PW_CODE_ACCESS_REQUEST:
363 if (listener->type != RAD_LISTEN_AUTH) goto bad_packet;
364 FR_STATS_INC(auth, total_requests);
365 fun = rad_authenticate;
368 #ifdef WITH_ACCOUNTING
369 case PW_CODE_ACCOUNTING_REQUEST:
370 if (listener->type != RAD_LISTEN_ACCT) {
372 * Allow auth + dual. Disallow
375 if (!((listener->type == RAD_LISTEN_AUTH) &&
380 FR_STATS_INC(acct, total_requests);
381 fun = rad_accounting;
385 case PW_CODE_STATUS_SERVER:
386 if (!main_config.status_server) {
387 FR_STATS_INC(auth, total_unknown_types);
388 WARN("Ignoring Status-Server request due to security configuration");
392 fun = rad_status_server;
397 FR_STATS_INC(auth, total_unknown_types);
399 DEBUG("Invalid packet code %d sent from client %s port %d : IGNORED",
400 packet->code, client->shortname, packet->src_port);
403 } /* switch over packet types */
405 if (!request_receive(NULL, listener, packet, client, fun)) {
406 FR_STATS_INC(auth, total_packets_dropped);
416 * Send a response packet
418 int dual_tls_send(rad_listen_t *listener, REQUEST *request)
420 listen_socket_t *sock = listener->data;
422 VERIFY_REQUEST(request);
424 rad_assert(request->listener == listener);
425 rad_assert(listener->send == dual_tls_send);
427 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
430 * Accounting reject's are silently dropped.
432 * We do it here to avoid polluting the rest of the
433 * code with this knowledge
435 if (request->reply->code == 0) return 0;
440 if (rad_encode(request->reply, request->packet,
441 request->client->secret) < 0) {
442 RERROR("Failed encoding packet: %s", fr_strerror());
446 if (request->reply->data_len > (MAX_PACKET_LEN - 100)) {
447 RWARN("Packet is large, and possibly truncated - %zd vs max %d",
448 request->reply->data_len, MAX_PACKET_LEN);
454 if (rad_sign(request->reply, request->packet,
455 request->client->secret) < 0) {
456 RERROR("Failed signing packet: %s", fr_strerror());
460 PTHREAD_MUTEX_LOCK(&sock->mutex);
463 * Write the packet to the SSL buffers.
465 sock->ssn->record_plus(&sock->ssn->clean_in,
466 request->reply->data, request->reply->data_len);
468 dump_hex("TUNNELED DATA < ", sock->ssn->clean_in.data, sock->ssn->clean_in.used);
471 * Do SSL magic to get encrypted data.
473 tls_handshake_send(request, sock->ssn);
476 * And finally write the data to the socket.
478 if (sock->ssn->dirty_out.used > 0) {
479 dump_hex("WRITE TO SSL", sock->ssn->dirty_out.data, sock->ssn->dirty_out.used);
481 tls_socket_write(listener, request);
483 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
491 * Read from the SSL socket. Safe with either blocking or
492 * non-blocking IO. This level of complexity is probably not
493 * necessary, as each packet gets put into one SSL application
494 * record. When SSL has a full record, we should be able to read
495 * the entire packet via one SSL_read().
497 * When SSL has a partial record, SSL_read() will return
498 * WANT_READ or WANT_WRITE, and zero application data.
500 * Called with the mutex held.
502 static ssize_t proxy_tls_read(rad_listen_t *listener)
507 listen_socket_t *sock = listener->data;
510 * Get the maximum size of data to receive.
512 if (!sock->data) sock->data = talloc_array(sock, uint8_t,
517 if (sock->partial < 4) {
518 rcode = SSL_read(sock->ssn->ssl, data + sock->partial,
521 int err = SSL_get_error(sock->ssn->ssl, rcode);
523 case SSL_ERROR_WANT_READ:
524 case SSL_ERROR_WANT_WRITE:
525 return 0; /* do some more work later */
527 case SSL_ERROR_ZERO_RETURN:
528 /* remote end sent close_notify, send one back */
529 SSL_shutdown(sock->ssn->ssl);
531 case SSL_ERROR_SYSCALL:
536 tls_error_log(NULL, "Failed in proxy receive");
542 sock->partial = rcode;
543 } /* try reading the packet header */
545 if (sock->partial < 4) return 0; /* read more data */
547 length = (data[2] << 8) | data[3];
550 * Do these checks only once, when we read the header.
552 if (sock->partial == 4) {
553 DEBUG3("Proxy received header saying we have a packet of %u bytes",
554 (unsigned int) length);
557 * FIXME: allocate a RADIUS_PACKET, and set
558 * "data" to be as large as necessary.
560 if (length > sock->ssn->mtu) {
561 INFO("Received packet will be too large! Set \"fragment_size = %u\"",
562 (data[2] << 8) | data[3]);
568 * Try to read some more.
570 if (sock->partial < length) {
571 rcode = SSL_read(sock->ssn->ssl, data + sock->partial,
572 length - sock->partial);
574 switch (SSL_get_error(sock->ssn->ssl, rcode)) {
575 case SSL_ERROR_WANT_READ:
576 case SSL_ERROR_WANT_WRITE:
579 case SSL_ERROR_ZERO_RETURN:
580 /* remote end sent close_notify, send one back */
581 SSL_shutdown(sock->ssn->ssl);
588 sock->partial += rcode;
592 * If we're not done, say so.
594 * Otherwise, reset the partially read data flag, and say
597 if (sock->partial < length) {
601 sock->partial = 0; /* we've now read the packet */
606 int proxy_tls_recv(rad_listen_t *listener)
608 listen_socket_t *sock = listener->data;
610 RADIUS_PACKET *packet;
614 if (listener->status != RAD_LISTEN_STATUS_KNOWN) return 0;
616 DEBUG3("Proxy SSL socket has data to read");
617 PTHREAD_MUTEX_LOCK(&sock->mutex);
618 data_len = proxy_tls_read(listener);
619 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
622 DEBUG("Closing TLS socket to home server");
623 PTHREAD_MUTEX_LOCK(&sock->mutex);
624 tls_socket_close(listener);
625 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
629 if (data_len == 0) return 0; /* not done yet */
633 packet = rad_alloc(sock, false);
634 packet->sockfd = listener->fd;
635 packet->src_ipaddr = sock->other_ipaddr;
636 packet->src_port = sock->other_port;
637 packet->dst_ipaddr = sock->my_ipaddr;
638 packet->dst_port = sock->my_port;
639 packet->code = data[0];
640 packet->id = data[1];
641 packet->data_len = data_len;
642 packet->data = talloc_array(packet, uint8_t, packet->data_len);
643 memcpy(packet->data, data, packet->data_len);
644 memcpy(packet->vector, packet->data + 4, 16);
647 * FIXME: Client MIB updates?
649 switch (packet->code) {
650 case PW_CODE_ACCESS_ACCEPT:
651 case PW_CODE_ACCESS_CHALLENGE:
652 case PW_CODE_ACCESS_REJECT:
655 #ifdef WITH_ACCOUNTING
656 case PW_CODE_ACCOUNTING_RESPONSE:
662 * FIXME: Update MIB for packet types?
664 ERROR("Invalid packet code %d sent to a proxy port "
665 "from home server %s port %d - ID %d : IGNORED",
667 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
668 packet->src_port, packet->id);
673 if (!request_proxy_reply(packet)) {
682 int proxy_tls_send(rad_listen_t *listener, REQUEST *request)
685 listen_socket_t *sock = listener->data;
687 VERIFY_REQUEST(request);
689 if ((listener->status != RAD_LISTEN_STATUS_INIT) &&
690 (listener->status != RAD_LISTEN_STATUS_KNOWN)) return 0;
693 * Normal proxying calls us with the data already
694 * encoded. The "ping home server" code does not. So,
695 * if there's no packet, encode it here.
697 if (!request->proxy->data) {
698 request->proxy_listener->encode(request->proxy_listener,
702 DEBUG3("Proxy is writing %u bytes to SSL",
703 (unsigned int) request->proxy->data_len);
704 PTHREAD_MUTEX_LOCK(&sock->mutex);
705 rcode = SSL_write(sock->ssn->ssl, request->proxy->data,
706 request->proxy->data_len);
710 err = ERR_get_error();
713 case SSL_ERROR_WANT_READ:
714 case SSL_ERROR_WANT_WRITE:
715 break; /* let someone else retry */
718 tls_error_log(NULL, "Failed in proxy send");
719 DEBUG("Closing TLS socket to home server");
720 tls_socket_close(listener);
721 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
725 PTHREAD_MUTEX_UNLOCK(&sock->mutex);
729 #endif /* WITH_PROXY */
731 #endif /* WITH_TLS */
732 #endif /* WITH_TCP */