xref: /freebsd/usr.sbin/rpc.tlsclntd/rpc.tlsclntd.c (revision f5b40aa0dea690314bc70f88634a13cbaa53b6f0)
1b9cbc85dSRick Macklem /*-
2b9cbc85dSRick Macklem  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3b9cbc85dSRick Macklem  *
4b9cbc85dSRick Macklem  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5b9cbc85dSRick Macklem  * Authors: Doug Rabson <dfr@rabson.org>
6b9cbc85dSRick Macklem  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7b9cbc85dSRick Macklem  *
8b9cbc85dSRick Macklem  * Redistribution and use in source and binary forms, with or without
9b9cbc85dSRick Macklem  * modification, are permitted provided that the following conditions
10b9cbc85dSRick Macklem  * are met:
11b9cbc85dSRick Macklem  * 1. Redistributions of source code must retain the above copyright
12b9cbc85dSRick Macklem  *    notice, this list of conditions and the following disclaimer.
13b9cbc85dSRick Macklem  * 2. Redistributions in binary form must reproduce the above copyright
14b9cbc85dSRick Macklem  *    notice, this list of conditions and the following disclaimer in the
15b9cbc85dSRick Macklem  *    documentation and/or other materials provided with the distribution.
16b9cbc85dSRick Macklem  *
17b9cbc85dSRick Macklem  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18b9cbc85dSRick Macklem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19b9cbc85dSRick Macklem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20b9cbc85dSRick Macklem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21b9cbc85dSRick Macklem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22b9cbc85dSRick Macklem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23b9cbc85dSRick Macklem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24b9cbc85dSRick Macklem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25b9cbc85dSRick Macklem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26b9cbc85dSRick Macklem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27b9cbc85dSRick Macklem  * SUCH DAMAGE.
28b9cbc85dSRick Macklem  */
29b9cbc85dSRick Macklem 
30b9cbc85dSRick Macklem /*
31b9cbc85dSRick Macklem  * Extensively modified from /usr/src/usr.sbin/gssd.c r344402 for
32b9cbc85dSRick Macklem  * the client side of kernel RPC-over-TLS by Rick Macklem.
33b9cbc85dSRick Macklem  */
34b9cbc85dSRick Macklem 
35b9cbc85dSRick Macklem #include <sys/cdefs.h>
36b9cbc85dSRick Macklem __FBSDID("$FreeBSD$");
37b9cbc85dSRick Macklem 
38b9cbc85dSRick Macklem #include <sys/param.h>
39b9cbc85dSRick Macklem #include <sys/types.h>
40b9cbc85dSRick Macklem #include <sys/queue.h>
41b9cbc85dSRick Macklem #include <sys/linker.h>
42b9cbc85dSRick Macklem #include <sys/module.h>
43b9cbc85dSRick Macklem #include <sys/stat.h>
44b9cbc85dSRick Macklem #include <sys/sysctl.h>
45b9cbc85dSRick Macklem #include <sys/syslog.h>
46b9cbc85dSRick Macklem #include <sys/time.h>
47b9cbc85dSRick Macklem #include <err.h>
48b9cbc85dSRick Macklem #include <getopt.h>
49b9cbc85dSRick Macklem #include <libutil.h>
50b9cbc85dSRick Macklem #include <netdb.h>
51b9cbc85dSRick Macklem #include <signal.h>
52b9cbc85dSRick Macklem #include <stdarg.h>
53b9cbc85dSRick Macklem #include <stdbool.h>
54b9cbc85dSRick Macklem #include <stdio.h>
55b9cbc85dSRick Macklem #include <stdlib.h>
56b9cbc85dSRick Macklem #include <string.h>
57b9cbc85dSRick Macklem #include <unistd.h>
58b9cbc85dSRick Macklem 
59b9cbc85dSRick Macklem #include <rpc/rpc.h>
60b9cbc85dSRick Macklem #include <rpc/rpc_com.h>
61b9cbc85dSRick Macklem #include <rpc/rpcsec_tls.h>
62b9cbc85dSRick Macklem 
63b9cbc85dSRick Macklem #include <openssl/opensslconf.h>
64b9cbc85dSRick Macklem #include <openssl/bio.h>
65b9cbc85dSRick Macklem #include <openssl/ssl.h>
66b9cbc85dSRick Macklem #include <openssl/err.h>
67b9cbc85dSRick Macklem #include <openssl/x509v3.h>
68b9cbc85dSRick Macklem 
69b9cbc85dSRick Macklem #include "rpctlscd.h"
70b9cbc85dSRick Macklem #include "rpc.tlscommon.h"
71b9cbc85dSRick Macklem 
72b9cbc85dSRick Macklem #ifndef _PATH_RPCTLSCDSOCK
73b9cbc85dSRick Macklem #define _PATH_RPCTLSCDSOCK	"/var/run/rpc.tlsclntd.sock"
74b9cbc85dSRick Macklem #endif
75b9cbc85dSRick Macklem #ifndef	_PATH_CERTANDKEY
76b9cbc85dSRick Macklem #define	_PATH_CERTANDKEY	"/etc/rpc.tlsclntd/"
77b9cbc85dSRick Macklem #endif
78b9cbc85dSRick Macklem #ifndef	_PATH_RPCTLSCDPID
79b9cbc85dSRick Macklem #define	_PATH_RPCTLSCDPID	"/var/run/rpc.tlsclntd.pid"
80b9cbc85dSRick Macklem #endif
81b9cbc85dSRick Macklem 
82b9cbc85dSRick Macklem /* Global variables also used by rpc.tlscommon.c. */
83b9cbc85dSRick Macklem int			rpctls_debug_level;
84b9cbc85dSRick Macklem bool			rpctls_verbose;
85b9cbc85dSRick Macklem SSL_CTX			*rpctls_ctx = NULL;
86b9cbc85dSRick Macklem const char		*rpctls_verify_cafile = NULL;
87b9cbc85dSRick Macklem const char		*rpctls_verify_capath = NULL;
88b9cbc85dSRick Macklem char			*rpctls_crlfile = NULL;
89b9cbc85dSRick Macklem bool			rpctls_cert = false;
90b9cbc85dSRick Macklem bool			rpctls_gothup = false;
91b9cbc85dSRick Macklem struct ssl_list		rpctls_ssllist;
92b9cbc85dSRick Macklem 
93b9cbc85dSRick Macklem static struct pidfh	*rpctls_pfh = NULL;
94b9cbc85dSRick Macklem static const char	*rpctls_certdir = _PATH_CERTANDKEY;
95b9cbc85dSRick Macklem static const char	*rpctls_ciphers = NULL;
96b9cbc85dSRick Macklem static uint64_t		rpctls_ssl_refno = 0;
97b9cbc85dSRick Macklem static uint64_t		rpctls_ssl_sec = 0;
98b9cbc85dSRick Macklem static uint64_t		rpctls_ssl_usec = 0;
9972bf76d6SRick Macklem static int		rpctls_tlsvers = TLS1_3_VERSION;
100b9cbc85dSRick Macklem 
101b9cbc85dSRick Macklem static void		rpctlscd_terminate(int);
102b9cbc85dSRick Macklem static SSL_CTX		*rpctls_setupcl_ssl(void);
103b9cbc85dSRick Macklem static SSL		*rpctls_connect(SSL_CTX *ctx, int s, char *certname,
104b9cbc85dSRick Macklem 			    u_int certlen, X509 **certp);
105b9cbc85dSRick Macklem static void		rpctls_huphandler(int sig __unused);
106b9cbc85dSRick Macklem 
107b9cbc85dSRick Macklem extern void rpctlscd_1(struct svc_req *rqstp, SVCXPRT *transp);
108b9cbc85dSRick Macklem 
109b9cbc85dSRick Macklem static struct option longopts[] = {
11072bf76d6SRick Macklem 	{ "usetls1_2",		no_argument,		NULL,	'2' },
111b9cbc85dSRick Macklem 	{ "certdir",		required_argument,	NULL,	'D' },
112b9cbc85dSRick Macklem 	{ "ciphers",		required_argument,	NULL,	'C' },
113b9cbc85dSRick Macklem 	{ "debuglevel",		no_argument,		NULL,	'd' },
114b9cbc85dSRick Macklem 	{ "verifylocs",		required_argument,	NULL,	'l' },
115b9cbc85dSRick Macklem 	{ "mutualverf",		no_argument,		NULL,	'm' },
116b9cbc85dSRick Macklem 	{ "verifydir",		required_argument,	NULL,	'p' },
117b9cbc85dSRick Macklem 	{ "crl",		required_argument,	NULL,	'r' },
118b9cbc85dSRick Macklem 	{ "verbose",		no_argument,		NULL,	'v' },
119b9cbc85dSRick Macklem 	{ NULL,			0,			NULL,	0  }
120b9cbc85dSRick Macklem };
121b9cbc85dSRick Macklem 
122b9cbc85dSRick Macklem int
123b9cbc85dSRick Macklem main(int argc, char **argv)
124b9cbc85dSRick Macklem {
125b9cbc85dSRick Macklem 	/*
126b9cbc85dSRick Macklem 	 * We provide an RPC service on a local-domain socket. The
127b9cbc85dSRick Macklem 	 * kernel rpctls code will upcall to this daemon to do the initial
128b9cbc85dSRick Macklem 	 * TLS handshake.
129b9cbc85dSRick Macklem 	 */
130b9cbc85dSRick Macklem 	struct sockaddr_un sun;
131b9cbc85dSRick Macklem 	int ch, fd, oldmask;
132b9cbc85dSRick Macklem 	SVCXPRT *xprt;
133b9cbc85dSRick Macklem 	bool tls_enable;
134b9cbc85dSRick Macklem 	struct timeval tm;
135b9cbc85dSRick Macklem 	struct timezone tz;
136b9cbc85dSRick Macklem 	pid_t otherpid;
137b9cbc85dSRick Macklem 	size_t tls_enable_len;
138b9cbc85dSRick Macklem 
139b9cbc85dSRick Macklem 	/* Check that another rpctlscd isn't already running. */
140b9cbc85dSRick Macklem 	rpctls_pfh = pidfile_open(_PATH_RPCTLSCDPID, 0600, &otherpid);
141b9cbc85dSRick Macklem 	if (rpctls_pfh == NULL) {
142b9cbc85dSRick Macklem 		if (errno == EEXIST)
143b9cbc85dSRick Macklem 			errx(1, "rpctlscd already running, pid: %d.", otherpid);
144b9cbc85dSRick Macklem 		warn("cannot open or create pidfile");
145b9cbc85dSRick Macklem 	}
146b9cbc85dSRick Macklem 
147b9cbc85dSRick Macklem 	/* Check to see that the ktls is enabled. */
148b9cbc85dSRick Macklem 	tls_enable_len = sizeof(tls_enable);
149b9cbc85dSRick Macklem 	if (sysctlbyname("kern.ipc.tls.enable", &tls_enable, &tls_enable_len,
150b9cbc85dSRick Macklem 	    NULL, 0) != 0 || !tls_enable)
151b9cbc85dSRick Macklem 		errx(1, "Kernel TLS not enabled");
152b9cbc85dSRick Macklem 
153b9cbc85dSRick Macklem 	/* Get the time when this daemon is started. */
154b9cbc85dSRick Macklem 	gettimeofday(&tm, &tz);
155b9cbc85dSRick Macklem 	rpctls_ssl_sec = tm.tv_sec;
156b9cbc85dSRick Macklem 	rpctls_ssl_usec = tm.tv_usec;
157b9cbc85dSRick Macklem 
158b9cbc85dSRick Macklem 	rpctls_verbose = false;
15972bf76d6SRick Macklem 	while ((ch = getopt_long(argc, argv, "2C:D:dl:mp:r:v", longopts,
16015881823SRick Macklem 	    NULL)) != -1) {
161b9cbc85dSRick Macklem 		switch (ch) {
16272bf76d6SRick Macklem 		case '2':
16372bf76d6SRick Macklem 			rpctls_tlsvers = TLS1_2_VERSION;
16472bf76d6SRick Macklem 			break;
165b9cbc85dSRick Macklem 		case 'C':
166b9cbc85dSRick Macklem 			rpctls_ciphers = optarg;
167b9cbc85dSRick Macklem 			break;
168b9cbc85dSRick Macklem 		case 'D':
169b9cbc85dSRick Macklem 			rpctls_certdir = optarg;
170b9cbc85dSRick Macklem 			break;
171b9cbc85dSRick Macklem 		case 'd':
172b9cbc85dSRick Macklem 			rpctls_debug_level++;
173b9cbc85dSRick Macklem 			break;
174b9cbc85dSRick Macklem 		case 'l':
175b9cbc85dSRick Macklem 			rpctls_verify_cafile = optarg;
176b9cbc85dSRick Macklem 			break;
177b9cbc85dSRick Macklem 		case 'm':
178b9cbc85dSRick Macklem 			rpctls_cert = true;
179b9cbc85dSRick Macklem 			break;
180b9cbc85dSRick Macklem 		case 'p':
181b9cbc85dSRick Macklem 			rpctls_verify_capath = optarg;
182b9cbc85dSRick Macklem 			break;
183b9cbc85dSRick Macklem 		case 'r':
184b9cbc85dSRick Macklem 			rpctls_crlfile = optarg;
185b9cbc85dSRick Macklem 			break;
186b9cbc85dSRick Macklem 		case 'v':
187b9cbc85dSRick Macklem 			rpctls_verbose = true;
188b9cbc85dSRick Macklem 			break;
189b9cbc85dSRick Macklem 		default:
190b9cbc85dSRick Macklem 			fprintf(stderr, "usage: %s "
191*f5b40aa0SRick Macklem 			    "[-C/--ciphers available_ciphers] "
192b9cbc85dSRick Macklem 			    "[-D/--certdir certdir] [-d/--debuglevel] "
193b9cbc85dSRick Macklem 			    "[-l/--verifylocs CAfile] [-m/--mutualverf] "
194b9cbc85dSRick Macklem 			    "[-p/--verifydir CApath] [-r/--crl CRLfile] "
195b9cbc85dSRick Macklem 			    "[-v/--verbose]\n", argv[0]);
196b9cbc85dSRick Macklem 			exit(1);
197b9cbc85dSRick Macklem 			break;
198b9cbc85dSRick Macklem 		}
199b9cbc85dSRick Macklem 	}
200b9cbc85dSRick Macklem 	if (rpctls_crlfile != NULL && rpctls_verify_cafile == NULL &&
201b9cbc85dSRick Macklem 	    rpctls_verify_capath == NULL)
202b9cbc85dSRick Macklem 		errx(1, "-r requires the -l <CAfile> and/or "
203b9cbc85dSRick Macklem 		    "-p <CApath> options");
204b9cbc85dSRick Macklem 
205b9cbc85dSRick Macklem 	if (modfind("krpc") < 0) {
206b9cbc85dSRick Macklem 		/* Not present in kernel, try loading it */
207b9cbc85dSRick Macklem 		if (kldload("krpc") < 0 || modfind("krpc") < 0)
208b9cbc85dSRick Macklem 			errx(1, "Kernel RPC is not available");
209b9cbc85dSRick Macklem 	}
210b9cbc85dSRick Macklem 
211b9cbc85dSRick Macklem 	/*
212b9cbc85dSRick Macklem 	 * Set up the SSL_CTX *.
213b9cbc85dSRick Macklem 	 * Do it now, before daemonizing, in case the private key
214b9cbc85dSRick Macklem 	 * is encrypted and requires a passphrase to be entered.
215b9cbc85dSRick Macklem 	 */
216b9cbc85dSRick Macklem 	rpctls_ctx = rpctls_setupcl_ssl();
217b9cbc85dSRick Macklem 	if (rpctls_ctx == NULL) {
218b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
219b9cbc85dSRick Macklem 			syslog(LOG_ERR, "Can't set up TLS context");
220b9cbc85dSRick Macklem 			exit(1);
221b9cbc85dSRick Macklem 		}
222b9cbc85dSRick Macklem 		err(1, "Can't set up TLS context");
223b9cbc85dSRick Macklem 	}
224b9cbc85dSRick Macklem 	LIST_INIT(&rpctls_ssllist);
225b9cbc85dSRick Macklem 
226b9cbc85dSRick Macklem 	if (!rpctls_debug_level) {
227b9cbc85dSRick Macklem 		if (daemon(0, 0) != 0)
228b9cbc85dSRick Macklem 			err(1, "Can't daemonize");
229b9cbc85dSRick Macklem 		signal(SIGINT, SIG_IGN);
230b9cbc85dSRick Macklem 		signal(SIGQUIT, SIG_IGN);
231b9cbc85dSRick Macklem 		signal(SIGHUP, SIG_IGN);
232b9cbc85dSRick Macklem 	}
233b9cbc85dSRick Macklem 	signal(SIGTERM, rpctlscd_terminate);
234b9cbc85dSRick Macklem 	signal(SIGPIPE, SIG_IGN);
235b9cbc85dSRick Macklem 	signal(SIGHUP, rpctls_huphandler);
236b9cbc85dSRick Macklem 
237b9cbc85dSRick Macklem 	pidfile_write(rpctls_pfh);
238b9cbc85dSRick Macklem 
239b9cbc85dSRick Macklem 	memset(&sun, 0, sizeof sun);
240b9cbc85dSRick Macklem 	sun.sun_family = AF_LOCAL;
241b9cbc85dSRick Macklem 	unlink(_PATH_RPCTLSCDSOCK);
242b9cbc85dSRick Macklem 	strcpy(sun.sun_path, _PATH_RPCTLSCDSOCK);
243b9cbc85dSRick Macklem 	sun.sun_len = SUN_LEN(&sun);
244b9cbc85dSRick Macklem 	fd = socket(AF_LOCAL, SOCK_STREAM, 0);
245b9cbc85dSRick Macklem 	if (fd < 0) {
246b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
247b9cbc85dSRick Macklem 			syslog(LOG_ERR, "Can't create local rpctlscd socket");
248b9cbc85dSRick Macklem 			exit(1);
249b9cbc85dSRick Macklem 		}
250b9cbc85dSRick Macklem 		err(1, "Can't create local rpctlscd socket");
251b9cbc85dSRick Macklem 	}
252b9cbc85dSRick Macklem 	oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
253b9cbc85dSRick Macklem 	if (bind(fd, (struct sockaddr *)&sun, sun.sun_len) < 0) {
254b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
255b9cbc85dSRick Macklem 			syslog(LOG_ERR, "Can't bind local rpctlscd socket");
256b9cbc85dSRick Macklem 			exit(1);
257b9cbc85dSRick Macklem 		}
258b9cbc85dSRick Macklem 		err(1, "Can't bind local rpctlscd socket");
259b9cbc85dSRick Macklem 	}
260b9cbc85dSRick Macklem 	umask(oldmask);
261b9cbc85dSRick Macklem 	if (listen(fd, SOMAXCONN) < 0) {
262b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
263b9cbc85dSRick Macklem 			syslog(LOG_ERR,
264b9cbc85dSRick Macklem 			    "Can't listen on local rpctlscd socket");
265b9cbc85dSRick Macklem 			exit(1);
266b9cbc85dSRick Macklem 		}
267b9cbc85dSRick Macklem 		err(1, "Can't listen on local rpctlscd socket");
268b9cbc85dSRick Macklem 	}
269b9cbc85dSRick Macklem 	xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
270b9cbc85dSRick Macklem 	if (!xprt) {
271b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
272b9cbc85dSRick Macklem 			syslog(LOG_ERR,
273b9cbc85dSRick Macklem 			    "Can't create transport for local rpctlscd socket");
274b9cbc85dSRick Macklem 			exit(1);
275b9cbc85dSRick Macklem 		}
276b9cbc85dSRick Macklem 		err(1, "Can't create transport for local rpctlscd socket");
277b9cbc85dSRick Macklem 	}
278b9cbc85dSRick Macklem 	if (!svc_reg(xprt, RPCTLSCD, RPCTLSCDVERS, rpctlscd_1, NULL)) {
279b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0) {
280b9cbc85dSRick Macklem 			syslog(LOG_ERR,
281b9cbc85dSRick Macklem 			    "Can't register service for local rpctlscd socket");
282b9cbc85dSRick Macklem 			exit(1);
283b9cbc85dSRick Macklem 		}
284b9cbc85dSRick Macklem 		err(1, "Can't register service for local rpctlscd socket");
285b9cbc85dSRick Macklem 	}
286b9cbc85dSRick Macklem 
287b9cbc85dSRick Macklem 	rpctls_syscall(RPCTLS_SYSC_CLSETPATH, _PATH_RPCTLSCDSOCK);
288b9cbc85dSRick Macklem 
289b9cbc85dSRick Macklem 	rpctls_svc_run();
290b9cbc85dSRick Macklem 
291b9cbc85dSRick Macklem 	rpctls_syscall(RPCTLS_SYSC_CLSHUTDOWN, "");
292b9cbc85dSRick Macklem 
293b9cbc85dSRick Macklem 	SSL_CTX_free(rpctls_ctx);
294b9cbc85dSRick Macklem 	EVP_cleanup();
295b9cbc85dSRick Macklem 	return (0);
296b9cbc85dSRick Macklem }
297b9cbc85dSRick Macklem 
298b9cbc85dSRick Macklem bool_t
299b9cbc85dSRick Macklem rpctlscd_null_1_svc(__unused void *argp, __unused void *result,
300b9cbc85dSRick Macklem     __unused struct svc_req *rqstp)
301b9cbc85dSRick Macklem {
302b9cbc85dSRick Macklem 
303b9cbc85dSRick Macklem 	rpctls_verbose_out("rpctlscd_null: done\n");
304b9cbc85dSRick Macklem 	return (TRUE);
305b9cbc85dSRick Macklem }
306b9cbc85dSRick Macklem 
307b9cbc85dSRick Macklem bool_t
308b9cbc85dSRick Macklem rpctlscd_connect_1_svc(struct rpctlscd_connect_arg *argp,
309b9cbc85dSRick Macklem     struct rpctlscd_connect_res *result, __unused struct svc_req *rqstp)
310b9cbc85dSRick Macklem {
311b9cbc85dSRick Macklem 	int s;
312b9cbc85dSRick Macklem 	SSL *ssl;
313b9cbc85dSRick Macklem 	struct ssl_entry *newslp;
314b9cbc85dSRick Macklem 	X509 *cert;
315b9cbc85dSRick Macklem 
316b9cbc85dSRick Macklem 	rpctls_verbose_out("rpctlsd_connect: started\n");
317b9cbc85dSRick Macklem 	/* Get the socket fd from the kernel. */
318b9cbc85dSRick Macklem 	s = rpctls_syscall(RPCTLS_SYSC_CLSOCKET, "");
319b9cbc85dSRick Macklem 	if (s < 0) {
320b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_NOSOCKET;
321b9cbc85dSRick Macklem 		return (TRUE);
322b9cbc85dSRick Macklem 	}
323b9cbc85dSRick Macklem 
324b9cbc85dSRick Macklem 	/* Do a TLS connect handshake. */
325b9cbc85dSRick Macklem 	ssl = rpctls_connect(rpctls_ctx, s, argp->certname.certname_val,
326b9cbc85dSRick Macklem 	    argp->certname.certname_len, &cert);
327b9cbc85dSRick Macklem 	if (ssl == NULL) {
328b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctlsd_connect: can't do TLS "
329b9cbc85dSRick Macklem 		    "handshake\n");
330b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_NOSSL;
331b9cbc85dSRick Macklem 	} else {
332b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_OK;
333b9cbc85dSRick Macklem 		result->sec = rpctls_ssl_sec;
334b9cbc85dSRick Macklem 		result->usec = rpctls_ssl_usec;
335b9cbc85dSRick Macklem 		result->ssl = ++rpctls_ssl_refno;
336b9cbc85dSRick Macklem 		/* Hard to believe this will ever wrap around.. */
337b9cbc85dSRick Macklem 		if (rpctls_ssl_refno == 0)
338b9cbc85dSRick Macklem 			result->ssl = ++rpctls_ssl_refno;
339b9cbc85dSRick Macklem 	}
340b9cbc85dSRick Macklem 
341b9cbc85dSRick Macklem 	if (ssl == NULL) {
342b9cbc85dSRick Macklem 		/*
343b9cbc85dSRick Macklem 		 * For RPC-over-TLS, this upcall is expected
344b9cbc85dSRick Macklem 		 * to close off the socket.
345b9cbc85dSRick Macklem 		 */
346b9cbc85dSRick Macklem 		close(s);
347b9cbc85dSRick Macklem 		return (TRUE);
348b9cbc85dSRick Macklem 	}
349b9cbc85dSRick Macklem 
350b9cbc85dSRick Macklem 	/* Maintain list of all current SSL *'s */
351b9cbc85dSRick Macklem 	newslp = malloc(sizeof(*newslp));
352b9cbc85dSRick Macklem 	newslp->refno = rpctls_ssl_refno;
353b9cbc85dSRick Macklem 	newslp->s = s;
354b9cbc85dSRick Macklem 	newslp->shutoff = false;
355b9cbc85dSRick Macklem 	newslp->ssl = ssl;
356b9cbc85dSRick Macklem 	newslp->cert = cert;
357b9cbc85dSRick Macklem 	LIST_INSERT_HEAD(&rpctls_ssllist, newslp, next);
358b9cbc85dSRick Macklem 	return (TRUE);
359b9cbc85dSRick Macklem }
360b9cbc85dSRick Macklem 
361b9cbc85dSRick Macklem bool_t
362b9cbc85dSRick Macklem rpctlscd_handlerecord_1_svc(struct rpctlscd_handlerecord_arg *argp,
363b9cbc85dSRick Macklem     struct rpctlscd_handlerecord_res *result, __unused struct svc_req *rqstp)
364b9cbc85dSRick Macklem {
365b9cbc85dSRick Macklem 	struct ssl_entry *slp;
366b9cbc85dSRick Macklem 	int ret;
367b9cbc85dSRick Macklem 	char junk;
368b9cbc85dSRick Macklem 
369b9cbc85dSRick Macklem 	slp = NULL;
370b9cbc85dSRick Macklem 	if (argp->sec == rpctls_ssl_sec && argp->usec ==
371b9cbc85dSRick Macklem 	    rpctls_ssl_usec) {
372b9cbc85dSRick Macklem 		LIST_FOREACH(slp, &rpctls_ssllist, next) {
373b9cbc85dSRick Macklem 			if (slp->refno == argp->ssl)
374b9cbc85dSRick Macklem 				break;
375b9cbc85dSRick Macklem 		}
376b9cbc85dSRick Macklem 	}
377b9cbc85dSRick Macklem 
378b9cbc85dSRick Macklem 	if (slp != NULL) {
379b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctlscd_handlerecord fd=%d\n",
380b9cbc85dSRick Macklem 		    slp->s);
381b9cbc85dSRick Macklem 		/*
382b9cbc85dSRick Macklem 		 * An SSL_read() of 0 bytes should fail, but it should
383b9cbc85dSRick Macklem 		 * handle the non-application data record before doing so.
384b9cbc85dSRick Macklem 		 */
385b9cbc85dSRick Macklem 		ret = SSL_read(slp->ssl, &junk, 0);
386b9cbc85dSRick Macklem 		if (ret <= 0) {
387b9cbc85dSRick Macklem 			/* Check to see if this was a close alert. */
388b9cbc85dSRick Macklem 			ret = SSL_get_shutdown(slp->ssl);
389b9cbc85dSRick Macklem 			if ((ret & (SSL_SENT_SHUTDOWN |
390b9cbc85dSRick Macklem 			    SSL_RECEIVED_SHUTDOWN)) == SSL_RECEIVED_SHUTDOWN)
391b9cbc85dSRick Macklem 				SSL_shutdown(slp->ssl);
392b9cbc85dSRick Macklem 		} else {
393b9cbc85dSRick Macklem 			if (rpctls_debug_level == 0)
394b9cbc85dSRick Macklem 				syslog(LOG_ERR, "SSL_read returned %d", ret);
395b9cbc85dSRick Macklem 			else
396b9cbc85dSRick Macklem 				fprintf(stderr, "SSL_read returned %d\n", ret);
397b9cbc85dSRick Macklem 		}
398b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_OK;
399b9cbc85dSRick Macklem 	} else
400b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_NOSSL;
401b9cbc85dSRick Macklem 	return (TRUE);
402b9cbc85dSRick Macklem }
403b9cbc85dSRick Macklem 
404b9cbc85dSRick Macklem bool_t
405b9cbc85dSRick Macklem rpctlscd_disconnect_1_svc(struct rpctlscd_disconnect_arg *argp,
406b9cbc85dSRick Macklem     struct rpctlscd_disconnect_res *result, __unused struct svc_req *rqstp)
407b9cbc85dSRick Macklem {
408b9cbc85dSRick Macklem 	struct ssl_entry *slp;
409b9cbc85dSRick Macklem 	int ret;
410b9cbc85dSRick Macklem 
411b9cbc85dSRick Macklem 	slp = NULL;
412b9cbc85dSRick Macklem 	if (argp->sec == rpctls_ssl_sec && argp->usec ==
413b9cbc85dSRick Macklem 	    rpctls_ssl_usec) {
414b9cbc85dSRick Macklem 		LIST_FOREACH(slp, &rpctls_ssllist, next) {
415b9cbc85dSRick Macklem 			if (slp->refno == argp->ssl)
416b9cbc85dSRick Macklem 				break;
417b9cbc85dSRick Macklem 		}
418b9cbc85dSRick Macklem 	}
419b9cbc85dSRick Macklem 
420b9cbc85dSRick Macklem 	if (slp != NULL) {
421b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctlscd_disconnect: fd=%d closed\n",
422b9cbc85dSRick Macklem 		    slp->s);
423b9cbc85dSRick Macklem 		LIST_REMOVE(slp, next);
424b9cbc85dSRick Macklem 		if (!slp->shutoff) {
425b9cbc85dSRick Macklem 			ret = SSL_get_shutdown(slp->ssl);
426b9cbc85dSRick Macklem 			/*
427b9cbc85dSRick Macklem 			 * Do an SSL_shutdown() unless a close alert has
428b9cbc85dSRick Macklem 			 * already been sent.
429b9cbc85dSRick Macklem 			 */
430b9cbc85dSRick Macklem 			if ((ret & SSL_SENT_SHUTDOWN) == 0)
431b9cbc85dSRick Macklem 				SSL_shutdown(slp->ssl);
432b9cbc85dSRick Macklem 		}
433b9cbc85dSRick Macklem 		SSL_free(slp->ssl);
434b9cbc85dSRick Macklem 		if (slp->cert != NULL)
435b9cbc85dSRick Macklem 			X509_free(slp->cert);
436b9cbc85dSRick Macklem 		/*
437b9cbc85dSRick Macklem 		 * For RPC-over-TLS, this upcall is expected
438b9cbc85dSRick Macklem 		 * to close off the socket.
439b9cbc85dSRick Macklem 		 */
440b9cbc85dSRick Macklem 		if (!slp->shutoff)
441b9cbc85dSRick Macklem 			shutdown(slp->s, SHUT_WR);
442b9cbc85dSRick Macklem 		close(slp->s);
443b9cbc85dSRick Macklem 		free(slp);
444b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_OK;
445b9cbc85dSRick Macklem 	} else
446b9cbc85dSRick Macklem 		result->reterr = RPCTLSERR_NOCLOSE;
447b9cbc85dSRick Macklem 	return (TRUE);
448b9cbc85dSRick Macklem }
449b9cbc85dSRick Macklem 
450b9cbc85dSRick Macklem int
451b9cbc85dSRick Macklem rpctlscd_1_freeresult(__unused SVCXPRT *transp, __unused xdrproc_t xdr_result,
452b9cbc85dSRick Macklem     __unused caddr_t result)
453b9cbc85dSRick Macklem {
454b9cbc85dSRick Macklem 
455b9cbc85dSRick Macklem 	return (TRUE);
456b9cbc85dSRick Macklem }
457b9cbc85dSRick Macklem 
458b9cbc85dSRick Macklem static void
459b9cbc85dSRick Macklem rpctlscd_terminate(int sig __unused)
460b9cbc85dSRick Macklem {
461b9cbc85dSRick Macklem 
462b9cbc85dSRick Macklem 	rpctls_syscall(RPCTLS_SYSC_CLSHUTDOWN, "");
463b9cbc85dSRick Macklem 	pidfile_remove(rpctls_pfh);
464b9cbc85dSRick Macklem 	exit(0);
465b9cbc85dSRick Macklem }
466b9cbc85dSRick Macklem 
467b9cbc85dSRick Macklem static SSL_CTX *
468b9cbc85dSRick Macklem rpctls_setupcl_ssl(void)
469b9cbc85dSRick Macklem {
470b9cbc85dSRick Macklem 	SSL_CTX *ctx;
471b9cbc85dSRick Macklem 	char path[PATH_MAX];
472b9cbc85dSRick Macklem 	size_t len, rlen;
473b9cbc85dSRick Macklem 	int ret;
474b9cbc85dSRick Macklem 
475b9cbc85dSRick Macklem 	SSL_library_init();
476b9cbc85dSRick Macklem 	SSL_load_error_strings();
477b9cbc85dSRick Macklem 	OpenSSL_add_all_algorithms();
478b9cbc85dSRick Macklem 
479b9cbc85dSRick Macklem 	ctx = SSL_CTX_new(TLS_client_method());
480b9cbc85dSRick Macklem 	if (ctx == NULL) {
481b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_setupcl_ssl: SSL_CTX_new "
482b9cbc85dSRick Macklem 		    "failed\n");
483b9cbc85dSRick Macklem 		return (NULL);
484b9cbc85dSRick Macklem 	}
485b9cbc85dSRick Macklem 	SSL_CTX_set_ecdh_auto(ctx, 1);
486b9cbc85dSRick Macklem 
487b9cbc85dSRick Macklem 	if (rpctls_ciphers != NULL) {
488b9cbc85dSRick Macklem 		/*
489*f5b40aa0SRick Macklem 		 * Set available ciphers, since KERN_TLS only supports a
490b9cbc85dSRick Macklem 		 * few of them.
491b9cbc85dSRick Macklem 		 */
492*f5b40aa0SRick Macklem 		ret = SSL_CTX_set_ciphersuites(ctx, rpctls_ciphers);
493b9cbc85dSRick Macklem 		if (ret == 0) {
494b9cbc85dSRick Macklem 			rpctls_verbose_out("rpctls_setupcl_ssl: "
495*f5b40aa0SRick Macklem 			    "SSL_CTX_set_ciphersuites failed: %s\n",
496b9cbc85dSRick Macklem 			    rpctls_ciphers);
497b9cbc85dSRick Macklem 			SSL_CTX_free(ctx);
498b9cbc85dSRick Macklem 			return (NULL);
499b9cbc85dSRick Macklem 		}
500b9cbc85dSRick Macklem 	}
501b9cbc85dSRick Macklem 
502b9cbc85dSRick Macklem 	/*
503b9cbc85dSRick Macklem 	 * If rpctls_cert is true, a certificate and key exists in
504b9cbc85dSRick Macklem 	 * rpctls_certdir, so that it can do mutual authentication.
505b9cbc85dSRick Macklem 	 */
506b9cbc85dSRick Macklem 	if (rpctls_cert) {
507b9cbc85dSRick Macklem 		/* Get the cert.pem and certkey.pem files. */
508b9cbc85dSRick Macklem 		len = strlcpy(path, rpctls_certdir, sizeof(path));
509b9cbc85dSRick Macklem 		rlen = sizeof(path) - len;
510b9cbc85dSRick Macklem 		if (strlcpy(&path[len], "cert.pem", rlen) != 8) {
511b9cbc85dSRick Macklem 			SSL_CTX_free(ctx);
512b9cbc85dSRick Macklem 			return (NULL);
513b9cbc85dSRick Macklem 		}
514b9cbc85dSRick Macklem 		ret = SSL_CTX_use_certificate_file(ctx, path,
515b9cbc85dSRick Macklem 		    SSL_FILETYPE_PEM);
516b9cbc85dSRick Macklem 		if (ret != 1) {
517b9cbc85dSRick Macklem 			rpctls_verbose_out("rpctls_setupcl_ssl: can't use "
518b9cbc85dSRick Macklem 			    "certificate file path=%s ret=%d\n", path, ret);
519b9cbc85dSRick Macklem 			SSL_CTX_free(ctx);
520b9cbc85dSRick Macklem 			return (NULL);
521b9cbc85dSRick Macklem 		}
522b9cbc85dSRick Macklem 		if (strlcpy(&path[len], "certkey.pem", rlen) != 11) {
523b9cbc85dSRick Macklem 			SSL_CTX_free(ctx);
524b9cbc85dSRick Macklem 			return (NULL);
525b9cbc85dSRick Macklem 		}
526b9cbc85dSRick Macklem 		ret = SSL_CTX_use_PrivateKey_file(ctx, path,
527b9cbc85dSRick Macklem 		    SSL_FILETYPE_PEM);
528b9cbc85dSRick Macklem 		if (ret != 1) {
529b9cbc85dSRick Macklem 			rpctls_verbose_out("rpctls_setupcl_ssl: Can't use "
530b9cbc85dSRick Macklem 			    "private key path=%s ret=%d\n", path, ret);
531b9cbc85dSRick Macklem 			SSL_CTX_free(ctx);
532b9cbc85dSRick Macklem 			return (NULL);
533b9cbc85dSRick Macklem 		}
534b9cbc85dSRick Macklem 	}
535b9cbc85dSRick Macklem 
536b9cbc85dSRick Macklem 	if (rpctls_verify_cafile != NULL || rpctls_verify_capath != NULL) {
537b9cbc85dSRick Macklem 		if (rpctls_crlfile != NULL) {
538b9cbc85dSRick Macklem 			ret = rpctls_loadcrlfile(ctx);
539b9cbc85dSRick Macklem 			if (ret == 0) {
540b9cbc85dSRick Macklem 				rpctls_verbose_out("rpctls_setupcl_ssl: "
541b9cbc85dSRick Macklem 				    "Load CRLfile failed\n");
542b9cbc85dSRick Macklem 				SSL_CTX_free(ctx);
543b9cbc85dSRick Macklem 				return (NULL);
544b9cbc85dSRick Macklem 			}
545b9cbc85dSRick Macklem 		}
546b9cbc85dSRick Macklem #if OPENSSL_VERSION_NUMBER >= 0x30000000
547b9cbc85dSRick Macklem 		ret = 1;
548b9cbc85dSRick Macklem 		if (rpctls_verify_cafile != NULL)
549b9cbc85dSRick Macklem 			ret = SSL_CTX_load_verify_file(ctx,
550b9cbc85dSRick Macklem 			    rpctls_verify_cafile);
551b9cbc85dSRick Macklem 		if (ret != 0 && rpctls_verify_capath != NULL)
552b9cbc85dSRick Macklem 			ret = SSL_CTX_load_verify_dir(ctx,
553b9cbc85dSRick Macklem 			    rpctls_verify_capath);
554b9cbc85dSRick Macklem #else
555b9cbc85dSRick Macklem 		ret = SSL_CTX_load_verify_locations(ctx,
556b9cbc85dSRick Macklem 		    rpctls_verify_cafile, rpctls_verify_capath);
557b9cbc85dSRick Macklem #endif
558b9cbc85dSRick Macklem 		if (ret == 0) {
559b9cbc85dSRick Macklem 			rpctls_verbose_out("rpctls_setupcl_ssl: "
560b9cbc85dSRick Macklem 			    "Can't load verify locations\n");
561b9cbc85dSRick Macklem 			SSL_CTX_free(ctx);
562b9cbc85dSRick Macklem 			return (NULL);
563b9cbc85dSRick Macklem 		}
564b9cbc85dSRick Macklem 		/*
565b9cbc85dSRick Macklem 		 * The man page says that the
566b9cbc85dSRick Macklem 		 * SSL_CTX_set0_CA_list() call is not normally
567b9cbc85dSRick Macklem 		 * needed, but I believe it is harmless.
568b9cbc85dSRick Macklem 		 */
569b9cbc85dSRick Macklem 		if (rpctls_verify_cafile != NULL)
570b9cbc85dSRick Macklem 			SSL_CTX_set0_CA_list(ctx,
571b9cbc85dSRick Macklem 			    SSL_load_client_CA_file(rpctls_verify_cafile));
572b9cbc85dSRick Macklem 	}
573b9cbc85dSRick Macklem 
57472bf76d6SRick Macklem 	/*
57572bf76d6SRick Macklem 	 * The RFC specifies that RPC-over-TLS must use TLS1.3.
57672bf76d6SRick Macklem 	 * However, early FreeBSD versions (13.0, 13.1) did not
57772bf76d6SRick Macklem 	 * support RX for KTLS1.3, so TLS1.2 needs to be used for
57872bf76d6SRick Macklem 	 * these servers.
57972bf76d6SRick Macklem 	 */
58072bf76d6SRick Macklem 	ret = SSL_CTX_set_min_proto_version(ctx, rpctls_tlsvers);
58172bf76d6SRick Macklem 	if (ret == 0) {
58272bf76d6SRick Macklem 		rpctls_verbose_out("rpctls_setupcl_ssl: "
58372bf76d6SRick Macklem 		    "SSL_CTX_set_min_proto_version failed\n");
58472bf76d6SRick Macklem 		SSL_CTX_free(ctx);
58572bf76d6SRick Macklem 		return (NULL);
58672bf76d6SRick Macklem 	}
58772bf76d6SRick Macklem 	ret = SSL_CTX_set_max_proto_version(ctx, rpctls_tlsvers);
58872bf76d6SRick Macklem 	if (ret == 0) {
58972bf76d6SRick Macklem 		rpctls_verbose_out("rpctls_setupcl_ssl: "
59072bf76d6SRick Macklem 		    "SSL_CTX_set_max_proto_version failed\n");
59172bf76d6SRick Macklem 		SSL_CTX_free(ctx);
59272bf76d6SRick Macklem 		return (NULL);
59372bf76d6SRick Macklem 	}
59472bf76d6SRick Macklem 
595c7bb0f47SJohn Baldwin #ifdef SSL_OP_ENABLE_KTLS
59672bf76d6SRick Macklem 	SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
597c7bb0f47SJohn Baldwin #endif
598c7bb0f47SJohn Baldwin #ifdef SSL_MODE_NO_KTLS_TX
599b9cbc85dSRick Macklem 	SSL_CTX_clear_mode(ctx, SSL_MODE_NO_KTLS_TX | SSL_MODE_NO_KTLS_RX);
600c7bb0f47SJohn Baldwin #endif
601b9cbc85dSRick Macklem 	return (ctx);
602b9cbc85dSRick Macklem }
603b9cbc85dSRick Macklem 
604b9cbc85dSRick Macklem static SSL *
605b9cbc85dSRick Macklem rpctls_connect(SSL_CTX *ctx, int s, char *certname, u_int certlen, X509 **certp)
606b9cbc85dSRick Macklem {
607b9cbc85dSRick Macklem 	SSL *ssl;
608b9cbc85dSRick Macklem 	X509 *cert;
609b9cbc85dSRick Macklem 	struct sockaddr_storage ad;
610b9cbc85dSRick Macklem 	struct sockaddr *sad;
611b9cbc85dSRick Macklem 	char hostnam[NI_MAXHOST], path[PATH_MAX];
612b9cbc85dSRick Macklem 	int gethostret, ret;
613b9cbc85dSRick Macklem 	char *cp, *cp2;
614b9cbc85dSRick Macklem 	size_t len, rlen;
615b9cbc85dSRick Macklem 	long verfret;
616b9cbc85dSRick Macklem 
617b9cbc85dSRick Macklem 	*certp = NULL;
618b9cbc85dSRick Macklem 	sad = (struct sockaddr *)&ad;
619b9cbc85dSRick Macklem 	ssl = SSL_new(ctx);
620b9cbc85dSRick Macklem 	if (ssl == NULL) {
621b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_connect: "
622b9cbc85dSRick Macklem 		    "SSL_new failed\n");
623b9cbc85dSRick Macklem 		return (NULL);
624b9cbc85dSRick Macklem 	}
625b9cbc85dSRick Macklem 	if (SSL_set_fd(ssl, s) != 1) {
626b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_connect: "
627b9cbc85dSRick Macklem 		    "SSL_set_fd failed\n");
628b9cbc85dSRick Macklem 		SSL_free(ssl);
629b9cbc85dSRick Macklem 		return (NULL);
630b9cbc85dSRick Macklem 	}
631b9cbc85dSRick Macklem 
632b9cbc85dSRick Macklem 	/*
633b9cbc85dSRick Macklem 	 * If rpctls_cert is true and certname is set, a alternate certificate
634b9cbc85dSRick Macklem 	 * and key exists in files named <certname>.pem and <certname>key.pem
635b9cbc85dSRick Macklem 	 * in rpctls_certdir that is to be used for mutual authentication.
636b9cbc85dSRick Macklem 	 */
637b9cbc85dSRick Macklem 	if (rpctls_cert && certlen > 0) {
638b9cbc85dSRick Macklem 		len = strlcpy(path, rpctls_certdir, sizeof(path));
639b9cbc85dSRick Macklem 		rlen = sizeof(path) - len;
640b9cbc85dSRick Macklem 		if (rlen <= certlen) {
641b9cbc85dSRick Macklem 			SSL_free(ssl);
642b9cbc85dSRick Macklem 			return (NULL);
643b9cbc85dSRick Macklem 		}
644b9cbc85dSRick Macklem 		memcpy(&path[len], certname, certlen);
645b9cbc85dSRick Macklem 		rlen -= certlen;
646b9cbc85dSRick Macklem 		len += certlen;
647b9cbc85dSRick Macklem 		path[len] = '\0';
648b9cbc85dSRick Macklem 		if (strlcpy(&path[len], ".pem", rlen) != 4) {
649b9cbc85dSRick Macklem 			SSL_free(ssl);
650b9cbc85dSRick Macklem 			return (NULL);
651b9cbc85dSRick Macklem 		}
652b9cbc85dSRick Macklem 		ret = SSL_use_certificate_file(ssl, path, SSL_FILETYPE_PEM);
653b9cbc85dSRick Macklem 		if (ret != 1) {
654b9cbc85dSRick Macklem 			rpctls_verbose_out("rpctls_connect: can't use "
655b9cbc85dSRick Macklem 			    "certificate file path=%s ret=%d\n", path, ret);
656b9cbc85dSRick Macklem 			SSL_free(ssl);
657b9cbc85dSRick Macklem 			return (NULL);
658b9cbc85dSRick Macklem 		}
659b9cbc85dSRick Macklem 		if (strlcpy(&path[len], "key.pem", rlen) != 7) {
660b9cbc85dSRick Macklem 			SSL_free(ssl);
661b9cbc85dSRick Macklem 			return (NULL);
662b9cbc85dSRick Macklem 		}
663b9cbc85dSRick Macklem 		ret = SSL_use_PrivateKey_file(ssl, path, SSL_FILETYPE_PEM);
664b9cbc85dSRick Macklem 		if (ret != 1) {
665b9cbc85dSRick Macklem 			rpctls_verbose_out("rpctls_connect: Can't use "
666b9cbc85dSRick Macklem 			    "private key path=%s ret=%d\n", path, ret);
667b9cbc85dSRick Macklem 			SSL_free(ssl);
668b9cbc85dSRick Macklem 			return (NULL);
669b9cbc85dSRick Macklem 		}
670b9cbc85dSRick Macklem 	}
671b9cbc85dSRick Macklem 
672b9cbc85dSRick Macklem 	ret = SSL_connect(ssl);
673b9cbc85dSRick Macklem 	if (ret != 1) {
674b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_connect: "
675b9cbc85dSRick Macklem 		    "SSL_connect failed %d\n",
676b9cbc85dSRick Macklem 		    ret);
677b9cbc85dSRick Macklem 		SSL_free(ssl);
678b9cbc85dSRick Macklem 		return (NULL);
679b9cbc85dSRick Macklem 	}
680b9cbc85dSRick Macklem 
681b9cbc85dSRick Macklem 	cert = SSL_get_peer_certificate(ssl);
682b9cbc85dSRick Macklem 	if (cert == NULL) {
683b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_connect: get peer"
684b9cbc85dSRick Macklem 		    " certificate failed\n");
685b9cbc85dSRick Macklem 		SSL_free(ssl);
686b9cbc85dSRick Macklem 		return (NULL);
687b9cbc85dSRick Macklem 	}
688b9cbc85dSRick Macklem 	gethostret = rpctls_gethost(s, sad, hostnam, sizeof(hostnam));
689b9cbc85dSRick Macklem 	if (gethostret == 0)
690b9cbc85dSRick Macklem 		hostnam[0] = '\0';
691b9cbc85dSRick Macklem 	verfret = SSL_get_verify_result(ssl);
692b9cbc85dSRick Macklem 	if (verfret == X509_V_OK && (rpctls_verify_cafile != NULL ||
693b9cbc85dSRick Macklem 	    rpctls_verify_capath != NULL) && (gethostret == 0 ||
694b9cbc85dSRick Macklem 	    rpctls_checkhost(sad, cert, X509_CHECK_FLAG_NO_WILDCARDS) != 1))
695b9cbc85dSRick Macklem 		verfret = X509_V_ERR_HOSTNAME_MISMATCH;
696b9cbc85dSRick Macklem 	if (verfret != X509_V_OK && (rpctls_verify_cafile != NULL ||
697b9cbc85dSRick Macklem 	    rpctls_verify_capath != NULL)) {
698b9cbc85dSRick Macklem 		if (verfret != X509_V_OK) {
699b9cbc85dSRick Macklem 			cp = X509_NAME_oneline(X509_get_issuer_name(cert),
700b9cbc85dSRick Macklem 			    NULL, 0);
701b9cbc85dSRick Macklem 			cp2 = X509_NAME_oneline(X509_get_subject_name(cert),
702b9cbc85dSRick Macklem 			    NULL, 0);
703b9cbc85dSRick Macklem 			if (rpctls_debug_level == 0)
704b9cbc85dSRick Macklem 				syslog(LOG_INFO | LOG_DAEMON,
705b9cbc85dSRick Macklem 				    "rpctls_connect: client IP %s "
706b9cbc85dSRick Macklem 				    "issuerName=%s subjectName=%s verify "
707b9cbc85dSRick Macklem 				    "failed %s\n", hostnam, cp, cp2,
708b9cbc85dSRick Macklem 				    X509_verify_cert_error_string(verfret));
709b9cbc85dSRick Macklem 			else
710b9cbc85dSRick Macklem 				fprintf(stderr,
711b9cbc85dSRick Macklem 				    "rpctls_connect: client IP %s "
712b9cbc85dSRick Macklem 				    "issuerName=%s subjectName=%s verify "
713b9cbc85dSRick Macklem 				    "failed %s\n", hostnam, cp, cp2,
714b9cbc85dSRick Macklem 				    X509_verify_cert_error_string(verfret));
715b9cbc85dSRick Macklem 		}
716b9cbc85dSRick Macklem 		X509_free(cert);
717b9cbc85dSRick Macklem 		SSL_free(ssl);
718b9cbc85dSRick Macklem 		return (NULL);
719b9cbc85dSRick Macklem 	}
720b9cbc85dSRick Macklem 
721b9cbc85dSRick Macklem 	/* Check to see if ktls is enabled on the connection. */
722b9cbc85dSRick Macklem 	ret = BIO_get_ktls_send(SSL_get_wbio(ssl));
723b9cbc85dSRick Macklem 	rpctls_verbose_out("rpctls_connect: BIO_get_ktls_send=%d\n", ret);
724b9cbc85dSRick Macklem 	if (ret != 0) {
725b9cbc85dSRick Macklem 		ret = BIO_get_ktls_recv(SSL_get_rbio(ssl));
726b9cbc85dSRick Macklem 		rpctls_verbose_out("rpctls_connect: BIO_get_ktls_recv=%d\n",
727b9cbc85dSRick Macklem 		    ret);
728b9cbc85dSRick Macklem 	}
729b9cbc85dSRick Macklem 	if (ret == 0) {
730b9cbc85dSRick Macklem 		if (rpctls_debug_level == 0)
731b9cbc85dSRick Macklem 			syslog(LOG_ERR, "ktls not working\n");
732b9cbc85dSRick Macklem 		else
733b9cbc85dSRick Macklem 			fprintf(stderr, "ktls not working\n");
734b9cbc85dSRick Macklem 		X509_free(cert);
735b9cbc85dSRick Macklem 		SSL_free(ssl);
736b9cbc85dSRick Macklem 		return (NULL);
737b9cbc85dSRick Macklem 	}
738b9cbc85dSRick Macklem 	if (ret == X509_V_OK && (rpctls_verify_cafile != NULL ||
739b9cbc85dSRick Macklem 	    rpctls_verify_capath != NULL) && rpctls_crlfile != NULL)
740b9cbc85dSRick Macklem 		*certp = cert;
741b9cbc85dSRick Macklem 	else
742b9cbc85dSRick Macklem 		X509_free(cert);
743b9cbc85dSRick Macklem 
744b9cbc85dSRick Macklem 	return (ssl);
745b9cbc85dSRick Macklem }
746b9cbc85dSRick Macklem 
747b9cbc85dSRick Macklem static void
748b9cbc85dSRick Macklem rpctls_huphandler(int sig __unused)
749b9cbc85dSRick Macklem {
750b9cbc85dSRick Macklem 
751b9cbc85dSRick Macklem 	rpctls_gothup = true;
752b9cbc85dSRick Macklem }
753