Index: projects/nfs-over-tls/usr.sbin/rpc.tlsclntd/Makefile =================================================================== --- projects/nfs-over-tls/usr.sbin/rpc.tlsclntd/Makefile (revision 365865) +++ projects/nfs-over-tls/usr.sbin/rpc.tlsclntd/Makefile (revision 365866) @@ -1,32 +1,32 @@ # $FreeBSD$ .include PROG= rpc.tlsclntd MAN= rpc.tlsclntd.8 -SRCS= rpc.tlsclntd.c rpctlscd.h rpctlscd_svc.c rpctlscd_xdr.c +SRCS= rpc.tlsclntd.c rpc.tlscommon.c rpctlscd.h rpctlscd_svc.c rpctlscd_xdr.c -CFLAGS+= -I. +CFLAGS+= -I. -I${SRCTOP}/usr.sbin/rpc.tlsservd CFLAGS+= -I/usr/ktls/include LDFLAGS+= -L/usr/ktls/lib LIBADD= ssl crypto util CLEANFILES= rpctlscd_svc.c rpctlscd_xdr.c rpctlscd.h RPCSRC= ${SRCTOP}/sys/rpc/rpcsec_tls/rpctlscd.x RPCGEN= RPCGEN_CPP=${CPP:Q} rpcgen -L -C -M rpctlscd_svc.c: ${RPCSRC} rpctlscd.h ${RPCGEN} -m -o ${.TARGET} ${RPCSRC} rpctlscd_xdr.c: ${RPCSRC} rpctlscd.h ${RPCGEN} -c -o ${.TARGET} ${RPCSRC} rpctlscd.h: ${RPCSRC} ${RPCGEN} -h -o ${.TARGET} ${RPCSRC} -.PATH: ${SRCTOP}/sys/rpc/rpcsec_tls +.PATH: ${SRCTOP}/sys/rpc/rpcsec_tls ${SRCTOP}/usr.sbin/rpc.tlsservd .include Index: projects/nfs-over-tls/usr.sbin/rpc.tlsclntd/rpc.tlsclntd.c =================================================================== --- projects/nfs-over-tls/usr.sbin/rpc.tlsclntd/rpc.tlsclntd.c (revision 365865) +++ projects/nfs-over-tls/usr.sbin/rpc.tlsclntd/rpc.tlsclntd.c (revision 365866) @@ -1,913 +1,684 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2008 Isilon Inc http://www.isilon.com/ * Authors: Doug Rabson * Developed with Red Inc: Alfred Perlstein * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* Modified from gssd.c for the client side of kernel RPC-over-TLS. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "rpctlscd.h" +#include "rpc.tlscommon.h" #ifndef _PATH_RPCTLSCDSOCK #define _PATH_RPCTLSCDSOCK "/var/run/rpc.tlsclntd.sock" #endif #ifndef _PATH_CERTANDKEY #define _PATH_CERTANDKEY "/etc/rpc.tlsclntd/" #endif #ifndef _PATH_RPCTLSCDPID #define _PATH_RPCTLSCDPID "/var/run/rpc.tlsclntd.pid" #endif #ifndef _PREFERRED_CIPHERS #define _PREFERRED_CIPHERS "AES128-GCM-SHA256" #endif -/* - * How long to delay a reload of the CRL when there are RPC request(s) - * to process, in usec. Must be less than 1second. - */ -#define RELOADDELAY 250000 +/* Global variables also used by rpc.tlscommon.c. */ +int rpctls_debug_level; +bool rpctls_verbose; +SSL_CTX *rpctls_ctx = NULL; +const char *rpctls_verify_cafile = NULL; +const char *rpctls_verify_capath = NULL; +const char *rpctls_crlfile = NULL; +bool rpctls_gothup = false; +struct ssl_list rpctls_ssllist; + static struct pidfh *rpctls_pfh = NULL; -static int rpctls_debug_level; -static bool rpctls_verbose; -static SSL_CTX *rpctls_ctx = NULL; -static const char *rpctls_verify_cafile = NULL; -static const char *rpctls_verify_capath = NULL; -static const char *rpctls_crlfile = NULL; static const char *rpctls_certdir = _PATH_CERTANDKEY; static uint64_t rpctls_ssl_refno = 0; static uint64_t rpctls_ssl_sec = 0; static uint64_t rpctls_ssl_usec = 0; -static bool rpctls_gothup = false; -/* - * A linked list of all current "SSL *"s and socket "fd"s - * for kernel RPC TLS connections is maintained. - * The "refno" field is a unique 64bit value used to - * identify which entry a kernel RPC upcall refers to. - */ -LIST_HEAD(ssl_list, ssl_entry); -struct ssl_entry { - LIST_ENTRY(ssl_entry) next; - uint64_t refno; - int s; - bool shutoff; - SSL *ssl; - X509 *cert; -}; -static struct ssl_list rpctls_ssllist; - static void rpctlscd_terminate(int); static SSL_CTX *rpctls_setupcl_ssl(bool cert); static SSL *rpctls_connect(SSL_CTX *ctx, int s, X509 **certp); -static int rpctls_gethost(int s, struct sockaddr *sad, - char *hostip, size_t hostlen); -static int rpctls_checkhost(struct sockaddr *sad, X509 *cert); -static int rpctls_loadcrlfile(SSL_CTX *ctx); static void rpctls_huphandler(int sig __unused); -static void rpctls_checkcrl(void); -static void rpctlscd_verbose_out(const char *fmt, ...); extern void rpctlscd_1(struct svc_req *rqstp, SVCXPRT *transp); static struct option longopts[] = { { "certdir", required_argument, NULL, 'D' }, { "debuglevel", no_argument, NULL, 'd' }, { "verifylocs", required_argument, NULL, 'l' }, { "mutualverf", no_argument, NULL, 'm' }, { "verifydir", required_argument, NULL, 'p' }, { "crl", required_argument, NULL, 'r' }, { "verbose", no_argument, NULL, 'v' }, { NULL, 0, NULL, 0 } }; int main(int argc, char **argv) { /* * We provide an RPC service on a local-domain socket. The * kernel rpctls code will upcall to this daemon to do the initial * TLS handshake. */ struct sockaddr_un sun; - int ch, fd, oldmask, ret; + int ch, fd, oldmask; SVCXPRT *xprt; bool cert; struct timeval tm; struct timezone tz; pid_t otherpid; - fd_set readfds; - uint64_t curtime, nexttime; - struct timespec tp; - sigset_t sighup_mask; /* Check that another rpctlscd isn't already running. */ rpctls_pfh = pidfile_open(_PATH_RPCTLSCDPID, 0600, &otherpid); if (rpctls_pfh == NULL) { if (errno == EEXIST) errx(1, "rpctlscd already running, pid: %d.", otherpid); warn("cannot open or create pidfile"); } if (modfind("ktls_ocf") < 0) { /* Not present in kernel, try loading it */ if (kldload("ktls_ocf") < 0 || modfind("ktls_ocf") < 0) errx(1, "Cannot load ktls_ocf"); } if (modfind("aesni") < 0) { /* Not present in kernel, try loading it */ kldload("aesni"); } /* Get the time when this daemon is started. */ gettimeofday(&tm, &tz); rpctls_ssl_sec = tm.tv_sec; rpctls_ssl_usec = tm.tv_usec; rpctls_verbose = false; cert = false; while ((ch = getopt_long(argc, argv, "D:dl:mp:r:v", longopts, NULL)) != -1) { switch (ch) { case 'D': rpctls_certdir = optarg; break; case 'd': rpctls_debug_level++; break; case 'l': rpctls_verify_cafile = optarg; break; case 'm': cert = true; break; case 'p': rpctls_verify_capath = optarg; break; case 'r': rpctls_crlfile = optarg; break; case 'v': rpctls_verbose = true; break; default: fprintf(stderr, "usage: %s " "[-D/--certdir certdir] [-d/--debuglevel] " "[-l/--verifylocs CAfile] [-m/--mutualverf] " "[-p/--verifydir CApath] [-r/--crl CRLfile] " "[-v/--verbose]\n", argv[0]); exit(1); break; } } if (rpctls_crlfile != NULL && rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL) errx(1, "-r requires the -l and/or " "-p options"); if (modfind("krpc") < 0) { /* Not present in kernel, try loading it */ if (kldload("krpc") < 0 || modfind("krpc") < 0) errx(1, "Kernel RPC is not available"); } /* * Set up the SSL_CTX *. * Do it now, before daemonizing, in case the private key * is encrypted and requires a passphrase to be entered. */ rpctls_ctx = rpctls_setupcl_ssl(cert); if (rpctls_ctx == NULL) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't set up TSL context"); exit(1); } err(1, "Can't set up TSL context"); } LIST_INIT(&rpctls_ssllist); if (!rpctls_debug_level) { if (daemon(0, 0) != 0) err(1, "Can't daemonize"); signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGHUP, SIG_IGN); } signal(SIGTERM, rpctlscd_terminate); signal(SIGPIPE, SIG_IGN); signal(SIGHUP, rpctls_huphandler); pidfile_write(rpctls_pfh); memset(&sun, 0, sizeof sun); sun.sun_family = AF_LOCAL; unlink(_PATH_RPCTLSCDSOCK); strcpy(sun.sun_path, _PATH_RPCTLSCDSOCK); sun.sun_len = SUN_LEN(&sun); fd = socket(AF_LOCAL, SOCK_STREAM, 0); if (fd < 0) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't create local rpctlscd socket"); exit(1); } err(1, "Can't create local rpctlscd socket"); } oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO); if (bind(fd, (struct sockaddr *)&sun, sun.sun_len) < 0) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't bind local rpctlscd socket"); exit(1); } err(1, "Can't bind local rpctlscd socket"); } umask(oldmask); if (listen(fd, SOMAXCONN) < 0) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't listen on local rpctlscd socket"); exit(1); } err(1, "Can't listen on local rpctlscd socket"); } xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE); if (!xprt) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't create transport for local rpctlscd socket"); exit(1); } err(1, "Can't create transport for local rpctlscd socket"); } if (!svc_reg(xprt, RPCTLSCD, RPCTLSCDVERS, rpctlscd_1, NULL)) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't register service for local rpctlscd socket"); exit(1); } err(1, "Can't register service for local rpctlscd socket"); } rpctls_syscall(RPCTLS_SYSC_CLSETPATH, _PATH_RPCTLSCDSOCK); - /* Expand svc_run() here so that we can call rpctls_loadcrlfile(). */ - curtime = nexttime = 0; - sigemptyset(&sighup_mask); - sigaddset(&sighup_mask, SIGHUP); - for (;;) { - clock_gettime(CLOCK_MONOTONIC, &tp); - curtime = tp.tv_sec; - curtime = curtime * 1000000 + tp.tv_nsec / 1000; - sigprocmask(SIG_BLOCK, &sighup_mask, NULL); - if (rpctls_gothup && curtime >= nexttime) { - rpctls_gothup = false; - sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); - ret = rpctls_loadcrlfile(rpctls_ctx); - if (ret != 0) - rpctls_checkcrl(); - else - rpctlscd_verbose_out("rpc.tlsclntd: Can't " - "reload CRLfile\n"); - clock_gettime(CLOCK_MONOTONIC, &tp); - nexttime = tp.tv_sec; - nexttime = nexttime * 1000000 + tp.tv_nsec / 1000 + - RELOADDELAY; - } else - sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); + rpctls_svc_run(); - /* - * If a reload is pending, poll for received request(s), - * otherwise set a RELOADDELAY timeout, since a SIGHUP - * could be processed between the got_sighup test and - * the select() system call. - */ - tm.tv_sec = 0; - if (rpctls_gothup) - tm.tv_usec = 0; - else - tm.tv_usec = RELOADDELAY; - readfds = svc_fdset; - switch (select(svc_maxfd + 1, &readfds, NULL, NULL, &tm)) { - case -1: - if (errno == EINTR) { - /* Allow a reload now. */ - nexttime = 0; - continue; - } - syslog(LOG_ERR, "rpc.tlsservd died: select: %m"); - exit(1); - case 0: - /* Allow a reload now. */ - nexttime = 0; - continue; - default: - svc_getreqset(&readfds); - } - } - rpctls_syscall(RPCTLS_SYSC_CLSHUTDOWN, ""); SSL_CTX_free(rpctls_ctx); EVP_cleanup(); return (0); } -static void -rpctlscd_verbose_out(const char *fmt, ...) -{ - va_list ap; - - if (rpctls_verbose) { - va_start(ap, fmt); - if (rpctls_debug_level == 0) - vsyslog(LOG_INFO | LOG_DAEMON, fmt, ap); - else - vfprintf(stderr, fmt, ap); - va_end(ap); - } -} - bool_t rpctlscd_null_1_svc(void *argp, void *result, struct svc_req *rqstp) { - rpctlscd_verbose_out("rpctlscd_null: done\n"); + rpctls_verbose_out("rpctlscd_null: done\n"); return (TRUE); } bool_t rpctlscd_connect_1_svc(void *argp, struct rpctlscd_connect_res *result, struct svc_req *rqstp) { int s; bool_t res; SSL *ssl; char buf[1024]; ssize_t siz, ret; struct ssl_entry *newslp; X509 *cert; - rpctlscd_verbose_out("rpctlsd_connect: started\n"); + rpctls_verbose_out("rpctlsd_connect: started\n"); /* Get the socket fd from the kernel. */ s = rpctls_syscall(RPCTLS_SYSC_CLSOCKET, ""); -rpctlscd_verbose_out("rpctlsd_connect s=%d\n", s); +rpctls_verbose_out("rpctlsd_connect s=%d\n", s); if (s < 0) { result->reterr = RPCTLSERR_NOSOCKET; return (TRUE); } /* Do a TLS connect handshake. */ ssl = rpctls_connect(rpctls_ctx, s, &cert); if (ssl == NULL) { - rpctlscd_verbose_out("rpctlsd_connect: can't do TLS " + rpctls_verbose_out("rpctlsd_connect: can't do TLS " "handshake\n"); result->reterr = RPCTLSERR_NOSSL; } else { result->reterr = RPCTLSERR_OK; result->sec = rpctls_ssl_sec; result->usec = rpctls_ssl_usec; result->ssl = ++rpctls_ssl_refno; /* Hard to believe this will ever wrap around.. */ if (rpctls_ssl_refno == 0) result->ssl = ++rpctls_ssl_refno; } if (ssl == NULL) { /* * For RPC-over-TLS, this upcall is expected * to close off the socket. */ close(s); return (TRUE); } /* Maintain list of all current SSL *'s */ newslp = malloc(sizeof(*newslp)); newslp->refno = rpctls_ssl_refno; newslp->s = s; newslp->shutoff = false; newslp->ssl = ssl; newslp->cert = cert; LIST_INSERT_HEAD(&rpctls_ssllist, newslp, next); return (TRUE); } bool_t rpctlscd_handlerecord_1_svc(struct rpctlscd_handlerecord_arg *argp, struct rpctlscd_handlerecord_res *result, struct svc_req *rqstp) { struct ssl_entry *slp; int ret; char junk; -rpctlscd_verbose_out("handlerec sslref=%jx\n", (uintmax_t)slp->refno); +rpctls_verbose_out("handlerec sslref=%jx\n", (uintmax_t)slp->refno); slp = NULL; if (argp->sec == rpctls_ssl_sec && argp->usec == rpctls_ssl_usec) { LIST_FOREACH(slp, &rpctls_ssllist, next) { if (slp->refno == argp->ssl) break; } } if (slp != NULL) { - rpctlscd_verbose_out("rpctlscd_handlerecord fd=%d\n", + rpctls_verbose_out("rpctlscd_handlerecord fd=%d\n", slp->s); /* * An SSL_read() of 0 bytes should fail, but it should * handle the non-application data record before doing so. */ ret = SSL_read(slp->ssl, &junk, 0); if (ret <= 0) { /* Check to see if this was a close alert. */ ret = SSL_get_shutdown(slp->ssl); -rpctlscd_verbose_out("get_shutdown2=%d\n", ret); +rpctls_verbose_out("get_shutdown2=%d\n", ret); if ((ret & (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN)) == SSL_RECEIVED_SHUTDOWN) SSL_shutdown(slp->ssl); } else { if (rpctls_debug_level == 0) syslog(LOG_ERR, "SSL_read returned %d", ret); else fprintf(stderr, "SSL_read returned %d\n", ret); } result->reterr = RPCTLSERR_OK; } else result->reterr = RPCTLSERR_NOSSL; return (TRUE); } bool_t rpctlscd_disconnect_1_svc(struct rpctlscd_disconnect_arg *argp, struct rpctlscd_disconnect_res *result, struct svc_req *rqstp) { struct ssl_entry *slp; int ret; -rpctlscd_verbose_out("disconnect refno=%jx\n", (uintmax_t)slp->refno); +rpctls_verbose_out("disconnect refno=%jx\n", (uintmax_t)slp->refno); slp = NULL; if (argp->sec == rpctls_ssl_sec && argp->usec == rpctls_ssl_usec) { LIST_FOREACH(slp, &rpctls_ssllist, next) { if (slp->refno == argp->ssl) break; } } if (slp != NULL) { - rpctlscd_verbose_out("rpctlscd_disconnect: fd=%d closed\n", + rpctls_verbose_out("rpctlscd_disconnect: fd=%d closed\n", slp->s); LIST_REMOVE(slp, next); if (!slp->shutoff) { ret = SSL_get_shutdown(slp->ssl); -rpctlscd_verbose_out("get_shutdown0=%d\n", ret); +rpctls_verbose_out("get_shutdown0=%d\n", ret); /* * Do an SSL_shutdown() unless a close alert has * already been sent. */ if ((ret & SSL_SENT_SHUTDOWN) == 0) SSL_shutdown(slp->ssl); } SSL_free(slp->ssl); if (slp->cert != NULL) X509_free(slp->cert); /* * For RPC-over-TLS, this upcall is expected * to close off the socket. */ if (!slp->shutoff) shutdown(slp->s, SHUT_WR); close(slp->s); free(slp); result->reterr = RPCTLSERR_OK; } else result->reterr = RPCTLSERR_NOCLOSE; return (TRUE); } int rpctlscd_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result) { return (TRUE); } static void rpctlscd_terminate(int sig __unused) { rpctls_syscall(RPCTLS_SYSC_CLSHUTDOWN, ""); pidfile_remove(rpctls_pfh); exit(0); } static SSL_CTX * rpctls_setupcl_ssl(bool cert) { SSL_CTX *ctx; long flags; char path[PATH_MAX]; size_t len, rlen; int ret; SSL_library_init(); SSL_load_error_strings(); OpenSSL_add_all_algorithms(); ctx = SSL_CTX_new(TLS_client_method()); if (ctx == NULL) { - rpctlscd_verbose_out("rpctls_setupcl_ssl: SSL_CTX_new " + rpctls_verbose_out("rpctls_setupcl_ssl: SSL_CTX_new " "failed\n"); return (NULL); } SSL_CTX_set_ecdh_auto(ctx, 1); /* * Set preferred ciphers, since KERN_TLS only supports a * few of them. */ ret = SSL_CTX_set_cipher_list(ctx, _PREFERRED_CIPHERS); if (ret == 0) { - rpctlscd_verbose_out("rpctls_setupcl_ssl: " + rpctls_verbose_out("rpctls_setupcl_ssl: " "SSL_CTX_set_cipher_list failed to set any ciphers\n"); SSL_CTX_free(ctx); return (NULL); } /* * If cert is true, a certificate and key exists in * rpctls_certdir, so that it can do mutual authentication. */ if (cert) { /* Get the cert.pem and key.pem files. */ len = strlcpy(path, rpctls_certdir, sizeof(path)); rlen = sizeof(path) - len; if (strlcpy(&path[len], "cert.pem", rlen) != 8) { SSL_CTX_free(ctx); return (NULL); } ret = SSL_CTX_use_certificate_file(ctx, path, SSL_FILETYPE_PEM); if (ret != 1) { - rpctlscd_verbose_out("rpctls_setupcl_ssl: can't use " + rpctls_verbose_out("rpctls_setupcl_ssl: can't use " "certificate file path=%s ret=%d\n", path, ret); SSL_CTX_free(ctx); return (NULL); } if (strlcpy(&path[len], "key.pem", rlen) != 7) { SSL_CTX_free(ctx); return (NULL); } ret = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM); if (ret != 1) { - rpctlscd_verbose_out("rpctls_setupcl_ssl: Can't use " + rpctls_verbose_out("rpctls_setupcl_ssl: Can't use " "private key path=%s ret=%d\n", path, ret); SSL_CTX_free(ctx); return (NULL); } } if (rpctls_verify_cafile != NULL || rpctls_verify_capath != NULL) { if (rpctls_crlfile != NULL) { ret = rpctls_loadcrlfile(ctx); if (ret == 0) { - rpctlscd_verbose_out("rpctls_setupcl_ssl: " + rpctls_verbose_out("rpctls_setupcl_ssl: " "Load CRLfile failed\n"); SSL_CTX_free(ctx); return (NULL); } } #if OPENSSL_VERSION_NUMBER >= 0x30000000 ret = 1; if (rpctls_verify_cafile != NULL) ret = SSL_CTX_load_verify_file(ctx, rpctls_verify_cafile); if (ret != 0 && rpctls_verify_capath != NULL) ret = SSL_CTX_load_verify_dir(ctx, rpctls_verify_capath); #else ret = SSL_CTX_load_verify_locations(ctx, rpctls_verify_cafile, rpctls_verify_capath); #endif if (ret == 0) { - rpctlscd_verbose_out("rpctls_setupcl_ssl: " + rpctls_verbose_out("rpctls_setupcl_ssl: " "Can't load verify locations\n"); SSL_CTX_free(ctx); return (NULL); } /* * The man page says that the * SSL_CTX_set0_CA_list() call is not normally * needed, but I believe it is harmless. */ if (rpctls_verify_cafile != NULL) SSL_CTX_set0_CA_list(ctx, SSL_load_client_CA_file(rpctls_verify_cafile)); } /* RPC-over-TLS must use TLSv1.3. */ #ifdef notyet flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1_2; #else flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1_3; #endif SSL_CTX_set_options(ctx, flags); return (ctx); } static SSL * rpctls_connect(SSL_CTX *ctx, int s, X509 **certp) { SSL *ssl; X509 *cert; struct sockaddr *sad; struct sockaddr_storage ad; char hostnam[NI_MAXHOST]; int gethostret, ret; char *cp, *cp2; *certp = NULL; ssl = SSL_new(ctx); if (ssl == NULL) { - rpctlscd_verbose_out("rpctls_connect: " + rpctls_verbose_out("rpctls_connect: " "SSL_new failed\n"); return (NULL); } if (SSL_set_fd(ssl, s) != 1) { - rpctlscd_verbose_out("rpctls_connect: " + rpctls_verbose_out("rpctls_connect: " "SSL_set_fd failed\n"); SSL_free(ssl); return (NULL); } -rpctlscd_verbose_out("at SSL_connect\n"); +rpctls_verbose_out("at SSL_connect\n"); ret = SSL_connect(ssl); -rpctlscd_verbose_out("aft SSL_connect ret=%d\n", ret); +rpctls_verbose_out("aft SSL_connect ret=%d\n", ret); if (ret != 1) { - rpctlscd_verbose_out("rpctls_connect: " + rpctls_verbose_out("rpctls_connect: " "SSL_connect failed %d\n", ret); SSL_free(ssl); return (NULL); } cert = SSL_get_peer_certificate(ssl); if (cert == NULL) { - rpctlscd_verbose_out("rpctls_connect: get peer" + rpctls_verbose_out("rpctls_connect: get peer" " certificate failed\n"); SSL_free(ssl); return (NULL); } gethostret = rpctls_gethost(s, sad, hostnam, sizeof(hostnam)); if (gethostret == 0) hostnam[0] = '\0'; ret = SSL_get_verify_result(ssl); if (ret == X509_V_OK && (rpctls_verify_cafile != NULL || rpctls_verify_capath != NULL) && (gethostret == 0 || - rpctls_checkhost(sad, cert) != 1)) + rpctls_checkhost(sad, cert, X509_CHECK_FLAG_NO_WILDCARDS) != 1)) ret = X509_V_ERR_HOSTNAME_MISMATCH; if (ret != X509_V_OK && (rpctls_verify_cafile != NULL || rpctls_verify_capath != NULL)) { if (ret != X509_V_OK) { cp = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0); cp2 = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0); if (rpctls_debug_level == 0) syslog(LOG_INFO | LOG_DAEMON, "rpctls_connect: client IP %s " "issuerName=%s subjectName=%s verify " "failed %s\n", hostnam, cp, cp2, X509_verify_cert_error_string(ret)); else fprintf(stderr, "rpctls_connect: client IP %s " "issuerName=%s subjectName=%s verify " "failed %s\n", hostnam, cp, cp2, X509_verify_cert_error_string(ret)); } X509_free(cert); SSL_free(ssl); return (NULL); } /* Check to see if ktls is enabled on the connection. */ ret = BIO_get_ktls_send(SSL_get_wbio(ssl)); - rpctlscd_verbose_out("rpctls_connect: BIO_get_ktls_send=%d\n", ret); + rpctls_verbose_out("rpctls_connect: BIO_get_ktls_send=%d\n", ret); if (ret != 0) { ret = BIO_get_ktls_recv(SSL_get_rbio(ssl)); - rpctlscd_verbose_out("rpctls_connect: BIO_get_ktls_recv=%d\n", + rpctls_verbose_out("rpctls_connect: BIO_get_ktls_recv=%d\n", ret); } if (ret == 0) { if (rpctls_debug_level == 0) syslog(LOG_ERR, "ktls not working\n"); else fprintf(stderr, "ktls not working\n"); X509_free(cert); SSL_free(ssl); return (NULL); } if (ret == X509_V_OK && (rpctls_verify_cafile != NULL || rpctls_verify_capath != NULL) && rpctls_crlfile != NULL) *certp = cert; else X509_free(cert); return (ssl); } -/* - * Get the server's IP address. - */ -static int -rpctls_gethost(int s, struct sockaddr *sad, char *hostip, size_t hostlen) -{ - socklen_t slen; - int ret; - - slen = sizeof(struct sockaddr_storage); - if (getpeername(s, sad, &slen) < 0) - return (0); - ret = 0; - if (getnameinfo((const struct sockaddr *)sad, - sad->sa_len, hostip, hostlen, - NULL, 0, NI_NUMERICHOST) == 0) { - rpctlscd_verbose_out("rpctls_gethost: %s\n", - hostip); - ret = 1; - } - return (ret); -} - -/* - * Check a server IP address against any host address in the - * certificate. Basically getnameinfo(3) and - * X509_check_host(). - */ -static int -rpctls_checkhost(struct sockaddr *sad, X509 *cert) -{ - char hostnam[NI_MAXHOST]; - int ret; - - if (getnameinfo((const struct sockaddr *)sad, - sad->sa_len, hostnam, sizeof(hostnam), - NULL, 0, NI_NAMEREQD) != 0) - return (0); - rpctlscd_verbose_out("rpctls_checkhost: DNS %s\n", - hostnam); - ret = X509_check_host(cert, hostnam, strlen(hostnam), - X509_CHECK_FLAG_NO_WILDCARDS, NULL); - return (ret); -} - -/* - * (re)load the CRLfile into the certificate verification store. - */ -static int -rpctls_loadcrlfile(SSL_CTX *ctx) -{ - X509_STORE *certstore; - X509_LOOKUP *certlookup; - int ret; - - if ((rpctls_verify_cafile != NULL || - rpctls_verify_capath != NULL) && - rpctls_crlfile != NULL) { - certstore = SSL_CTX_get_cert_store(ctx); - certlookup = X509_STORE_add_lookup( - certstore, X509_LOOKUP_file()); - ret = 0; - if (certlookup != NULL) - ret = X509_load_crl_file(certlookup, - rpctls_crlfile, X509_FILETYPE_PEM); - if (ret != 0) - ret = X509_STORE_set_flags(certstore, - X509_V_FLAG_CRL_CHECK | - X509_V_FLAG_CRL_CHECK_ALL); - if (ret == 0) { - rpctlscd_verbose_out( - "rpctls_loadcrlfile: Can't" - " load CRLfile=%s\n", - rpctls_crlfile); - return (ret); - } - } - return (1); -} - static void rpctls_huphandler(int sig __unused) { rpctls_gothup = true; -} - -/* - * Read the CRL file and check for any extant connections - * that might now be revoked. - */ -static void -rpctls_checkcrl(void) -{ - struct ssl_entry *slp; - BIO *infile; - X509_CRL *crl; - X509_REVOKED *revoked; - int ret; - - if (rpctls_crlfile == NULL || (rpctls_verify_cafile == NULL && - rpctls_verify_capath == NULL)) - return; - infile = BIO_new(BIO_s_file()); - if (infile == NULL) { - rpctlscd_verbose_out("rpctls_checkcrl: Cannot BIO_new\n"); - return; - } - ret = BIO_read_filename(infile, rpctls_crlfile); - if (ret != 1) { - rpctlscd_verbose_out("rpctls_checkcrl: Cannot read CRL file\n"); - BIO_free(infile); - return; - } - - for (crl = PEM_read_bio_X509_CRL(infile, NULL, NULL, ""); - crl != NULL; crl = PEM_read_bio_X509_CRL(infile, NULL, NULL, "")) { - LIST_FOREACH(slp, &rpctls_ssllist, next) { - if (slp->cert != NULL) { - ret = X509_CRL_get0_by_cert(crl, &revoked, - slp->cert); -rpctlscd_verbose_out("get0_by_cert=%d\n", ret); - /* - * Do a shutdown on the socket, so that it - * can no longer be used. The kernel RPC - * code will notice the socket is disabled - * and will do a disconnect upcall, which will - * close the socket. - */ - if (ret == 1) { - shutdown(slp->s, SHUT_WR); - slp->shutoff = true; - } - } - } - X509_CRL_free(crl); - } - BIO_free(infile); } Index: projects/nfs-over-tls/usr.sbin/rpc.tlsservd/Makefile =================================================================== --- projects/nfs-over-tls/usr.sbin/rpc.tlsservd/Makefile (revision 365865) +++ projects/nfs-over-tls/usr.sbin/rpc.tlsservd/Makefile (revision 365866) @@ -1,32 +1,32 @@ # $FreeBSD$ .include PROG= rpc.tlsservd MAN= rpc.tlsservd.8 -SRCS= rpc.tlsservd.c rpctlssd.h rpctlssd_svc.c rpctlssd_xdr.c +SRCS= rpc.tlsservd.c rpc.tlscommon.c rpctlssd.h rpctlssd_svc.c rpctlssd_xdr.c CFLAGS+= -I. CFLAGS+= -I/usr/ktls/include LDFLAGS+= -L/usr/ktls/lib LIBADD= ssl crypto util CLEANFILES= rpctlssd_svc.c rpctlssd_xdr.c rpctlssd.h RPCSRC= ${SRCTOP}/sys/rpc/rpcsec_tls/rpctlssd.x RPCGEN= RPCGEN_CPP=${CPP:Q} rpcgen -L -C -M rpctlssd_svc.c: ${RPCSRC} rpctlssd.h ${RPCGEN} -m -o ${.TARGET} ${RPCSRC} rpctlssd_xdr.c: ${RPCSRC} rpctlssd.h ${RPCGEN} -c -o ${.TARGET} ${RPCSRC} rpctlssd.h: ${RPCSRC} ${RPCGEN} -h -o ${.TARGET} ${RPCSRC} .PATH: ${SRCTOP}/sys/rpc/rpcsec_tls .include Index: projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlscommon.c =================================================================== --- projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlscommon.c (nonexistent) +++ projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlscommon.c (revision 365866) @@ -0,0 +1,305 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Rick Macklem + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include "rpctlssd.h" +#include "rpc.tlscommon.h" + +/* Global variables defined in the daemons. */ +extern bool rpctls_gothup; +extern bool rpctls_verbose; +extern int rpctls_debug_level; +extern SSL_CTX *rpctls_ctx; +extern const char *rpctls_verify_cafile; +extern const char *rpctls_verify_capath; +extern const char *rpctls_crlfile; +extern struct ssl_list rpctls_ssllist; + +/* + * How long to delay a reload of the CRL when there are RPC request(s) + * to process, in usec. Must be less than 1second. + */ +#define RELOADDELAY 250000 + +void +rpctls_svc_run(void) +{ + int ret; + struct timeval tv; + fd_set readfds; + uint64_t curtime, nexttime; + struct timespec tp; + sigset_t sighup_mask; + + /* Expand svc_run() here so that we can call rpctls_loadcrlfile(). */ + curtime = nexttime = 0; + sigemptyset(&sighup_mask); + sigaddset(&sighup_mask, SIGHUP); + for (;;) { + clock_gettime(CLOCK_MONOTONIC, &tp); + curtime = tp.tv_sec; + curtime = curtime * 1000000 + tp.tv_nsec / 1000; + sigprocmask(SIG_BLOCK, &sighup_mask, NULL); + if (rpctls_gothup && curtime >= nexttime) { + rpctls_gothup = false; + sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); + ret = rpctls_loadcrlfile(rpctls_ctx); + if (ret != 0) + rpctls_checkcrl(); + else + rpctls_verbose_out("rpc.tlsservd: Can't " + "reload CRLfile\n"); + clock_gettime(CLOCK_MONOTONIC, &tp); + nexttime = tp.tv_sec; + nexttime = nexttime * 1000000 + tp.tv_nsec / 1000 + + RELOADDELAY; + } else + sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); + + /* + * If a reload is pending, poll for received request(s), + * otherwise set a RELOADDELAY timeout, since a SIGHUP + * could be processed between the got_sighup test and + * the select() system call. + */ + tv.tv_sec = 0; + if (rpctls_gothup) + tv.tv_usec = 0; + else + tv.tv_usec = RELOADDELAY; + readfds = svc_fdset; + switch (select(svc_maxfd + 1, &readfds, NULL, NULL, &tv)) { + case -1: + if (errno == EINTR) { + /* Allow a reload now. */ + nexttime = 0; + continue; + } + syslog(LOG_ERR, "rpc.tls daemon died: select: %m"); + exit(1); + case 0: + /* Allow a reload now. */ + nexttime = 0; + continue; + default: + svc_getreqset(&readfds); + } + } +} + +/* + * (re)load the CRLfile into the certificate verification store. + */ +int +rpctls_loadcrlfile(SSL_CTX *ctx) +{ + X509_STORE *certstore; + X509_LOOKUP *certlookup; + int ret; + + if ((rpctls_verify_cafile != NULL || + rpctls_verify_capath != NULL) && + rpctls_crlfile != NULL) { + certstore = SSL_CTX_get_cert_store(ctx); + certlookup = X509_STORE_add_lookup( + certstore, X509_LOOKUP_file()); + ret = 0; + if (certlookup != NULL) + ret = X509_load_crl_file(certlookup, + rpctls_crlfile, X509_FILETYPE_PEM); + if (ret != 0) + ret = X509_STORE_set_flags(certstore, + X509_V_FLAG_CRL_CHECK | + X509_V_FLAG_CRL_CHECK_ALL); + if (ret == 0) { + rpctls_verbose_out( + "rpctls_loadcrlfile: Can't" + " load CRLfile=%s\n", + rpctls_crlfile); + return (ret); + } + } + return (1); +} + +/* + * Read the CRL file and check for any extant connections + * that might now be revoked. + */ +void +rpctls_checkcrl(void) +{ + struct ssl_entry *slp; + BIO *infile; + X509_CRL *crl; + X509_REVOKED *revoked; + char *cp, *cp2; + int ret; + + if (rpctls_crlfile == NULL || (rpctls_verify_cafile == NULL && + rpctls_verify_capath == NULL)) + return; + infile = BIO_new(BIO_s_file()); + if (infile == NULL) { + rpctls_verbose_out("rpctls_checkcrl: Cannot BIO_new\n"); + return; + } + ret = BIO_read_filename(infile, rpctls_crlfile); + if (ret != 1) { + rpctls_verbose_out("rpctls_checkcrl: Cannot read CRL file\n"); + BIO_free(infile); + return; + } + + for (crl = PEM_read_bio_X509_CRL(infile, NULL, NULL, ""); + crl != NULL; crl = PEM_read_bio_X509_CRL(infile, NULL, NULL, "")) { + LIST_FOREACH(slp, &rpctls_ssllist, next) { + if (slp->cert != NULL) { + ret = X509_CRL_get0_by_cert(crl, &revoked, + slp->cert); +rpctls_verbose_out("get0_by_cert=%d\n", ret); + /* + * Do a shutdown on the socket, so that it + * can no longer be used. The kernel RPC + * code will notice the socket is disabled + * and will do a disconnect upcall, which will + * close the socket. + */ + if (ret == 1) { + cp2 = X509_NAME_oneline( + X509_get_subject_name(slp->cert), + NULL, 0); + cp = X509_NAME_oneline( + X509_get_issuer_name(slp->cert), + NULL, 0); + if (rpctls_debug_level == 0) + syslog(LOG_INFO | LOG_DAEMON, + "rpctls_daemon: Certificate" + " Revoked " + "issuerName=%s " + "subjectName=%s: " + "TCP connection closed", + cp, cp2); + else + fprintf(stderr, + "rpctls_daemon: Certificate" + " Revoked " + "issuerName=%s " + "subjectName=%s: " + "TCP connection closed", + cp, cp2); + shutdown(slp->s, SHUT_WR); + slp->shutoff = true; + } + } + } + X509_CRL_free(crl); + } + BIO_free(infile); +} + +void +rpctls_verbose_out(const char *fmt, ...) +{ + va_list ap; + + if (rpctls_verbose) { + va_start(ap, fmt); + if (rpctls_debug_level == 0) + vsyslog(LOG_INFO | LOG_DAEMON, fmt, ap); + else + vfprintf(stderr, fmt, ap); + va_end(ap); + } +} + +/* + * Check a IP address against any host address in the + * certificate. Basically getnameinfo(3) and + * X509_check_host(). + */ +int +rpctls_checkhost(struct sockaddr *sad, X509 *cert, unsigned int wildcard) +{ + char hostnam[NI_MAXHOST]; + int ret; + + if (getnameinfo((const struct sockaddr *)sad, + sad->sa_len, hostnam, sizeof(hostnam), + NULL, 0, NI_NAMEREQD) != 0) + return (0); + rpctls_verbose_out("rpctls_checkhost: DNS %s\n", + hostnam); + ret = X509_check_host(cert, hostnam, strlen(hostnam), + wildcard, NULL); + return (ret); +} + +/* + * Get the peer's IP address. + */ +int +rpctls_gethost(int s, struct sockaddr *sad, char *hostip, size_t hostlen) +{ + socklen_t slen; + int ret; + + slen = sizeof(struct sockaddr_storage); + if (getpeername(s, sad, &slen) < 0) + return (0); + ret = 0; + if (getnameinfo((const struct sockaddr *)sad, + sad->sa_len, hostip, hostlen, + NULL, 0, NI_NUMERICHOST) == 0) { + rpctls_verbose_out("rpctls_gethost: %s\n", + hostip); + ret = 1; + } + return (ret); +} Property changes on: projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlscommon.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlscommon.h =================================================================== --- projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlscommon.h (nonexistent) +++ projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlscommon.h (revision 365866) @@ -0,0 +1,56 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Rick Macklem + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * Functions in rpc.tlscommon.c used by both rpc.tlsservd.c and rpc.tlsclntd.c. + */ +int rpctls_gethost(int s, struct sockaddr *sad, + char *hostip, size_t hostlen); +int rpctls_checkhost(struct sockaddr *sad, X509 *cert, + unsigned int wildcard); +int rpctls_loadcrlfile(SSL_CTX *ctx); +void rpctls_checkcrl(void); +void rpctls_verbose_out(const char *fmt, ...); +void rpctls_svc_run(void); + +/* + * A linked list of all current "SSL *"s and socket "fd"s + * for kernel RPC TLS connections is maintained. + * The "refno" field is a unique 64bit value used to + * identify which entry a kernel RPC upcall refers to. + */ +LIST_HEAD(ssl_list, ssl_entry); +struct ssl_entry { + LIST_ENTRY(ssl_entry) next; + uint64_t refno; + int s; + bool shutoff; + SSL *ssl; + X509 *cert; +}; Property changes on: projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlscommon.h ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlsservd.c =================================================================== --- projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlsservd.c (revision 365865) +++ projects/nfs-over-tls/usr.sbin/rpc.tlsservd/rpc.tlsservd.c (revision 365866) @@ -1,1115 +1,885 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2008 Isilon Inc http://www.isilon.com/ * Authors: Doug Rabson * Developed with Red Inc: Alfred Perlstein * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* Modified from gssd.c for the server side of kernel RPC-over-TLS. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "rpctlssd.h" +#include "rpc.tlscommon.h" #ifndef _PATH_RPCTLSSDSOCK #define _PATH_RPCTLSSDSOCK "/var/run/rpc.tlsservd.sock" #endif #ifndef _PATH_CERTANDKEY #define _PATH_CERTANDKEY "/etc/rpc.tlsservd/" #endif #ifndef _PATH_RPCTLSSDPID #define _PATH_RPCTLSSDPID "/var/run/rpc.tlsservd.pid" #endif #ifndef _PREFERRED_CIPHERS #define _PREFERRED_CIPHERS "AES128-GCM-SHA256" #endif -/* - * How long to delay a reload of the CRL when there are RPC request(s) - * to process, in usec. Must be less than 1second. - */ -#define RELOADDELAY 250000 +/* Global variables also used by rpc.tlscommon.c. */ +int rpctls_debug_level; +bool rpctls_verbose; +SSL_CTX *rpctls_ctx = NULL; +const char *rpctls_verify_cafile = NULL; +const char *rpctls_verify_capath = NULL; +const char *rpctls_crlfile = NULL; +bool rpctls_gothup = false; +struct ssl_list rpctls_ssllist; static struct pidfh *rpctls_pfh = NULL; -static int rpctls_debug_level; -static bool rpctls_verbose; -static SSL_CTX *rpctls_ctx = NULL; static bool rpctls_do_mutual = false; -static const char *rpctls_verify_cafile = NULL; -static const char *rpctls_verify_capath = NULL; -static const char *rpctls_crlfile = NULL; static const char *rpctls_certdir = _PATH_CERTANDKEY; static bool rpctls_comparehost = false; static unsigned int rpctls_wildcard = X509_CHECK_FLAG_NO_WILDCARDS; static uint64_t rpctls_ssl_refno = 0; static uint64_t rpctls_ssl_sec = 0; static uint64_t rpctls_ssl_usec = 0; -static bool rpctls_gothup = false; static bool rpctls_cnuser = false; static char *rpctls_dnsname; static const char *rpctls_cnuseroid = "1.3.6.1.4.1.2238.1.1.1"; -/* - * A linked list of all current "SSL *"s and socket "fd"s - * for kernel RPC TLS connections is maintained. - * The "refno" field is a unique 64bit value used to - * identify which entry a kernel RPC upcall refers to. - */ -LIST_HEAD(ssl_list, ssl_entry); -struct ssl_entry { - LIST_ENTRY(ssl_entry) next; - uint64_t refno; - int s; - bool shutoff; - SSL *ssl; - X509 *cert; -}; -static struct ssl_list rpctls_ssllist; - static void rpctlssd_terminate(int); static SSL_CTX *rpctls_setup_ssl(const char *certdir); static SSL *rpctls_server(SSL_CTX *ctx, int s, uint32_t *flags, uint32_t *uidp, int *ngrps, uint32_t *gidp, X509 **certp); -static int rpctls_gethost(int s, struct sockaddr *sad, - char *hostip, size_t hostlen); -static int rpctls_checkhost(struct sockaddr *sad, X509 *cert); -static int rpctls_loadcrlfile(SSL_CTX *ctx); static int rpctls_cnname(X509 *cert, uint32_t *uidp, int *ngrps, uint32_t *gidp); static char *rpctls_getdnsname(char *dnsname); static void rpctls_huphandler(int sig __unused); -static void rpctls_checkcrl(void); -static void rpctlssd_verbose_out(const char *fmt, ...); extern void rpctlssd_1(struct svc_req *rqstp, SVCXPRT *transp); static struct option longopts[] = { { "certdir", required_argument, NULL, 'D' }, { "debuglevel", no_argument, NULL, 'd' }, { "checkhost", no_argument, NULL, 'h' }, { "verifylocs", required_argument, NULL, 'l' }, { "mutualverf", no_argument, NULL, 'm' }, { "domain", required_argument, NULL, 'n' }, { "verifydir", required_argument, NULL, 'p' }, { "crl", required_argument, NULL, 'r' }, { "certuser", no_argument, NULL, 'u' }, { "verbose", no_argument, NULL, 'v' }, { "multiwild", no_argument, NULL, 'W' }, { "singlewild", no_argument, NULL, 'w' }, { NULL, 0, NULL, 0 } }; int main(int argc, char **argv) { /* * We provide an RPC service on a local-domain socket. The * kernel rpctls code will upcall to this daemon to do the initial * TLS handshake. */ struct sockaddr_un sun; - int ch, debug, fd, oldmask, ret; + int ch, debug, fd, oldmask; SVCXPRT *xprt; struct timeval tm; struct timezone tz; char hostname[MAXHOSTNAMELEN + 2]; pid_t otherpid; - fd_set readfds; - uint64_t curtime, nexttime; - struct timespec tp; - sigset_t sighup_mask; /* Check that another rpctlssd isn't already running. */ rpctls_pfh = pidfile_open(_PATH_RPCTLSSDPID, 0600, &otherpid); if (rpctls_pfh == NULL) { if (errno == EEXIST) errx(1, "rpctlssd already running, pid: %d.", otherpid); warn("cannot open or create pidfile"); } if (modfind("ktls_ocf") < 0) { /* Not present in kernel, try loading it */ if (kldload("ktls_ocf") < 0 || modfind("ktls_ocf") < 0) errx(1, "Cannot load ktls_ocf"); } if (modfind("aesni") < 0) { /* Not present in kernel, try loading it */ kldload("aesni"); } /* Get the time when this daemon is started. */ gettimeofday(&tm, &tz); rpctls_ssl_sec = tm.tv_sec; rpctls_ssl_usec = tm.tv_usec; /* Set the dns name for the server. */ rpctls_dnsname = rpctls_getdnsname(hostname); if (rpctls_dnsname == NULL) { strcpy(hostname, "@default.domain"); rpctls_dnsname = hostname; } fprintf(stderr, "dnsname=%s\n", rpctls_dnsname); debug = 0; rpctls_verbose = false; while ((ch = getopt_long(argc, argv, "D:dhl:n:mp:r:uvWw", longopts, NULL)) != -1) { switch (ch) { case 'D': rpctls_certdir = optarg; break; case 'd': rpctls_debug_level++; break; case 'h': rpctls_comparehost = true; break; case 'l': rpctls_verify_cafile = optarg; break; case 'm': rpctls_do_mutual = true; break; case 'n': hostname[0] = '@'; strlcpy(&hostname[1], optarg, MAXHOSTNAMELEN + 1); rpctls_dnsname = hostname; break; case 'p': rpctls_verify_capath = optarg; break; case 'r': rpctls_crlfile = optarg; break; case 'u': rpctls_cnuser = true; break; case 'v': rpctls_verbose = true; break; case 'W': if (rpctls_wildcard != X509_CHECK_FLAG_NO_WILDCARDS) errx(1, "options -w and -W are mutually " "exclusive"); rpctls_wildcard = X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS; break; case 'w': if (rpctls_wildcard != X509_CHECK_FLAG_NO_WILDCARDS) errx(1, "options -w and -W are mutually " "exclusive"); rpctls_wildcard = 0; break; default: fprintf(stderr, "usage: %s " "[-D/--certdir certdir] [-d/--debuglevel] " "[-h/--checkhost] " "[-l/--verifylocs CAfile] [-m/--mutualverf] " "[-n/--domain domain_name] " "[-p/--verifydir CApath] [-r/--crl CRLfile] " "[-u/--certuser] [-v/--verbose] [-W/--multiwild] " "[-w/--singlewild]\n", argv[0]); exit(1); } } if (rpctls_do_mutual && rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL) errx(1, "-m requires the -l and/or " "-p options"); if (rpctls_comparehost && (!rpctls_do_mutual || (rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL))) errx(1, "-h requires the -m plus the " "-l and/or -p options"); if (!rpctls_comparehost && rpctls_wildcard != X509_CHECK_FLAG_NO_WILDCARDS) errx(1, "The -w or -W options require the -h option"); if (rpctls_cnuser && (!rpctls_do_mutual || (rpctls_verify_cafile == NULL && rpctls_verify_capath == NULL))) errx(1, "-u requires the -m plus the " "-l and/or -p options"); if (modfind("krpc") < 0) { /* Not present in kernel, try loading it */ if (kldload("krpc") < 0 || modfind("krpc") < 0) errx(1, "Kernel RPC is not available"); } if (rpctls_debug_level == 0) { if (daemon(0, 0) != 0) err(1, "Can't daemonize"); signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGHUP, SIG_IGN); } signal(SIGTERM, rpctlssd_terminate); signal(SIGPIPE, SIG_IGN); signal(SIGHUP, rpctls_huphandler); pidfile_write(rpctls_pfh); memset(&sun, 0, sizeof sun); sun.sun_family = AF_LOCAL; unlink(_PATH_RPCTLSSDSOCK); strcpy(sun.sun_path, _PATH_RPCTLSSDSOCK); sun.sun_len = SUN_LEN(&sun); fd = socket(AF_LOCAL, SOCK_STREAM, 0); if (fd < 0) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't create local rpctlssd socket"); exit(1); } err(1, "Can't create local rpctlssd socket"); } oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO); if (bind(fd, (struct sockaddr *)&sun, sun.sun_len) < 0) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't bind local rpctlssd socket"); exit(1); } err(1, "Can't bind local rpctlssd socket"); } umask(oldmask); if (listen(fd, SOMAXCONN) < 0) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't listen on local rpctlssd socket"); exit(1); } err(1, "Can't listen on local rpctlssd socket"); } xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE); if (!xprt) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't create transport for local rpctlssd socket"); exit(1); } err(1, "Can't create transport for local rpctlssd socket"); } if (!svc_reg(xprt, RPCTLSSD, RPCTLSSDVERS, rpctlssd_1, NULL)) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't register service for local rpctlssd socket"); exit(1); } err(1, "Can't register service for local rpctlssd socket"); } rpctls_ctx = rpctls_setup_ssl(rpctls_certdir); if (rpctls_ctx == NULL) { if (rpctls_debug_level == 0) { syslog(LOG_ERR, "Can't create SSL context"); exit(1); } err(1, "Can't create SSL context"); } rpctls_gothup = false; LIST_INIT(&rpctls_ssllist); rpctls_syscall(RPCTLS_SYSC_SRVSETPATH, _PATH_RPCTLSSDSOCK); - /* Expand svc_run() here so that we can call rpctls_loadcrlfile(). */ - curtime = nexttime = 0; - sigemptyset(&sighup_mask); - sigaddset(&sighup_mask, SIGHUP); - for (;;) { - clock_gettime(CLOCK_MONOTONIC, &tp); - curtime = tp.tv_sec; - curtime = curtime * 1000000 + tp.tv_nsec / 1000; - sigprocmask(SIG_BLOCK, &sighup_mask, NULL); - if (rpctls_gothup && curtime >= nexttime) { - rpctls_gothup = false; - sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); - ret = rpctls_loadcrlfile(rpctls_ctx); - if (ret != 0) - rpctls_checkcrl(); - else - rpctlssd_verbose_out("rpc.tlsservd: Can't " - "reload CRLfile\n"); - clock_gettime(CLOCK_MONOTONIC, &tp); - nexttime = tp.tv_sec; - nexttime = nexttime * 1000000 + tp.tv_nsec / 1000 + - RELOADDELAY; - } else - sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); + rpctls_svc_run(); - /* - * If a reload is pending, poll for received request(s), - * otherwise set a RELOADDELAY timeout, since a SIGHUP - * could be processed between the got_sighup test and - * the select() system call. - */ - tm.tv_sec = 0; - if (rpctls_gothup) - tm.tv_usec = 0; - else - tm.tv_usec = RELOADDELAY; - readfds = svc_fdset; - switch (select(svc_maxfd + 1, &readfds, NULL, NULL, &tm)) { - case -1: - if (errno == EINTR) { - /* Allow a reload now. */ - nexttime = 0; - continue; - } - syslog(LOG_ERR, "rpc.tlsservd died: select: %m"); - exit(1); - case 0: - /* Allow a reload now. */ - nexttime = 0; - continue; - default: - svc_getreqset(&readfds); - } - } - rpctls_syscall(RPCTLS_SYSC_SRVSHUTDOWN, ""); SSL_CTX_free(rpctls_ctx); EVP_cleanup(); return (0); } -static void -rpctlssd_verbose_out(const char *fmt, ...) -{ - va_list ap; - - if (rpctls_verbose) { - va_start(ap, fmt); - if (rpctls_debug_level == 0) - vsyslog(LOG_INFO | LOG_DAEMON, fmt, ap); - else - vfprintf(stderr, fmt, ap); - va_end(ap); - } -} - bool_t rpctlssd_null_1_svc(void *argp, void *result, struct svc_req *rqstp) { - rpctlssd_verbose_out("rpctlssd_null_svc: done\n"); + rpctls_verbose_out("rpctlssd_null_svc: done\n"); return (TRUE); } bool_t rpctlssd_connect_1_svc(void *argp, struct rpctlssd_connect_res *result, struct svc_req *rqstp) { int ngrps, s; SSL *ssl; uint32_t flags; struct ssl_entry *newslp; uint32_t uid; uint32_t *gidp; X509 *cert; - rpctlssd_verbose_out("rpctlsd_connect_svc: started\n"); + rpctls_verbose_out("rpctlsd_connect_svc: started\n"); memset(result, 0, sizeof(*result)); /* Get the socket fd from the kernel. */ s = rpctls_syscall(RPCTLS_SYSC_SRVSOCKET, ""); -rpctlssd_verbose_out("rpctlsd_connect_svc s=%d\n", s); +rpctls_verbose_out("rpctlsd_connect_svc s=%d\n", s); if (s < 0) return (FALSE); /* Do the server side of a TLS handshake. */ gidp = calloc(NGROUPS, sizeof(*gidp)); ssl = rpctls_server(rpctls_ctx, s, &flags, &uid, &ngrps, gidp, &cert); if (ssl == NULL) { free(gidp); - rpctlssd_verbose_out("rpctlssd_connect_svc: ssl " + rpctls_verbose_out("rpctlssd_connect_svc: ssl " "accept failed\n"); /* * For RPC-over-TLS, this upcall is expected * to close off the socket upon handshake failure. */ close(s); return (FALSE); } else { - rpctlssd_verbose_out("rpctlssd_connect_svc: " + rpctls_verbose_out("rpctlssd_connect_svc: " "succeeded flags=0x%x\n", flags); result->flags = flags; result->sec = rpctls_ssl_sec; result->usec = rpctls_ssl_usec; result->ssl = ++rpctls_ssl_refno; /* Hard to believe this could ever wrap around.. */ if (rpctls_ssl_refno == 0) result->ssl = ++rpctls_ssl_refno; if ((flags & RPCTLS_FLAGS_CERTUSER) != 0) { result->uid = uid; result->gid.gid_len = ngrps; result->gid.gid_val = gidp; } else { result->uid = 0; result->gid.gid_len = 0; result->gid.gid_val = gidp; } } /* Maintain list of all current SSL *'s */ newslp = malloc(sizeof(*newslp)); newslp->ssl = ssl; newslp->s = s; newslp->shutoff = false; newslp->refno = rpctls_ssl_refno; newslp->cert = cert; LIST_INSERT_HEAD(&rpctls_ssllist, newslp, next); return (TRUE); } bool_t rpctlssd_handlerecord_1_svc(struct rpctlssd_handlerecord_arg *argp, struct rpctlssd_handlerecord_res *result, struct svc_req *rqstp) { struct ssl_entry *slp; int ret; char junk; slp = NULL; if (argp->sec == rpctls_ssl_sec && argp->usec == rpctls_ssl_usec) { LIST_FOREACH(slp, &rpctls_ssllist, next) { if (slp->refno == argp->ssl) break; } } if (slp != NULL) { - rpctlssd_verbose_out("rpctlssd_handlerecord fd=%d\n", + rpctls_verbose_out("rpctlssd_handlerecord fd=%d\n", slp->s); /* * An SSL_read() of 0 bytes should fail, but it should * handle the non-application data record before doing so. */ ret = SSL_read(slp->ssl, &junk, 0); if (ret <= 0) { /* Check to see if this was a close alert. */ ret = SSL_get_shutdown(slp->ssl); -rpctlssd_verbose_out("get_shutdown=%d\n", ret); +rpctls_verbose_out("get_shutdown=%d\n", ret); if ((ret & (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN)) == SSL_RECEIVED_SHUTDOWN) SSL_shutdown(slp->ssl); } else { if (rpctls_debug_level == 0) syslog(LOG_ERR, "SSL_read returned %d", ret); else fprintf(stderr, "SSL_read returned %d\n", ret); } result->reterr = RPCTLSERR_OK; } else result->reterr = RPCTLSERR_NOSSL; return (TRUE); } bool_t rpctlssd_disconnect_1_svc(struct rpctlssd_disconnect_arg *argp, struct rpctlssd_disconnect_res *result, struct svc_req *rqstp) { struct ssl_entry *slp; int ret; slp = NULL; if (argp->sec == rpctls_ssl_sec && argp->usec == rpctls_ssl_usec) { LIST_FOREACH(slp, &rpctls_ssllist, next) { if (slp->refno == argp->ssl) break; } } if (slp != NULL) { - rpctlssd_verbose_out("rpctlssd_disconnect fd=%d closed\n", + rpctls_verbose_out("rpctlssd_disconnect fd=%d closed\n", slp->s); LIST_REMOVE(slp, next); if (!slp->shutoff) { ret = SSL_get_shutdown(slp->ssl); -rpctlssd_verbose_out("get_shutdown1=%d\n", ret); +rpctls_verbose_out("get_shutdown1=%d\n", ret); /* * Do an SSL_shutdown() unless a close alert has * already been sent. */ if ((ret & SSL_SENT_SHUTDOWN) == 0) SSL_shutdown(slp->ssl); } SSL_free(slp->ssl); if (slp->cert != NULL) X509_free(slp->cert); /* * For RPC-over-TLS, this upcall is expected * to close off the socket. */ if (!slp->shutoff) shutdown(slp->s, SHUT_WR); close(slp->s); free(slp); result->reterr = RPCTLSERR_OK; } else result->reterr = RPCTLSERR_NOCLOSE; return (TRUE); } int rpctlssd_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result) { rpctlssd_connect_res *res; if (xdr_result == (xdrproc_t)xdr_rpctlssd_connect_res) { res = (rpctlssd_connect_res *)result; if (res->gid.gid_val != NULL) free(res->gid.gid_val); } return (TRUE); } static void rpctlssd_terminate(int sig __unused) { struct ssl_entry *slp; rpctls_syscall(RPCTLS_SYSC_SRVSHUTDOWN, ""); pidfile_remove(rpctls_pfh); LIST_FOREACH(slp, &rpctls_ssllist, next) shutdown(slp->s, SHUT_RD); exit(0); } /* Allow the handshake to proceed. */ static int rpctls_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) { return (1); } static SSL_CTX * rpctls_setup_ssl(const char *certdir) { SSL_CTX *ctx; char path[PATH_MAX]; size_t len, rlen; int ret; SSL_library_init(); SSL_load_error_strings(); OpenSSL_add_all_algorithms(); ctx = SSL_CTX_new(TLS_server_method()); if (ctx == NULL) { - rpctlssd_verbose_out("rpctls_setup_ssl: SSL_CTX_new failed\n"); + rpctls_verbose_out("rpctls_setup_ssl: SSL_CTX_new failed\n"); return (NULL); } SSL_CTX_set_ecdh_auto(ctx, 1); /* * Set preferred ciphers, since KERN_TLS only supports a * few of them. */ ret = SSL_CTX_set_cipher_list(ctx, _PREFERRED_CIPHERS); if (ret == 0) { - rpctlssd_verbose_out("rpctls_setup_ssl: " + rpctls_verbose_out("rpctls_setup_ssl: " "SSL_CTX_set_cipher_list failed to set any ciphers\n"); SSL_CTX_free(ctx); return (NULL); } /* Get the cert.pem and key.pem files from the directory certdir. */ len = strlcpy(path, certdir, sizeof(path)); rlen = sizeof(path) - len; if (strlcpy(&path[len], "cert.pem", rlen) != 8) { SSL_CTX_free(ctx); return (NULL); } ret = SSL_CTX_use_certificate_file(ctx, path, SSL_FILETYPE_PEM); if (ret != 1) { - rpctlssd_verbose_out("rpctls_setup_ssl: can't use certificate " + rpctls_verbose_out("rpctls_setup_ssl: can't use certificate " "file path=%s ret=%d\n", path, ret); SSL_CTX_free(ctx); return (NULL); } if (strlcpy(&path[len], "key.pem", rlen) != 7) { SSL_CTX_free(ctx); return (NULL); } ret = SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM); if (ret != 1) { - rpctlssd_verbose_out("rpctls_setup_ssl: Can't use private " + rpctls_verbose_out("rpctls_setup_ssl: Can't use private " "key path=%s ret=%d\n", path, ret); SSL_CTX_free(ctx); return (NULL); } /* Set Mutual authentication, as required. */ if (rpctls_do_mutual) { if (rpctls_verify_cafile != NULL || rpctls_verify_capath != NULL) { if (rpctls_crlfile != NULL) { ret = rpctls_loadcrlfile(ctx); if (ret == 0) { - rpctlssd_verbose_out("rpctls_setup_ssl:" + rpctls_verbose_out("rpctls_setup_ssl:" " Load CRLfile failed\n"); SSL_CTX_free(ctx); return (NULL); } } #if OPENSSL_VERSION_NUMBER >= 0x30000000 ret = 1; if (rpctls_verify_cafile != NULL) ret = SSL_CTX_load_verify_file(ctx, rpctls_verify_cafile); if (ret != 0 && rpctls_verify_capath != NULL) ret = SSL_CTX_load_verify_dir(ctx, rpctls_verify_capath); #else ret = SSL_CTX_load_verify_locations(ctx, rpctls_verify_cafile, rpctls_verify_capath); #endif if (ret == 0) { - rpctlssd_verbose_out("rpctls_setup_ssl: " + rpctls_verbose_out("rpctls_setup_ssl: " "Can't load verify locations\n"); SSL_CTX_free(ctx); return (NULL); } if (rpctls_verify_cafile != NULL) SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file( rpctls_verify_cafile)); } SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, rpctls_verify_callback); } return (ctx); } static SSL * rpctls_server(SSL_CTX *ctx, int s, uint32_t *flags, uint32_t *uidp, int *ngrps, uint32_t *gidp, X509 **certp) { SSL *ssl; X509 *cert; struct sockaddr *sad; struct sockaddr_storage ad; char hostnam[NI_MAXHOST]; int gethostret, ret; char *cp, *cp2; *flags = 0; *certp = NULL; sad = (struct sockaddr *)&ad; ssl = SSL_new(ctx); if (ssl == NULL) { - rpctlssd_verbose_out("rpctls_server: SSL_new failed\n"); + rpctls_verbose_out("rpctls_server: SSL_new failed\n"); return (NULL); } if (SSL_set_fd(ssl, s) != 1) { - rpctlssd_verbose_out("rpctls_server: SSL_set_fd failed\n"); + rpctls_verbose_out("rpctls_server: SSL_set_fd failed\n"); SSL_free(ssl); return (NULL); } ret = SSL_accept(ssl); if (ret != 1) { - rpctlssd_verbose_out("rpctls_server: SSL_accept " + rpctls_verbose_out("rpctls_server: SSL_accept " "failed ret=%d\n", ret); SSL_free(ssl); return (NULL); } *flags |= RPCTLS_FLAGS_HANDSHAKE; if (rpctls_do_mutual) { cert = SSL_get_peer_certificate(ssl); if (cert != NULL) { gethostret = rpctls_gethost(s, sad, hostnam, sizeof(hostnam)); if (gethostret == 0) hostnam[0] = '\0'; cp2 = X509_NAME_oneline( X509_get_subject_name(cert), NULL, 0); -rpctlssd_verbose_out("%s\n", cp2); +rpctls_verbose_out("%s\n", cp2); *flags |= RPCTLS_FLAGS_GOTCERT; ret = SSL_get_verify_result(ssl); if (ret != X509_V_OK) { cp = X509_NAME_oneline( X509_get_issuer_name(cert), NULL, 0); if (rpctls_debug_level == 0) syslog(LOG_INFO | LOG_DAEMON, "rpctls_server: client IP %s " "issuerName=%s subjectName=%s" " verify failed %s\n", hostnam, cp, cp2, X509_verify_cert_error_string(ret)); else fprintf(stderr, "rpctls_server: client IP %s " "issuerName=%s subjectName=%s" " verify failed %s\n", hostnam, cp, cp2, X509_verify_cert_error_string(ret)); } if (ret == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT || ret == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) *flags |= RPCTLS_FLAGS_SELFSIGNED; else if (ret == X509_V_OK) { if (rpctls_comparehost) { ret = 0; if (gethostret != 0) ret = rpctls_checkhost(sad, - cert); + cert, rpctls_wildcard); if (ret != 1) { *flags |= RPCTLS_FLAGS_DISABLED; - rpctlssd_verbose_out( + rpctls_verbose_out( "rpctls_server: " "checkhost " "failed\n"); } } if (rpctls_cnuser) { ret = rpctls_cnname(cert, uidp, ngrps, gidp); if (ret != 0) *flags |= RPCTLS_FLAGS_CERTUSER; } *flags |= RPCTLS_FLAGS_VERIFIED; *certp = cert; cert = NULL; } if (cert != NULL) X509_free(cert); } else - rpctlssd_verbose_out("rpctls_server: " + rpctls_verbose_out("rpctls_server: " "No peer certificate\n"); } /* Check to see that ktls is working for the connection. */ ret = BIO_get_ktls_send(SSL_get_wbio(ssl)); - rpctlssd_verbose_out("rpctls_server: BIO_get_ktls_send=%d\n", ret); + rpctls_verbose_out("rpctls_server: BIO_get_ktls_send=%d\n", ret); if (ret != 0) { ret = BIO_get_ktls_recv(SSL_get_rbio(ssl)); - rpctlssd_verbose_out("rpctls_server: BIO_get_ktls_recv=%d\n", ret); + rpctls_verbose_out("rpctls_server: BIO_get_ktls_recv=%d\n", ret); } if (ret == 0) { if (rpctls_debug_level == 0) syslog(LOG_ERR, "ktls not working"); else fprintf(stderr, "ktls not working\n"); /* * The handshake has completed, so all that can be * done is disable the connection. */ *flags |= RPCTLS_FLAGS_DISABLED; } return (ssl); } /* - * Get the client's IP address. - */ -static int -rpctls_gethost(int s, struct sockaddr *sad, char *hostip, size_t hostlen) -{ - socklen_t slen; - int ret; - - slen = sizeof(struct sockaddr_storage); - if (getpeername(s, sad, &slen) < 0) - return (0); - ret = 0; - if (getnameinfo((const struct sockaddr *)sad, - sad->sa_len, hostip, hostlen, - NULL, 0, NI_NUMERICHOST) == 0) { - rpctlssd_verbose_out("rpctls_gethost: %s\n", - hostip); - ret = 1; - } - return (ret); -} - -/* - * Check a client IP address against any host address in the - * certificate. Basically getnameinfo(3) and - * X509_check_host(). - */ -static int -rpctls_checkhost(struct sockaddr *sad, X509 *cert) -{ - char hostnam[NI_MAXHOST]; - int ret; - - if (getnameinfo((const struct sockaddr *)sad, - sad->sa_len, hostnam, sizeof(hostnam), - NULL, 0, NI_NAMEREQD) != 0) - return (0); - rpctlssd_verbose_out("rpctls_checkhost: DNS %s\n", - hostnam); - ret = X509_check_host(cert, hostnam, strlen(hostnam), - rpctls_wildcard, NULL); - return (ret); -} - -/* * Acquire the dnsname for this server. */ static char * rpctls_getdnsname(char *hostname) { char *cp, *dnsname; struct addrinfo *aip, hints; int error; dnsname = NULL; if (gethostname(hostname, MAXHOSTNAMELEN) == 0) { if ((cp = strchr(hostname, '.')) != NULL && *(cp + 1) != '\0') { *cp = '@'; dnsname = cp; } else { memset((void *)&hints, 0, sizeof (hints)); hints.ai_flags = AI_CANONNAME; error = getaddrinfo(hostname, NULL, &hints, &aip); if (error == 0) { if (aip->ai_canonname != NULL && (cp = strchr(aip->ai_canonname, '.')) != NULL && *(cp + 1) != '\0') { hostname[0] = '@'; strlcpy(&hostname[1], cp + 1, MAXHOSTNAMELEN + 1); dnsname = hostname; } freeaddrinfo(aip); } } } return (dnsname); } /* * Check a commonName to see if it maps to "user@domain" and * acquire a for it if it does. */ static int rpctls_cnname(X509 *cert, uint32_t *uidp, int *ngrps, uint32_t *gidp) { char *cp, usern[1024 + 1]; struct passwd *pwd; gid_t gids[NGROUPS]; int i; GENERAL_NAMES *genlist; GENERAL_NAME *genname; OTHERNAME *val; /* First, find the otherName in the subjectAltName. */ genlist = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); -rpctlssd_verbose_out("genlist=%p\n", genlist); +rpctls_verbose_out("genlist=%p\n", genlist); if (genlist == NULL) return (0); val = NULL; for (i = 0; i < sk_GENERAL_NAME_num(genlist); i++) { genname = sk_GENERAL_NAME_value(genlist, i); if (genname->type != GEN_OTHERNAME) continue; val = genname->d.otherName; break; } if (val == NULL) return (0); -rpctlssd_verbose_out("fnd type=0x%x len=%d anstyp=0x%x data=%s\n", val->value->type, val->value->value.utf8string->length, val->value->value.utf8string->type, val->value->value.utf8string->data); +rpctls_verbose_out("fnd type=0x%x len=%d anstyp=0x%x data=%s\n", val->value->type, val->value->value.utf8string->length, val->value->value.utf8string->type, val->value->value.utf8string->data); /* Check to see that it is the correct OID. */ i = i2t_ASN1_OBJECT(usern, sizeof(usern), val->type_id); -rpctlssd_verbose_out("obj=%d str=%s\n", i, usern); +rpctls_verbose_out("obj=%d str=%s\n", i, usern); if (i != strlen(rpctls_cnuseroid) || memcmp(usern, rpctls_cnuseroid, i) != 0) { - rpctlssd_verbose_out("rpctls_cnname: invalid cnuser " + rpctls_verbose_out("rpctls_cnname: invalid cnuser " "oid len=%d val=%s\n", i, usern); return (0); } /* Sanity check the otherName. */ if (val->value->type != V_ASN1_UTF8STRING || val->value->value.utf8string->length < 3 || val->value->value.utf8string->length > sizeof(usern) - 1) { - rpctlssd_verbose_out("rpctls_cnname: invalid cnuser " + rpctls_verbose_out("rpctls_cnname: invalid cnuser " "type=%d\n", val->value->type); return (0); } /* Look for a "user" in the otherName */ memcpy(usern, val->value->value.utf8string->data, val->value->value.utf8string->length); usern[val->value->value.utf8string->length] = '\0'; - rpctlssd_verbose_out("rpctls_cnname: userstr %s\n", usern); + rpctls_verbose_out("rpctls_cnname: userstr %s\n", usern); /* Now, look for the @dnsname suffix in the commonName. */ cp = strcasestr(usern, rpctls_dnsname); if (cp == NULL) return (0); -rpctlssd_verbose_out("dns=%s\n", cp); +rpctls_verbose_out("dns=%s\n", cp); if (*(cp + strlen(rpctls_dnsname)) != '\0') return (0); *cp = '\0'; /* See if the "user" is in the passwd database. */ -rpctlssd_verbose_out("user=%s\n", usern); +rpctls_verbose_out("user=%s\n", usern); pwd = getpwnam(usern); if (pwd == NULL) return (0); -rpctlssd_verbose_out("pwname=%s\n", pwd->pw_name); +rpctls_verbose_out("pwname=%s\n", pwd->pw_name); *uidp = pwd->pw_uid; *ngrps = NGROUPS; if (getgrouplist(pwd->pw_name, pwd->pw_gid, gids, ngrps) < 0) return (0); for (i = 0; i < *ngrps; i++) gidp[i] = gids[i]; return (1); } -/* - * (re)load the CRLfile into the certificate verification store. - */ -static int -rpctls_loadcrlfile(SSL_CTX *ctx) -{ - X509_STORE *certstore; - X509_LOOKUP *certlookup; - int ret; - - if ((rpctls_verify_cafile != NULL || - rpctls_verify_capath != NULL) && - rpctls_crlfile != NULL) { - certstore = SSL_CTX_get_cert_store(ctx); - certlookup = X509_STORE_add_lookup( - certstore, X509_LOOKUP_file()); - ret = 0; - if (certlookup != NULL) - ret = X509_load_crl_file(certlookup, - rpctls_crlfile, X509_FILETYPE_PEM); - if (ret != 0) - ret = X509_STORE_set_flags(certstore, - X509_V_FLAG_CRL_CHECK | - X509_V_FLAG_CRL_CHECK_ALL); - if (ret == 0) { - rpctlssd_verbose_out( - "rpctls_loadcrlfile: Can't" - " load CRLfile=%s\n", - rpctls_crlfile); - return (ret); - } - } - return (1); -} - static void rpctls_huphandler(int sig __unused) { rpctls_gothup = true; -} - -/* - * Read the CRL file and check for any extant connections - * that might now be revoked. - */ -static void -rpctls_checkcrl(void) -{ - struct ssl_entry *slp; - BIO *infile; - X509_CRL *crl; - X509_REVOKED *revoked; - int ret; - - if (rpctls_crlfile == NULL || (rpctls_verify_cafile == NULL && - rpctls_verify_capath == NULL)) - return; - infile = BIO_new(BIO_s_file()); - if (infile == NULL) { - rpctlssd_verbose_out("rpctls_checkcrl: Cannot BIO_new\n"); - return; - } - ret = BIO_read_filename(infile, rpctls_crlfile); - if (ret != 1) { - rpctlssd_verbose_out("rpctls_checkcrl: Cannot read CRL file\n"); - BIO_free(infile); - return; - } - - for (crl = PEM_read_bio_X509_CRL(infile, NULL, NULL, ""); - crl != NULL; crl = PEM_read_bio_X509_CRL(infile, NULL, NULL, "")) { - LIST_FOREACH(slp, &rpctls_ssllist, next) { - if (slp->cert != NULL) { - ret = X509_CRL_get0_by_cert(crl, &revoked, - slp->cert); -rpctlssd_verbose_out("get0_by_cert=%d\n", ret); - /* - * Do a shutdown on the socket, so that it - * can no longer be used. The kernel RPC - * code will notice the socket is disabled - * and will do a disconnect upcall, which will - * close the socket. - */ - if (ret == 1) { - shutdown(slp->s, SHUT_WR); - slp->shutoff = true; - } - } - } - X509_CRL_free(crl); - } - BIO_free(infile); }