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