1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
/* Copyright (c) 2007-2009, UNINETT AS
* Copyright (c) 2012, NORDUnet A/S
* Copyright (c) 2023, SWITCH */
/* See LICENSE for licensing information. */
#include "debug.h"
#include "hostport.h"
#include "radsecproxy.h"
#include "util.h"
#include <arpa/inet.h>
#include <ctype.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/in.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <poll.h>
#include <pthread.h>
#include <regex.h>
#include <signal.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef RADPROT_TLS
static void setprotoopts(struct commonprotoopts *opts);
static char **getlistenerargs(void);
void *tlslistener(void *arg);
int tlsconnect(struct server *server, int timeout, int reconnect);
void *tlsclientrd(void *arg);
int clientradputtls(struct server *server, unsigned char *rad, int radlen);
void tlssetsrcres(void);
static const struct protodefs protodefs = {
"tls",
"radsec", /* secretdefault */
SOCK_STREAM, /* socktype */
"2083", /* portdefault */
0, /* retrycountdefault */
0, /* retrycountmax */
REQUEST_RETRY_INTERVAL *REQUEST_RETRY_COUNT, /* retryintervaldefault */
60, /* retryintervalmax */
DUPLICATE_INTERVAL, /* duplicateintervaldefault */
setprotoopts, /* setprotoopts */
getlistenerargs, /* getlistenerargs */
tlslistener, /* listener */
tlsconnect, /* connecter */
tlsclientrd, /* clientconnreader */
clientradputtls, /* clientradput */
NULL, /* addclient */
NULL, /* addserverextra */
tlssetsrcres, /* setsrcres */
NULL /* initextra */
};
static struct addrinfo *srcres = NULL;
static uint8_t handle;
static struct commonprotoopts *protoopts = NULL;
const struct protodefs *tlsinit(uint8_t h) {
handle = h;
return &protodefs;
}
static void setprotoopts(struct commonprotoopts *opts) {
protoopts = opts;
}
static char **getlistenerargs(void) {
return protoopts ? protoopts->listenargs : NULL;
}
void tlssetsrcres(void) {
if (!srcres)
srcres =
resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
AF_UNSPEC, NULL, protodefs.socktype);
}
static void cleanup_connection(struct server *server) {
if (server->ssl)
SSL_shutdown(server->ssl);
if (server->sock >= 0)
close(server->sock);
server->sock = -1;
if (server->ssl)
SSL_free(server->ssl);
server->ssl = NULL;
}
int tlsconnect(struct server *server, int timeout, int reconnect) {
struct timeval now, start;
uint32_t wait;
int firsttry = 1;
X509 *cert;
SSL_CTX *ctx = NULL;
unsigned long error;
int origflags;
struct addrinfo *source = NULL;
char *subj;
struct list_node *entry;
struct hostportres *hp;
debug(DBG_DBG, "tlsconnect: %s to %s", reconnect ? "reconnecting" : "initial connection", server->conf->name);
pthread_mutex_lock(&server->lock);
if (server->state == RSP_SERVER_STATE_CONNECTED)
server->state = RSP_SERVER_STATE_RECONNECTING;
pthread_mutex_unlock(&server->lock);
if (server->conf->source) {
source = resolvepassiveaddrinfo(server->conf->source, AF_UNSPEC, NULL, protodefs.socktype);
if (!source)
debug(DBG_WARN, "tlsconnect: could not resolve source address to bind for server %s, using default", server->conf->name);
}
gettimeofday(&start, NULL);
for (;;) {
cleanup_connection(server);
wait = connect_wait(start, server->connecttime, firsttry);
gettimeofday(&now, NULL);
if (timeout && (now.tv_sec - start.tv_sec) + wait > timeout) {
debug(DBG_DBG, "tlsconnect: timeout");
if (source)
freeaddrinfo(source);
return 0;
}
if (wait)
debug(DBG_INFO, "Next connection attempt to %s in %lds", server->conf->name, wait);
sleep(wait);
firsttry = 0;
gettimeofday(&now, NULL);
if (timeout && (now.tv_sec - start.tv_sec) > timeout) {
debug(DBG_DBG, "tlsconnect: timeout");
if (source)
freeaddrinfo(source);
return 0;
}
for (entry = list_first(server->conf->hostports); entry; entry = list_next(entry)) {
hp = (struct hostportres *)entry->data;
debug(DBG_INFO, "tlsconnect: trying to open TLS connection to server %s (%s port %s)", server->conf->name, hp->host, hp->port);
if ((server->sock = connecttcp(hp->addrinfo, source ? source : srcres, list_count(server->conf->hostports) > 1 ? 5 : 30)) < 0) {
debug(DBG_ERR, "tlsconnect: TLS connection to %s (%s port %s) failed: TCP connect failed", server->conf->name, hp->host, hp->port);
goto concleanup;
}
if (server->conf->keepalive)
enable_keepalive(server->sock);
pthread_mutex_lock(&server->conf->tlsconf->lock);
if (!(ctx = tlsgetctx(handle, server->conf->tlsconf))) {
pthread_mutex_unlock(&server->conf->tlsconf->lock);
debug(DBG_ERR, "tlsconnect: failed to get TLS context for server %s", server->conf->name);
goto concleanup;
}
server->ssl = SSL_new(ctx);
pthread_mutex_unlock(&server->conf->tlsconf->lock);
if (!server->ssl) {
debug(DBG_ERR, "tlsconnect: failed to create SSL connection for server %s", server->conf->name);
goto concleanup;
}
if (!SSL_set_ex_data(server->ssl, RSP_EX_DATA_CONFIG, server->conf)) {
debug(DBG_WARN, "tlsconnect: failed to set ex data");
}
if (server->conf->sni) {
struct in6_addr tmp;
char *servername = server->conf->sniservername ? server->conf->sniservername
: server->conf->servername ? server->conf->servername
: (inet_pton(AF_INET, hp->host, &tmp) || inet_pton(AF_INET6, hp->host, &tmp)) ? NULL
: hp->host;
if (servername && !tlssetsni(server->ssl, servername)) {
debug(DBG_ERR, "tlsconnect: set SNI %s failed", servername);
goto concleanup;
}
}
SSL_set_fd(server->ssl, server->sock);
if (sslconnecttimeout(server->ssl, 5) <= 0) {
while ((error = ERR_get_error()))
debug(DBG_ERR, "tlsconnect: SSL connect to %s failed: %s", server->conf->name, ERR_error_string(error, NULL));
debug(DBG_ERR, "tlsconnect: SSL connect to %s (%s port %s) failed", server->conf->name, hp->host, hp->port);
goto concleanup;
}
if (server->conf->pskid && server->conf->pskkey) {
if (SSL_session_reused(server->ssl)) {
debug(DBG_WARN, "tlsconnect: TLS connection to %s (%s port %s), PSK identity %s with cipher %s up",
server->conf->name, hp->host, hp->port, server->conf->pskid, SSL_CIPHER_get_name(SSL_get_current_cipher(server->ssl)));
break;
} else {
debug(DBG_ERR, "tlsconnect: TLS PSK set for %s (%s port %s) but not used in session, rejecting connection",
server->conf->name, hp->host, hp->port);
goto concleanup;
}
}
cert = verifytlscert(server->ssl);
if (!cert) {
debug(DBG_ERR, "tlsconnect: certificate verification failed for %s (%s port %s)", server->conf->name, hp->host, hp->port);
goto concleanup;
}
if (verifyconfcert(cert, server->conf, hp)) {
subj = getcertsubject(cert);
if (subj) {
debug(DBG_WARN, "tlsconnect: TLS connection to %s (%s port %s), subject %s, %s with cipher %s up",
server->conf->name, hp->host, hp->port, subj,
SSL_get_version(server->ssl), SSL_CIPHER_get_name(SSL_get_current_cipher(server->ssl)));
free(subj);
}
X509_free(cert);
break;
} else {
debug(DBG_ERR, "tlsconnect: certificate verification failed for %s (%s port %s)", server->conf->name, hp->host, hp->port);
}
X509_free(cert);
concleanup:
/* ensure previous connection is properly closed */
cleanup_connection(server);
}
if (server->ssl)
break;
}
gettimeofday(&server->connecttime, NULL);
server->tlsnewkey = server->connecttime;
origflags = fcntl(server->sock, F_GETFL, 0);
if (origflags == -1) {
debugerrno(errno, DBG_WARN, "Failed to get flags");
} else if (fcntl(server->sock, F_SETFL, origflags | O_NONBLOCK) == -1) {
debugerrno(errno, DBG_WARN, "Failed to set O_NONBLOCK");
}
pthread_mutex_lock(&server->lock);
server->state = RSP_SERVER_STATE_CONNECTED;
server->lostrqs = 0;
pthread_mutex_unlock(&server->lock);
pthread_mutex_lock(&server->newrq_mutex);
server->conreset = reconnect;
pthread_cond_signal(&server->newrq_cond);
pthread_mutex_unlock(&server->newrq_mutex);
if (source)
freeaddrinfo(source);
return 1;
}
int clientradputtls(struct server *server, unsigned char *rad, int radlen) {
int cnt;
struct clsrvconf *conf = server->conf;
struct timeval now;
if (radlen <= 0) {
debug(DBG_ERR, "clientradputtls: invalid buffer (length)");
return 0;
}
pthread_mutex_lock(&server->lock);
if (server->state != RSP_SERVER_STATE_CONNECTED) {
pthread_mutex_unlock(&server->lock);
return 0;
}
gettimeofday(&now, NULL);
#if OPENSSL_VERSION_NUMBER >= 0x10101000
if (now.tv_sec - server->tlsnewkey.tv_sec > RSP_TLS_REKEY_INTERVAL && SSL_version(server->ssl) >= TLS1_3_VERSION) {
debug(DBG_DBG, "clientradputtls: perform key update for long-running connection");
if (SSL_get_key_update_type(server->ssl) == SSL_KEY_UPDATE_NONE &&
!SSL_key_update(server->ssl, SSL_KEY_UPDATE_REQUESTED))
debug(DBG_WARN, "clientradputtls: request for key update failed for %s", conf->name);
server->tlsnewkey = now;
}
#endif
if ((cnt = sslwrite(server->ssl, rad, radlen, 0)) <= 0) {
pthread_mutex_unlock(&server->lock);
return 0;
}
debug(DBG_DBG, "clientradputtls: Sent %d bytes, Radius packet of length %zu to TLS peer %s", cnt, radlen, conf->name);
pthread_mutex_unlock(&server->lock);
return 1;
}
void *tlsclientrd(void *arg) {
struct server *server = (struct server *)arg;
unsigned char *buf = NULL;
int len = 0;
for (;;) {
len = radtlsget(server->ssl, server->conf->retryinterval * (server->conf->retrycount + 1), &server->lock, &buf);
if (buf && len > 0) {
if (!replyh(server, buf, len))
if (closeh(server))
break;
buf = NULL;
} else if (SSL_get_shutdown(server->ssl)) {
if (closeh(server))
break;
} else {
if (timeouth(server))
break;
}
}
debug(DBG_INFO, "tlsclientrd: exiting for %s", server->conf->name);
pthread_mutex_lock(&server->lock);
server->state = RSP_SERVER_STATE_FAILING;
SSL_shutdown(server->ssl);
shutdown(server->sock, SHUT_RDWR);
close(server->sock);
/* Wake up clientwr(). */
server->clientrdgone = 1;
pthread_mutex_unlock(&server->lock);
pthread_mutex_lock(&server->newrq_mutex);
pthread_cond_signal(&server->newrq_cond);
pthread_mutex_unlock(&server->newrq_mutex);
return NULL;
}
void *tlsservernew(void *arg) {
int s, origflags;
struct sockaddr_storage from;
socklen_t fromlen = sizeof(from);
struct clsrvconf *conf;
struct list_node *cur = NULL;
SSL *ssl = NULL;
X509 *cert = NULL;
SSL_CTX *ctx = NULL;
unsigned long error;
struct client *client;
struct tls *accepted_tls = NULL;
char tmp[INET6_ADDRSTRLEN], *subj;
struct hostportres *hp;
s = *(int *)arg;
free(arg);
if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
debug(DBG_DBG, "tlsservernew: getpeername failed, exiting");
goto exit;
}
debug(DBG_WARN, "tlsservernew: incoming TLS connection from %s", addr2string((struct sockaddr *)&from, tmp, sizeof(tmp)));
if (!(conf = find_clconf(handle, (struct sockaddr *)&from, &cur, &hp))) {
debug(DBG_WARN, "tlsservernew: ignoring unknown TLS client %s", addr2string((struct sockaddr *)&from, tmp, sizeof(tmp)));
goto exit;
}
pthread_mutex_lock(&conf->tlsconf->lock);
ctx = tlsgetctx(handle, conf->tlsconf);
if (!ctx) {
pthread_mutex_unlock(&conf->tlsconf->lock);
goto exit;
}
ssl = SSL_new(ctx);
pthread_mutex_unlock(&conf->tlsconf->lock);
if (!ssl)
goto exit;
if (!SSL_set_ex_data(ssl, RSP_EX_DATA_CONFIG_LIST, find_all_clconf(handle, (struct sockaddr *)&from, cur, &hp))) {
debug(DBG_WARN, "tlsservernew: failed to set ex data");
}
SSL_set_fd(ssl, s);
if (sslaccepttimeout(ssl, 30) <= 0) {
struct clsrvconf *selected = SSL_get_ex_data(ssl, RSP_EX_DATA_CONFIG);
conf = selected ? selected : conf;
while ((error = ERR_get_error()))
debug(DBG_ERR, "tlsservernew: SSL accept from %s (%s) failed: %s", conf->name, addr2string((struct sockaddr *)&from, tmp, sizeof(tmp)), ERR_error_string(error, NULL));
debug(DBG_ERR, "tlsservernew: SSL_accept failed");
list_free(SSL_get_ex_data(ssl, RSP_EX_DATA_CONFIG_LIST));
goto exit;
}
list_free(SSL_get_ex_data(ssl, RSP_EX_DATA_CONFIG_LIST));
if (!(SSL_session_reused(ssl) || (cert = verifytlscert(ssl))))
goto exit;
{
struct clsrvconf *selected = SSL_get_ex_data(ssl, RSP_EX_DATA_CONFIG);
conf = selected ? selected : conf;
}
accepted_tls = conf->tlsconf;
origflags = fcntl(s, F_GETFL, 0);
if (origflags == -1) {
debugerrno(errno, DBG_WARN, "Failed to get flags");
} else if (fcntl(s, F_SETFL, origflags | O_NONBLOCK) == -1) {
debugerrno(errno, DBG_WARN, "Failed to set O_NONBLOCK");
}
if (SSL_session_reused(ssl)) {
debug(DBG_WARN, "tlsservernew: TLS connection from %s, client %s, PSK identity %s wtih cipher %s up",
addr2string((struct sockaddr *)&from, tmp, sizeof(tmp)), conf->name, conf->pskid,
SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)));
} else {
while (conf) {
if (!conf->pskid && accepted_tls == conf->tlsconf && (verifyconfcert(cert, conf, NULL))) {
subj = getcertsubject(cert);
if (subj) {
debug(DBG_WARN, "tlsservernew: TLS connection from %s, client %s, subject %s, %s with cipher %s up",
addr2string((struct sockaddr *)&from, tmp, sizeof(tmp)), conf->name, subj,
SSL_get_version(ssl), SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)));
free(subj);
}
X509_free(cert);
cert = NULL;
break;
}
conf = find_clconf(handle, (struct sockaddr *)&from, &cur, &hp);
}
}
if (!conf) {
debug(DBG_WARN, "tlsservernew: ignoring request, no matching TLS client for %s",
addr2string((struct sockaddr *)&from, tmp, sizeof(tmp)));
goto exit;
}
client = addclient(conf, 1);
if (client) {
if (conf->keepalive)
enable_keepalive(s);
client->ssl = ssl;
client->addr = addr_copy((struct sockaddr *)&from);
tlsserverrd(client);
removeclient(client);
} else
debug(DBG_WARN, "tlsservernew: failed to create new client instance");
exit:
if (cert)
X509_free(cert);
if (ssl) {
SSL_shutdown(ssl);
SSL_free(ssl);
}
shutdown(s, SHUT_RDWR);
close(s);
pthread_exit(NULL);
}
void tlsaccept(int s) {
int *s_arg;
pthread_t tcpserverth;
s_arg = malloc(sizeof(s));
if (!s_arg) {
debug(DBG_ERR, "tlsaccept: malloc failed");
return;
}
*s_arg = s;
if (pthread_create(&tcpserverth, &pthread_attr, tlsservernew, (void *)s_arg)) {
debug(DBG_ERR, "tlsaccept: pthread_create failed");
free(s_arg);
shutdown(s, SHUT_RDWR);
close(s);
return;
}
pthread_detach(tcpserverth);
}
void *tlslistener(void *arg) {
accepttcp(*(int *)arg, tlsaccept);
free(arg);
return NULL;
}
#else
const struct protodefs *tlsinit(uint8_t h) {
return NULL;
}
#endif
/* Local Variables: */
/* c-file-style: "stroustrup" */
/* End: */
|