2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Extensively modified from /usr/src/usr.sbin/gssd.c r344402 for
32 * the client side of kernel RPC-over-TLS by Rick Macklem.
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/queue.h>
39 #include <sys/linker.h>
40 #include <sys/module.h>
42 #include <sys/sysctl.h>
43 #include <sys/syslog.h>
58 #include <rpc/rpc_com.h>
59 #include <rpc/rpcsec_tls.h>
61 #include <openssl/opensslconf.h>
62 #include <openssl/bio.h>
63 #include <openssl/ssl.h>
64 #include <openssl/err.h>
65 #include <openssl/x509v3.h>
68 #include "rpc.tlscommon.h"
70 #ifndef _PATH_RPCTLSCDSOCK
71 #define _PATH_RPCTLSCDSOCK "/var/run/rpc.tlsclntd.sock"
73 #ifndef _PATH_CERTANDKEY
74 #define _PATH_CERTANDKEY "/etc/rpc.tlsclntd/"
76 #ifndef _PATH_RPCTLSCDPID
77 #define _PATH_RPCTLSCDPID "/var/run/rpc.tlsclntd.pid"
80 /* Global variables also used by rpc.tlscommon.c. */
81 int rpctls_debug_level;
83 SSL_CTX *rpctls_ctx = NULL;
84 const char *rpctls_verify_cafile = NULL;
85 const char *rpctls_verify_capath = NULL;
86 char *rpctls_crlfile = NULL;
87 bool rpctls_cert = false;
88 bool rpctls_gothup = false;
89 struct ssl_list rpctls_ssllist;
91 static struct pidfh *rpctls_pfh = NULL;
92 static const char *rpctls_certdir = _PATH_CERTANDKEY;
93 static const char *rpctls_ciphers = NULL;
94 static uint64_t rpctls_ssl_refno = 0;
95 static uint64_t rpctls_ssl_sec = 0;
96 static uint64_t rpctls_ssl_usec = 0;
97 static int rpctls_tlsvers = TLS1_3_VERSION;
99 static void rpctlscd_terminate(int);
100 static SSL_CTX *rpctls_setupcl_ssl(void);
101 static SSL *rpctls_connect(SSL_CTX *ctx, int s, char *certname,
102 u_int certlen, X509 **certp);
103 static void rpctls_huphandler(int sig __unused);
105 extern void rpctlscd_1(struct svc_req *rqstp, SVCXPRT *transp);
107 static struct option longopts[] = {
108 { "usetls1_2", no_argument, NULL, '2' },
109 { "certdir", required_argument, NULL, 'D' },
110 { "ciphers", required_argument, NULL, 'C' },
111 { "debuglevel", no_argument, NULL, 'd' },
112 { "verifylocs", required_argument, NULL, 'l' },
113 { "mutualverf", no_argument, NULL, 'm' },
114 { "verifydir", required_argument, NULL, 'p' },
115 { "crl", required_argument, NULL, 'r' },
116 { "verbose", no_argument, NULL, 'v' },
121 main(int argc, char **argv)
124 * We provide an RPC service on a local-domain socket. The
125 * kernel rpctls code will upcall to this daemon to do the initial
128 struct sockaddr_un sun;
135 size_t tls_enable_len;
137 /* Check that another rpctlscd isn't already running. */
138 rpctls_pfh = pidfile_open(_PATH_RPCTLSCDPID, 0600, &otherpid);
139 if (rpctls_pfh == NULL) {
141 errx(1, "rpctlscd already running, pid: %d.", otherpid);
142 warn("cannot open or create pidfile");
145 /* Check to see that the ktls is enabled. */
146 tls_enable_len = sizeof(tls_enable);
147 if (sysctlbyname("kern.ipc.tls.enable", &tls_enable, &tls_enable_len,
148 NULL, 0) != 0 || !tls_enable)
149 errx(1, "Kernel TLS not enabled");
151 /* Get the time when this daemon is started. */
152 gettimeofday(&tm, &tz);
153 rpctls_ssl_sec = tm.tv_sec;
154 rpctls_ssl_usec = tm.tv_usec;
156 rpctls_verbose = false;
157 while ((ch = getopt_long(argc, argv, "2C:D:dl:mp:r:v", longopts,
161 rpctls_tlsvers = TLS1_2_VERSION;
164 rpctls_ciphers = optarg;
167 rpctls_certdir = optarg;
170 rpctls_debug_level++;
173 rpctls_verify_cafile = optarg;
179 rpctls_verify_capath = optarg;
182 rpctls_crlfile = optarg;
185 rpctls_verbose = true;
188 fprintf(stderr, "usage: %s "
190 "[-C/--ciphers available_ciphers] "
191 "[-D/--certdir certdir] [-d/--debuglevel] "
192 "[-l/--verifylocs CAfile] [-m/--mutualverf] "
193 "[-p/--verifydir CApath] [-r/--crl CRLfile] "
194 "[-v/--verbose]\n", argv[0]);
199 if (rpctls_crlfile != NULL && rpctls_verify_cafile == NULL &&
200 rpctls_verify_capath == NULL)
201 errx(1, "-r requires the -l <CAfile> and/or "
202 "-p <CApath> options");
204 if (modfind("krpc") < 0) {
205 /* Not present in kernel, try loading it */
206 if (kldload("krpc") < 0 || modfind("krpc") < 0)
207 errx(1, "Kernel RPC is not available");
211 * Set up the SSL_CTX *.
212 * Do it now, before daemonizing, in case the private key
213 * is encrypted and requires a passphrase to be entered.
215 rpctls_ctx = rpctls_setupcl_ssl();
216 if (rpctls_ctx == NULL) {
217 if (rpctls_debug_level == 0) {
218 syslog(LOG_ERR, "Can't set up TLS context");
221 err(1, "Can't set up TLS context");
223 LIST_INIT(&rpctls_ssllist);
225 if (!rpctls_debug_level) {
226 if (daemon(0, 0) != 0)
227 err(1, "Can't daemonize");
228 signal(SIGINT, SIG_IGN);
229 signal(SIGQUIT, SIG_IGN);
230 signal(SIGHUP, SIG_IGN);
232 signal(SIGTERM, rpctlscd_terminate);
233 signal(SIGPIPE, SIG_IGN);
234 signal(SIGHUP, rpctls_huphandler);
236 pidfile_write(rpctls_pfh);
238 memset(&sun, 0, sizeof sun);
239 sun.sun_family = AF_LOCAL;
240 unlink(_PATH_RPCTLSCDSOCK);
241 strcpy(sun.sun_path, _PATH_RPCTLSCDSOCK);
242 sun.sun_len = SUN_LEN(&sun);
243 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
245 if (rpctls_debug_level == 0) {
246 syslog(LOG_ERR, "Can't create local rpctlscd socket");
249 err(1, "Can't create local rpctlscd socket");
251 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
252 if (bind(fd, (struct sockaddr *)&sun, sun.sun_len) < 0) {
253 if (rpctls_debug_level == 0) {
254 syslog(LOG_ERR, "Can't bind local rpctlscd socket");
257 err(1, "Can't bind local rpctlscd socket");
260 if (listen(fd, SOMAXCONN) < 0) {
261 if (rpctls_debug_level == 0) {
263 "Can't listen on local rpctlscd socket");
266 err(1, "Can't listen on local rpctlscd socket");
268 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
270 if (rpctls_debug_level == 0) {
272 "Can't create transport for local rpctlscd socket");
275 err(1, "Can't create transport for local rpctlscd socket");
277 if (!svc_reg(xprt, RPCTLSCD, RPCTLSCDVERS, rpctlscd_1, NULL)) {
278 if (rpctls_debug_level == 0) {
280 "Can't register service for local rpctlscd socket");
283 err(1, "Can't register service for local rpctlscd socket");
286 if (rpctls_syscall(RPCTLS_SYSC_CLSETPATH, _PATH_RPCTLSCDSOCK) < 0) {
287 if (rpctls_debug_level == 0) {
289 "Can't set upcall socket path errno=%d", errno);
292 err(1, "Can't set upcall socket path");
297 rpctls_syscall(RPCTLS_SYSC_CLSHUTDOWN, "");
299 SSL_CTX_free(rpctls_ctx);
304 rpctlscd_null_1_svc(__unused void *argp, __unused void *result,
305 __unused struct svc_req *rqstp)
308 rpctls_verbose_out("rpctlscd_null: done\n");
313 rpctlscd_connect_1_svc(struct rpctlscd_connect_arg *argp,
314 struct rpctlscd_connect_res *result, __unused struct svc_req *rqstp)
318 struct ssl_entry *newslp;
321 rpctls_verbose_out("rpctlsd_connect: started\n");
322 /* Get the socket fd from the kernel. */
323 s = rpctls_syscall(RPCTLS_SYSC_CLSOCKET, "");
325 result->reterr = RPCTLSERR_NOSOCKET;
329 /* Do a TLS connect handshake. */
330 ssl = rpctls_connect(rpctls_ctx, s, argp->certname.certname_val,
331 argp->certname.certname_len, &cert);
333 rpctls_verbose_out("rpctlsd_connect: can't do TLS "
335 result->reterr = RPCTLSERR_NOSSL;
337 result->reterr = RPCTLSERR_OK;
338 result->sec = rpctls_ssl_sec;
339 result->usec = rpctls_ssl_usec;
340 result->ssl = ++rpctls_ssl_refno;
341 /* Hard to believe this will ever wrap around.. */
342 if (rpctls_ssl_refno == 0)
343 result->ssl = ++rpctls_ssl_refno;
348 * For RPC-over-TLS, this upcall is expected
349 * to close off the socket.
355 /* Maintain list of all current SSL *'s */
356 newslp = malloc(sizeof(*newslp));
357 newslp->refno = rpctls_ssl_refno;
359 newslp->shutoff = false;
362 LIST_INSERT_HEAD(&rpctls_ssllist, newslp, next);
367 rpctlscd_handlerecord_1_svc(struct rpctlscd_handlerecord_arg *argp,
368 struct rpctlscd_handlerecord_res *result, __unused struct svc_req *rqstp)
370 struct ssl_entry *slp;
375 if (argp->sec == rpctls_ssl_sec && argp->usec ==
377 LIST_FOREACH(slp, &rpctls_ssllist, next) {
378 if (slp->refno == argp->ssl)
384 rpctls_verbose_out("rpctlscd_handlerecord fd=%d\n",
387 * An SSL_read() of 0 bytes should fail, but it should
388 * handle the non-application data record before doing so.
390 ret = SSL_read(slp->ssl, &junk, 0);
392 /* Check to see if this was a close alert. */
393 ret = SSL_get_shutdown(slp->ssl);
394 if ((ret & (SSL_SENT_SHUTDOWN |
395 SSL_RECEIVED_SHUTDOWN)) == SSL_RECEIVED_SHUTDOWN)
396 SSL_shutdown(slp->ssl);
398 if (rpctls_debug_level == 0)
399 syslog(LOG_ERR, "SSL_read returned %d", ret);
401 fprintf(stderr, "SSL_read returned %d\n", ret);
403 result->reterr = RPCTLSERR_OK;
405 result->reterr = RPCTLSERR_NOSSL;
410 rpctlscd_disconnect_1_svc(struct rpctlscd_disconnect_arg *argp,
411 struct rpctlscd_disconnect_res *result, __unused struct svc_req *rqstp)
413 struct ssl_entry *slp;
417 if (argp->sec == rpctls_ssl_sec && argp->usec ==
419 LIST_FOREACH(slp, &rpctls_ssllist, next) {
420 if (slp->refno == argp->ssl)
426 rpctls_verbose_out("rpctlscd_disconnect: fd=%d closed\n",
428 LIST_REMOVE(slp, next);
430 ret = SSL_get_shutdown(slp->ssl);
432 * Do an SSL_shutdown() unless a close alert has
435 if ((ret & SSL_SENT_SHUTDOWN) == 0)
436 SSL_shutdown(slp->ssl);
439 if (slp->cert != NULL)
440 X509_free(slp->cert);
442 * For RPC-over-TLS, this upcall is expected
443 * to close off the socket.
446 shutdown(slp->s, SHUT_WR);
449 result->reterr = RPCTLSERR_OK;
451 result->reterr = RPCTLSERR_NOCLOSE;
456 rpctlscd_1_freeresult(__unused SVCXPRT *transp, __unused xdrproc_t xdr_result,
457 __unused caddr_t result)
464 rpctlscd_terminate(int sig __unused)
467 rpctls_syscall(RPCTLS_SYSC_CLSHUTDOWN, "");
468 pidfile_remove(rpctls_pfh);
473 rpctls_setupcl_ssl(void)
480 ctx = SSL_CTX_new(TLS_client_method());
482 rpctls_verbose_out("rpctls_setupcl_ssl: SSL_CTX_new "
487 if (rpctls_ciphers != NULL) {
489 * Set available ciphers, since KERN_TLS only supports a
492 ret = SSL_CTX_set_ciphersuites(ctx, rpctls_ciphers);
494 rpctls_verbose_out("rpctls_setupcl_ssl: "
495 "SSL_CTX_set_ciphersuites failed: %s\n",
503 * If rpctls_cert is true, a certificate and key exists in
504 * rpctls_certdir, so that it can do mutual authentication.
507 /* Get the cert.pem and certkey.pem files. */
508 len = strlcpy(path, rpctls_certdir, sizeof(path));
509 rlen = sizeof(path) - len;
510 if (strlcpy(&path[len], "cert.pem", rlen) != 8) {
514 ret = SSL_CTX_use_certificate_file(ctx, path,
517 rpctls_verbose_out("rpctls_setupcl_ssl: can't use "
518 "certificate file path=%s ret=%d\n", path, ret);
522 if (strlcpy(&path[len], "certkey.pem", rlen) != 11) {
526 ret = SSL_CTX_use_PrivateKey_file(ctx, path,
529 rpctls_verbose_out("rpctls_setupcl_ssl: Can't use "
530 "private key path=%s ret=%d\n", path, ret);
536 if (rpctls_verify_cafile != NULL || rpctls_verify_capath != NULL) {
537 if (rpctls_crlfile != NULL) {
538 ret = rpctls_loadcrlfile(ctx);
540 rpctls_verbose_out("rpctls_setupcl_ssl: "
541 "Load CRLfile failed\n");
546 #if OPENSSL_VERSION_NUMBER >= 0x30000000
548 if (rpctls_verify_cafile != NULL)
549 ret = SSL_CTX_load_verify_file(ctx,
550 rpctls_verify_cafile);
551 if (ret != 0 && rpctls_verify_capath != NULL)
552 ret = SSL_CTX_load_verify_dir(ctx,
553 rpctls_verify_capath);
555 ret = SSL_CTX_load_verify_locations(ctx,
556 rpctls_verify_cafile, rpctls_verify_capath);
559 rpctls_verbose_out("rpctls_setupcl_ssl: "
560 "Can't load verify locations\n");
565 * The man page says that the
566 * SSL_CTX_set0_CA_list() call is not normally
567 * needed, but I believe it is harmless.
569 if (rpctls_verify_cafile != NULL)
570 SSL_CTX_set0_CA_list(ctx,
571 SSL_load_client_CA_file(rpctls_verify_cafile));
575 * The RFC specifies that RPC-over-TLS must use TLS1.3.
576 * However, early FreeBSD versions (13.0, 13.1) did not
577 * support RX for KTLS1.3, so TLS1.2 needs to be used for
580 ret = SSL_CTX_set_min_proto_version(ctx, rpctls_tlsvers);
582 rpctls_verbose_out("rpctls_setupcl_ssl: "
583 "SSL_CTX_set_min_proto_version failed\n");
587 ret = SSL_CTX_set_max_proto_version(ctx, rpctls_tlsvers);
589 rpctls_verbose_out("rpctls_setupcl_ssl: "
590 "SSL_CTX_set_max_proto_version failed\n");
595 #ifdef SSL_OP_ENABLE_KTLS
596 SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
598 #ifdef SSL_MODE_NO_KTLS_TX
599 SSL_CTX_clear_mode(ctx, SSL_MODE_NO_KTLS_TX | SSL_MODE_NO_KTLS_RX);
605 rpctls_connect(SSL_CTX *ctx, int s, char *certname, u_int certlen, X509 **certp)
609 struct sockaddr_storage ad;
610 struct sockaddr *sad;
611 char hostnam[NI_MAXHOST], path[PATH_MAX];
618 sad = (struct sockaddr *)&ad;
621 rpctls_verbose_out("rpctls_connect: "
625 if (SSL_set_fd(ssl, s) != 1) {
626 rpctls_verbose_out("rpctls_connect: "
627 "SSL_set_fd failed\n");
633 * If rpctls_cert is true and certname is set, a alternate certificate
634 * and key exists in files named <certname>.pem and <certname>key.pem
635 * in rpctls_certdir that is to be used for mutual authentication.
637 if (rpctls_cert && certlen > 0) {
638 len = strlcpy(path, rpctls_certdir, sizeof(path));
639 rlen = sizeof(path) - len;
640 if (rlen <= certlen) {
644 memcpy(&path[len], certname, certlen);
648 if (strlcpy(&path[len], ".pem", rlen) != 4) {
652 ret = SSL_use_certificate_file(ssl, path, SSL_FILETYPE_PEM);
654 rpctls_verbose_out("rpctls_connect: can't use "
655 "certificate file path=%s ret=%d\n", path, ret);
659 if (strlcpy(&path[len], "key.pem", rlen) != 7) {
663 ret = SSL_use_PrivateKey_file(ssl, path, SSL_FILETYPE_PEM);
665 rpctls_verbose_out("rpctls_connect: Can't use "
666 "private key path=%s ret=%d\n", path, ret);
672 ret = SSL_connect(ssl);
674 rpctls_verbose_out("rpctls_connect: "
675 "SSL_connect failed %d: %s\n",
676 ret, ERR_error_string(ERR_get_error(), NULL));
681 #if OPENSSL_VERSION_NUMBER >= 0x30000000
682 cert = SSL_get1_peer_certificate(ssl);
684 cert = SSL_get_peer_certificate(ssl);
687 rpctls_verbose_out("rpctls_connect: get peer"
688 " certificate failed\n");
692 gethostret = rpctls_gethost(s, sad, hostnam, sizeof(hostnam));
695 verfret = SSL_get_verify_result(ssl);
696 if (verfret == X509_V_OK && (rpctls_verify_cafile != NULL ||
697 rpctls_verify_capath != NULL) && (gethostret == 0 ||
698 rpctls_checkhost(sad, cert, X509_CHECK_FLAG_NO_WILDCARDS) != 1))
699 verfret = X509_V_ERR_HOSTNAME_MISMATCH;
700 if (verfret != X509_V_OK && (rpctls_verify_cafile != NULL ||
701 rpctls_verify_capath != NULL)) {
702 if (verfret != X509_V_OK) {
703 cp = X509_NAME_oneline(X509_get_issuer_name(cert),
705 cp2 = X509_NAME_oneline(X509_get_subject_name(cert),
707 if (rpctls_debug_level == 0)
708 syslog(LOG_INFO | LOG_DAEMON,
709 "rpctls_connect: client IP %s "
710 "issuerName=%s subjectName=%s verify "
711 "failed %s\n", hostnam, cp, cp2,
712 X509_verify_cert_error_string(verfret));
715 "rpctls_connect: client IP %s "
716 "issuerName=%s subjectName=%s verify "
717 "failed %s\n", hostnam, cp, cp2,
718 X509_verify_cert_error_string(verfret));
725 /* Check to see if ktls is enabled on the connection. */
726 ret = BIO_get_ktls_send(SSL_get_wbio(ssl));
727 rpctls_verbose_out("rpctls_connect: BIO_get_ktls_send=%d\n", ret);
729 ret = BIO_get_ktls_recv(SSL_get_rbio(ssl));
730 rpctls_verbose_out("rpctls_connect: BIO_get_ktls_recv=%d\n",
734 if (rpctls_debug_level == 0)
735 syslog(LOG_ERR, "ktls not working\n");
737 fprintf(stderr, "ktls not working\n");
742 if (ret == X509_V_OK && (rpctls_verify_cafile != NULL ||
743 rpctls_verify_capath != NULL) && rpctls_crlfile != NULL)
752 rpctls_huphandler(int sig __unused)
755 rpctls_gothup = true;