51#include <freeradius-devel/server/base.h>
53#include <freeradius-devel/tls/base.h>
54#include <freeradius-devel/tls/connection.h>
55#include <freeradius-devel/tls/dtls.h>
57#include <freeradius-devel/unlang/interpret.h>
59#include <freeradius-devel/bio/bio_priv.h>
60#include <freeradius-devel/bio/fd.h>
61#include <freeradius-devel/bio/tls.h>
127 if (!my->events || (my->write_wait == wait))
return 0;
134 my->write_wait = wait;
144 if (!my->events)
return;
151 my->write_wait =
false;
152 my->read_paused =
false;
163 if (this->priv_cb.write_resume) (void) this->priv_cb.write_resume(next);
175 my->conn->failed = reason;
176 my->conn->error = error;
181 if (my->cb.failed) my->cb.failed(&my->bio);
204 fr_bio_tls_t *my = talloc_get_type_abort(conn->uctx, fr_bio_tls_t);
212 if (my->write_blocked)
return 0;
218 slen = next->
write(next, my->packet_ctx,
data, size);
220 if ((
size_t) slen == size) {
234 my->write_blocked =
true;
249 if (slen ==
fr_bio_error(IO_WOULD_BLOCK)) errno = EAGAIN;
251 switch (fr_tls_connection_io_error(conn, slen)) {
252 case FR_TLS_CONNECTION_IO_RETRY:
255 case FR_TLS_CONNECTION_IO_BLOCKED:
256 my->write_blocked =
true;
263 case FR_TLS_CONNECTION_IO_OK:
264 case FR_TLS_CONNECTION_IO_EOF:
265 case FR_TLS_CONNECTION_IO_FATAL:
287 fr_bio_tls_t *my = talloc_get_type_abort(conn->uctx, fr_bio_tls_t);
294 my->clean_len =
fr_dbuff_used(&conn->tls_session->clean_out);
295 my->clean_offset = 0;
297 if (my->cb.connected) my->cb.connected(&my->bio);
307 fr_bio_tls_t *my = talloc_get_type_abort(conn->uctx, fr_bio_tls_t);
314 if (my->cb.failed) my->cb.failed(&my->bio);
326 fr_tls_session_t *tls_session = my->conn->tls_session;
328 if (fr_tls_connection_write(my->conn) < 0)
return (
int) my->next_error;
340 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
346 fr_assert((my->conn->state != TLS_CONNECTION_CONNECTED) ||
349 my->write_blocked =
true;
363 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
369 my->write_blocked =
false;
372 if (rcode <= 0)
return rcode;
374 if (!my->cb.write_resume)
return 1;
376 return my->cb.write_resume(bio);
395 fr_tls_session_t *tls_session = my->conn->tls_session;
405 if (my->write_blocked) {
416 if (rcode < 0)
return rcode;
420 while (written < size) {
421 size_t chunk = size - written;
431 if (fr_tls_session_send(my->conn->request, tls_session) < 0) {
444 if (rcode == 1)
continue;
459 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
470 my->write_blocked =
false;
488 my->packet_ctx = NULL;
498 fr_tls_session_t *tls_session = my->conn->tls_session;
501 (SSL_pending(tls_session->ssl) > 0);
513 fr_tls_session_t *tls_session = my->conn->tls_session;
516 rcode = fr_tls_session_recv(my->conn->request, tls_session);
527 if (rcode == 1)
return 0;
530 my->clean_offset = 0;
538 if (rcode < 0)
return rcode;
549 size_t used = my->clean_len - my->clean_offset;
554 my->clean_offset += size;
572 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
573 fr_tls_connection_t *conn = my->conn;
574 fr_tls_session_t *tls_session = conn->tls_session;
593 if (rcode < 0)
return rcode;
596 if (my->clean_len > 0)
continue;
602 if ((SSL_get_shutdown(tls_session->ssl) & SSL_RECEIVED_SHUTDOWN) != 0) {
626 if ((tls_session->socket_type == SOCK_DGRAM) && (room < tls_session->
conf->max_datagram_len)) {
627 fr_strerror_printf(
"Only %zu bytes are free for the next datagram, and max_datagram_len is %u",
628 room, tls_session->conf->max_datagram_len);
648 if (rcode ==
fr_bio_error(IO_WOULD_BLOCK)) errno = EAGAIN;
650 switch (fr_tls_connection_io_error(conn, rcode)) {
651 case FR_TLS_CONNECTION_IO_RETRY:
657 case FR_TLS_CONNECTION_IO_BLOCKED:
658 case FR_TLS_CONNECTION_IO_OK:
659 case FR_TLS_CONNECTION_IO_EOF:
660 case FR_TLS_CONNECTION_IO_FATAL:
679 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
691 if (rcode < 0)
return rcode;
692 if (rcode == 0)
break;
699 my->priv_cb.eof = NULL;
715 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
720 if (my->conn->state != TLS_CONNECTION_CONNECTED)
return 1;
738 fr_bio_tls_t *my = talloc_get_type_abort(bio, fr_bio_tls_t);
760 if (size == SIZE_MAX)
return 0;
781 fr_bio_tls_t *my = talloc_get_type_abort(conn->uctx, fr_bio_tls_t);
783 if (!my->events || !my->read_paused)
return;
786 fr_tls_connection_failed(conn, TLS_CONNECTION_FAIL_APPLICATION);
790 my->read_paused =
false;
804 fr_bio_tls_t *my = talloc_get_type_abort(uctx, fr_bio_tls_t);
810 room = fr_tls_connection_recv_room(my->conn);
811 dirty_in = my->conn->tls_session->dirty_in;
814 fr_tls_connection_failed(my->conn, TLS_CONNECTION_FAIL_APPLICATION);
818 my->read_paused =
true;
826 fr_tls_connection_received(my->conn, (
size_t) slen);
834 if (slen ==
fr_bio_error(IO_WOULD_BLOCK)) errno = EAGAIN;
841 if (fr_tls_connection_io_error(my->conn, slen) != FR_TLS_CONNECTION_IO_RETRY)
return;
855 fr_bio_tls_t *my = talloc_get_type_abort(uctx, fr_bio_tls_t);
866 my->write_blocked =
false;
869 fr_tls_connection_process(my->conn);
877 fr_bio_tls_t *my = talloc_get_type_abort(uctx, fr_bio_tls_t);
880 (void) fr_tls_connection_io_error(my->conn, 0);
889 (void) fr_tls_connection_io_error(my->conn, -1);
904 fr_tls_connection_t *conn = my->conn;
918 if (!conn->name)
return -1;
922 conn->tls_session = fr_tls_session_alloc_client(
request, my->ssl_ctx,
request);
924 conn->tls_session = fr_tls_session_alloc_server(
request, my->ssl_ctx,
request, my->client_cert);
926 if (!conn->tls_session)
return -1;
932 if (info->
socket.
type == SOCK_DGRAM) conn->tls_session->cookie = &info->
socket;
938 if (my->clienthello) {
940 fr_strerror_printf(
"The ClientHello packet (%zu octets) does not fit in the TLS buffer", my->clienthello_len);
943 TALLOC_FREE(my->clienthello);
944 my->clienthello_len = 0;
959 my->write_wait =
true;
965 if (fr_tls_connection_push(conn) < 0) {
981 fr_tls_connection_wake(conn);
994 if (!prev)
return NULL;
996 return talloc_get_type_abort(prev, fr_bio_tls_t);
1046 if (my->conn->state != TLS_CONNECTION_CONNECTED)
return;
1047 if (my->conn->failed != TLS_CONNECTION_FAIL_NONE)
return;
1053 fr_tls_session_close_send(my->conn->request, my->conn->tls_session);
1077 my->cb.failed = NULL;
1081 my->write_wait =
false;
1125 if (!talloc_check_name(next,
"fr_bio_fd_t")) {
1130 my = talloc_zero(
request, fr_bio_tls_t);
1131 if (!my)
return NULL;
1134 my->ssl_ctx = ssl_ctx;
1135 my->client = client;
1136 my->client_cert = client_cert;
1145 my->conn->uctx = my;
1146 my->conn->client = client;
1192 if (my->client || (info->
socket.
type != SOCK_DGRAM) || my->conn->tls_session) {
1193 fr_strerror_const(
"The first datagram can only be given to a DTLS server before the connection starts");
1197 if (data_len <= DTLS1_RT_HEADER_LENGTH) {
1199 data_len, DTLS1_RT_HEADER_LENGTH + 1);
1203 if (data_len > FR_TLS_MAX_PACKET_SIZE) {
1205 data_len, FR_TLS_MAX_PACKET_SIZE);
1209 if (
data[0] != SSL3_RT_HANDSHAKE) {
1214 if ((
data[3] != 0) || (
data[4] != 0)) {
1226 if (
data[DTLS1_RT_HEADER_LENGTH] != SSL3_MT_CLIENT_HELLO) {
1228 data[DTLS1_RT_HEADER_LENGTH], SSL3_MT_CLIENT_HELLO);
1235 TALLOC_FREE(my->clienthello);
1237 my->clienthello = talloc_memdup(my,
data, data_len);
1238 if (!my->clienthello) {
1242 my->clienthello_len = data_len;
static int const char char buffer[256]
fr_bio_write_t _CONST write
write to the underlying bio
static fr_bio_t * fr_bio_prev(fr_bio_t *bio)
fr_bio_read_t _CONST read
read from the underlying bio
static fr_bio_t * fr_bio_next(fr_bio_t *bio)
static int packet_ctx
the tests pass only the address of packet_ctx
static uint8_t request[4]
static void fr_bio_chain(fr_bio_t *first, fr_bio_t *second)
Chain one bio after another.
#define fr_dbuff_advance(_dbuff_or_marker, _len)
Advance 'current' position in dbuff or marker by _len bytes.
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
#define fr_dbuff_extend_lowat(_status, _dbuff_or_marker, _lowat)
Extend if we're below _lowat.
#define fr_dbuff_remaining(_dbuff_or_marker)
Return the number of bytes remaining between the dbuff or marker and the end of the buffer.
#define fr_dbuff_in_memcpy(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into a dbuff or marker.
#define fr_dbuff_in_memcpy_partial(_out, _in, _inlen)
Copy at most _inlen bytes into the dbuff.
#define fr_event_fd_insert(...)
#define fr_event_filter_update(...)
#define FR_EVENT_RESUME(_s, _f)
Re-add the filter for a func from kevent.
#define FR_EVENT_SUSPEND(_s, _f)
Temporarily remove the filter for a func from kevent.
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Callbacks for the FR_EVENT_FILTER_IO filter.
Structure describing a modification to a filter's state.
int fr_bio_fd_connect_full(fr_bio_t *bio, fr_event_list_t *el, fr_bio_callback_t connected_cb, fr_bio_callback_t error_cb, fr_time_delta_t *timeout, fr_bio_callback_t timeout_cb)
Finalize a connect()
fr_bio_fd_info_t const * fr_bio_fd_info(fr_bio_t *bio)
Returns a pointer to the bio-specific information.
fr_socket_t socket
as connected socket
char const * name
printable name of this BIO
fr_bio_fd_state_t state
connecting, open, closed, etc.
@ FR_BIO_FD_STATE_OPEN
error states must be before this
Run-time status of the socket.
rlm_rcode_t unlang_interpret(request_t *request, bool running)
Run the interpreter for a current request.
void unlang_interpret_signal(request_t *request, fr_signal_t action)
Send a signal (usually stop) to a request.
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
#define UNLANG_REQUEST_RESUME
int fr_bio_write_blocked(fr_bio_t *bio)
Internal BIO function to tell all BIOs that it's blocked.
ssize_t fr_bio_shutdown_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
ssize_t fr_bio_shutdown_write(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
fr_bio_t * fr_bio_eof(fr_bio_t *bio)
Internal BIO function to run EOF callbacks.
int fr_bio_shutdown(fr_bio_t *bio)
Shut down a set of BIOs.
static void fr_bio_tls_events_delete(fr_bio_tls_t *my)
Stop owning the events of the fd.
void fr_bio_tls_close_send(fr_bio_tls_t *my)
Send a TLS close_notify alert to the peer.
static void fr_bio_tls_el_write(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
The fd is ready for writes.
static void fr_bio_tls_conn_connected(fr_tls_connection_t *conn)
The TLS connection can carry application data.
size_t clean_offset
How much of the decrypted data the caller has read.
static ssize_t fr_bio_tls_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read encrypted data from the next bio, and return the decrypted application data.
static void fr_bio_tls_conn_close(fr_tls_connection_t *conn)
The TLS connection failed.
static ssize_t fr_bio_tls_read_wait(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
Application data cannot move until the connection is connected.
bool client
Act as the client, and connect to a server.
bool read_paused
The read event of the fd paused.
fr_tls_connection_t * fr_bio_tls_conn(fr_bio_tls_t *my)
The TLS connection, so the caller can set conn->name
void * packet_ctx
Passed to the next bio when writing encrypted records.
bool events
The bio owns the events of the fd, which it has until the connection is connected.
static int fr_bio_tls_write_blocked(fr_bio_t *bio)
Record that the next bio is blocked.
static int fr_bio_tls_write_wait(fr_bio_tls_t *my, bool wait)
Start or stop waiting for the fd to have room for more records.
static ssize_t fr_bio_tls_write_data(fr_bio_tls_t *my, void const *buffer, size_t size)
Encrypt application data, and write the encrypted records to the next bio.
size_t clean_len
How much decrypted data is in clean_out.
static bool fr_bio_tls_pending(fr_bio_tls_t *my)
Check whether OpenSSL holds data which the bio has not yet processed.
static int fr_bio_tls_decrypt(fr_bio_tls_t *my)
Decrypt one record which is waiting in OpenSSL.
static void fr_bio_tls_fd_connected(fr_bio_t *fd_bio)
The fd is connected.
static fr_event_update_t const resume_write[]
static fr_event_update_t const pause_read[]
ssize_t next_error
The error which the next bio returned from a write.
bool write_wait
The write event of the fd is active.
static ssize_t fr_bio_tls_write_wait_connected(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, size_t size)
int fd
The fd of the next bio.
static void fr_bio_tls_failed(fr_bio_tls_t *my, fr_tls_connection_fail_t reason, int error)
Stop the connection, and tell the application.
static void fr_bio_tls_fd_error(fr_bio_t *fd_bio)
The fd failed to connect.
SSL_CTX * ssl_ctx
Used to create the TLS session.
static fr_event_update_t const resume_read[]
static void fr_bio_tls_el_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
The fd failed.
static int fr_bio_tls_write_resume(fr_bio_t *bio)
Write the encrypted records which were waiting for the next bio.
bool write_blocked
The next bio is blocked.
static ssize_t fr_bio_tls_read_eof(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Return the decrypted data which remains after the next bio reached EOF, and then return EOF.
static int fr_bio_tls_eof(fr_bio_t *bio)
Handle EOF from the next bio.
int fr_bio_tls_connect(fr_bio_tls_t *my)
Take the connection through every state which comes before application data.
static void fr_bio_tls_el_read(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
The fd is ready for reads.
fr_bio_tls_t * fr_bio_tls_alloc(request_t *request, fr_bio_t *next, SSL_CTX *ssl_ctx, bool client, bool client_cert)
Allocate a TLS bio.
int fr_bio_tls_dtls_clienthello(fr_bio_tls_t *my, uint8_t const *data, size_t data_len)
Give a DTLS server the datagram which started the connection.
bool client_cert
For a server, require a client certificate.
static void fr_bio_tls_conn_recv_resume(fr_tls_connection_t *conn)
Resume reading from the connection.
static int fr_bio_tls_flush(fr_bio_tls_t *my)
Write any pending encrypted records to the next bio.
uint8_t * clienthello
For DTLS, the datagram which started the connection.
static int fr_bio_tls_shutdown(fr_bio_t *bio)
Disconnect the bio from the fd.
fr_tls_connection_t * conn
The TLS connection, which is a child of the bio.
static size_t fr_bio_tls_clean_read(fr_bio_tls_t *my, void *buffer, size_t size)
Copy decrypted data which the caller has not yet read into the caller's buffer.
size_t clienthello_len
Length of clienthello.
static int fr_bio_tls_destructor(fr_bio_tls_t *my)
Send any close_notify, stop the fd events, cancel the request, and then run the common bio destructor...
static int fr_bio_tls_handshake_start(fr_bio_tls_t *my)
The fd is connected, so start the TLS connection.
static ssize_t fr_bio_tls_conn_write(fr_tls_connection_t *conn, uint8_t const *data, size_t size)
Write encrypted records to the next bio.
static fr_bio_tls_t * fr_bio_tls_from_fd(fr_bio_t *fd_bio)
Find the TLS bio from the fd bio.
static void fr_bio_tls_next_resume(fr_bio_t *next)
Tell the next bio that it can write again.
fr_event_list_t * el
The event list of the interpreter.
static fr_event_update_t const pause_write[]
static ssize_t fr_bio_tls_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Stores all information relating to an event list.
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
@ FR_SIGNAL_CANCEL
Request has been cancelled.
static fr_event_list_t * el
int fd
File descriptor if this is a live socket.
int type
SOCK_STREAM, SOCK_DGRAM, etc.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_const_push(_msg)
#define fr_strerror_const(_msg)