xref: /freebsd/usr.sbin/rpc.tlsclntd/rpc.tlsclntd.c (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
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 	return (0);
303 }
304 
305 bool_t
306 rpctlscd_null_1_svc(__unused void *argp, __unused void *result,
307     __unused struct svc_req *rqstp)
308 {
309 
310 	rpctls_verbose_out("rpctlscd_null: done\n");
311 	return (TRUE);
312 }
313 
314 bool_t
315 rpctlscd_connect_1_svc(struct rpctlscd_connect_arg *argp,
316     struct rpctlscd_connect_res *result, __unused struct svc_req *rqstp)
317 {
318 	int s;
319 	SSL *ssl;
320 	struct ssl_entry *newslp;
321 	X509 *cert;
322 
323 	rpctls_verbose_out("rpctlsd_connect: started\n");
324 	/* Get the socket fd from the kernel. */
325 	s = rpctls_syscall(RPCTLS_SYSC_CLSOCKET, "");
326 	if (s < 0) {
327 		result->reterr = RPCTLSERR_NOSOCKET;
328 		return (TRUE);
329 	}
330 
331 	/* Do a TLS connect handshake. */
332 	ssl = rpctls_connect(rpctls_ctx, s, argp->certname.certname_val,
333 	    argp->certname.certname_len, &cert);
334 	if (ssl == NULL) {
335 		rpctls_verbose_out("rpctlsd_connect: can't do TLS "
336 		    "handshake\n");
337 		result->reterr = RPCTLSERR_NOSSL;
338 	} else {
339 		result->reterr = RPCTLSERR_OK;
340 		result->sec = rpctls_ssl_sec;
341 		result->usec = rpctls_ssl_usec;
342 		result->ssl = ++rpctls_ssl_refno;
343 		/* Hard to believe this will ever wrap around.. */
344 		if (rpctls_ssl_refno == 0)
345 			result->ssl = ++rpctls_ssl_refno;
346 	}
347 
348 	if (ssl == NULL) {
349 		/*
350 		 * For RPC-over-TLS, this upcall is expected
351 		 * to close off the socket.
352 		 */
353 		close(s);
354 		return (TRUE);
355 	}
356 
357 	/* Maintain list of all current SSL *'s */
358 	newslp = malloc(sizeof(*newslp));
359 	newslp->refno = rpctls_ssl_refno;
360 	newslp->s = s;
361 	newslp->shutoff = false;
362 	newslp->ssl = ssl;
363 	newslp->cert = cert;
364 	LIST_INSERT_HEAD(&rpctls_ssllist, newslp, next);
365 	return (TRUE);
366 }
367 
368 bool_t
369 rpctlscd_handlerecord_1_svc(struct rpctlscd_handlerecord_arg *argp,
370     struct rpctlscd_handlerecord_res *result, __unused struct svc_req *rqstp)
371 {
372 	struct ssl_entry *slp;
373 	int ret;
374 	char junk;
375 
376 	slp = NULL;
377 	if (argp->sec == rpctls_ssl_sec && argp->usec ==
378 	    rpctls_ssl_usec) {
379 		LIST_FOREACH(slp, &rpctls_ssllist, next) {
380 			if (slp->refno == argp->ssl)
381 				break;
382 		}
383 	}
384 
385 	if (slp != NULL) {
386 		rpctls_verbose_out("rpctlscd_handlerecord fd=%d\n",
387 		    slp->s);
388 		/*
389 		 * An SSL_read() of 0 bytes should fail, but it should
390 		 * handle the non-application data record before doing so.
391 		 */
392 		ret = SSL_read(slp->ssl, &junk, 0);
393 		if (ret <= 0) {
394 			/* Check to see if this was a close alert. */
395 			ret = SSL_get_shutdown(slp->ssl);
396 			if ((ret & (SSL_SENT_SHUTDOWN |
397 			    SSL_RECEIVED_SHUTDOWN)) == SSL_RECEIVED_SHUTDOWN)
398 				SSL_shutdown(slp->ssl);
399 		} else {
400 			if (rpctls_debug_level == 0)
401 				syslog(LOG_ERR, "SSL_read returned %d", ret);
402 			else
403 				fprintf(stderr, "SSL_read returned %d\n", ret);
404 		}
405 		result->reterr = RPCTLSERR_OK;
406 	} else
407 		result->reterr = RPCTLSERR_NOSSL;
408 	return (TRUE);
409 }
410 
411 bool_t
412 rpctlscd_disconnect_1_svc(struct rpctlscd_disconnect_arg *argp,
413     struct rpctlscd_disconnect_res *result, __unused struct svc_req *rqstp)
414 {
415 	struct ssl_entry *slp;
416 	int ret;
417 
418 	slp = NULL;
419 	if (argp->sec == rpctls_ssl_sec && argp->usec ==
420 	    rpctls_ssl_usec) {
421 		LIST_FOREACH(slp, &rpctls_ssllist, next) {
422 			if (slp->refno == argp->ssl)
423 				break;
424 		}
425 	}
426 
427 	if (slp != NULL) {
428 		rpctls_verbose_out("rpctlscd_disconnect: fd=%d closed\n",
429 		    slp->s);
430 		LIST_REMOVE(slp, next);
431 		if (!slp->shutoff) {
432 			ret = SSL_get_shutdown(slp->ssl);
433 			/*
434 			 * Do an SSL_shutdown() unless a close alert has
435 			 * already been sent.
436 			 */
437 			if ((ret & SSL_SENT_SHUTDOWN) == 0)
438 				SSL_shutdown(slp->ssl);
439 		}
440 		SSL_free(slp->ssl);
441 		if (slp->cert != NULL)
442 			X509_free(slp->cert);
443 		/*
444 		 * For RPC-over-TLS, this upcall is expected
445 		 * to close off the socket.
446 		 */
447 		if (!slp->shutoff)
448 			shutdown(slp->s, SHUT_WR);
449 		close(slp->s);
450 		free(slp);
451 		result->reterr = RPCTLSERR_OK;
452 	} else
453 		result->reterr = RPCTLSERR_NOCLOSE;
454 	return (TRUE);
455 }
456 
457 int
458 rpctlscd_1_freeresult(__unused SVCXPRT *transp, __unused xdrproc_t xdr_result,
459     __unused caddr_t result)
460 {
461 
462 	return (TRUE);
463 }
464 
465 static void
466 rpctlscd_terminate(int sig __unused)
467 {
468 
469 	rpctls_syscall(RPCTLS_SYSC_CLSHUTDOWN, "");
470 	pidfile_remove(rpctls_pfh);
471 	exit(0);
472 }
473 
474 static SSL_CTX *
475 rpctls_setupcl_ssl(void)
476 {
477 	SSL_CTX *ctx;
478 	char path[PATH_MAX];
479 	size_t len, rlen;
480 	int ret;
481 
482 	ctx = SSL_CTX_new(TLS_client_method());
483 	if (ctx == NULL) {
484 		rpctls_verbose_out("rpctls_setupcl_ssl: SSL_CTX_new "
485 		    "failed\n");
486 		return (NULL);
487 	}
488 
489 	if (rpctls_ciphers != NULL) {
490 		/*
491 		 * Set available ciphers, since KERN_TLS only supports a
492 		 * few of them.
493 		 */
494 		ret = SSL_CTX_set_ciphersuites(ctx, rpctls_ciphers);
495 		if (ret == 0) {
496 			rpctls_verbose_out("rpctls_setupcl_ssl: "
497 			    "SSL_CTX_set_ciphersuites failed: %s\n",
498 			    rpctls_ciphers);
499 			SSL_CTX_free(ctx);
500 			return (NULL);
501 		}
502 	}
503 
504 	/*
505 	 * If rpctls_cert is true, a certificate and key exists in
506 	 * rpctls_certdir, so that it can do mutual authentication.
507 	 */
508 	if (rpctls_cert) {
509 		/* Get the cert.pem and certkey.pem files. */
510 		len = strlcpy(path, rpctls_certdir, sizeof(path));
511 		rlen = sizeof(path) - len;
512 		if (strlcpy(&path[len], "cert.pem", rlen) != 8) {
513 			SSL_CTX_free(ctx);
514 			return (NULL);
515 		}
516 		ret = SSL_CTX_use_certificate_file(ctx, path,
517 		    SSL_FILETYPE_PEM);
518 		if (ret != 1) {
519 			rpctls_verbose_out("rpctls_setupcl_ssl: can't use "
520 			    "certificate file path=%s ret=%d\n", path, ret);
521 			SSL_CTX_free(ctx);
522 			return (NULL);
523 		}
524 		if (strlcpy(&path[len], "certkey.pem", rlen) != 11) {
525 			SSL_CTX_free(ctx);
526 			return (NULL);
527 		}
528 		ret = SSL_CTX_use_PrivateKey_file(ctx, path,
529 		    SSL_FILETYPE_PEM);
530 		if (ret != 1) {
531 			rpctls_verbose_out("rpctls_setupcl_ssl: Can't use "
532 			    "private key path=%s ret=%d\n", path, ret);
533 			SSL_CTX_free(ctx);
534 			return (NULL);
535 		}
536 	}
537 
538 	if (rpctls_verify_cafile != NULL || rpctls_verify_capath != NULL) {
539 		if (rpctls_crlfile != NULL) {
540 			ret = rpctls_loadcrlfile(ctx);
541 			if (ret == 0) {
542 				rpctls_verbose_out("rpctls_setupcl_ssl: "
543 				    "Load CRLfile failed\n");
544 				SSL_CTX_free(ctx);
545 				return (NULL);
546 			}
547 		}
548 #if OPENSSL_VERSION_NUMBER >= 0x30000000
549 		ret = 1;
550 		if (rpctls_verify_cafile != NULL)
551 			ret = SSL_CTX_load_verify_file(ctx,
552 			    rpctls_verify_cafile);
553 		if (ret != 0 && rpctls_verify_capath != NULL)
554 			ret = SSL_CTX_load_verify_dir(ctx,
555 			    rpctls_verify_capath);
556 #else
557 		ret = SSL_CTX_load_verify_locations(ctx,
558 		    rpctls_verify_cafile, rpctls_verify_capath);
559 #endif
560 		if (ret == 0) {
561 			rpctls_verbose_out("rpctls_setupcl_ssl: "
562 			    "Can't load verify locations\n");
563 			SSL_CTX_free(ctx);
564 			return (NULL);
565 		}
566 		/*
567 		 * The man page says that the
568 		 * SSL_CTX_set0_CA_list() call is not normally
569 		 * needed, but I believe it is harmless.
570 		 */
571 		if (rpctls_verify_cafile != NULL)
572 			SSL_CTX_set0_CA_list(ctx,
573 			    SSL_load_client_CA_file(rpctls_verify_cafile));
574 	}
575 
576 	/*
577 	 * The RFC specifies that RPC-over-TLS must use TLS1.3.
578 	 * However, early FreeBSD versions (13.0, 13.1) did not
579 	 * support RX for KTLS1.3, so TLS1.2 needs to be used for
580 	 * these servers.
581 	 */
582 	ret = SSL_CTX_set_min_proto_version(ctx, rpctls_tlsvers);
583 	if (ret == 0) {
584 		rpctls_verbose_out("rpctls_setupcl_ssl: "
585 		    "SSL_CTX_set_min_proto_version failed\n");
586 		SSL_CTX_free(ctx);
587 		return (NULL);
588 	}
589 	ret = SSL_CTX_set_max_proto_version(ctx, rpctls_tlsvers);
590 	if (ret == 0) {
591 		rpctls_verbose_out("rpctls_setupcl_ssl: "
592 		    "SSL_CTX_set_max_proto_version failed\n");
593 		SSL_CTX_free(ctx);
594 		return (NULL);
595 	}
596 
597 #ifdef SSL_OP_ENABLE_KTLS
598 	SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
599 #endif
600 #ifdef SSL_MODE_NO_KTLS_TX
601 	SSL_CTX_clear_mode(ctx, SSL_MODE_NO_KTLS_TX | SSL_MODE_NO_KTLS_RX);
602 #endif
603 	return (ctx);
604 }
605 
606 static SSL *
607 rpctls_connect(SSL_CTX *ctx, int s, char *certname, u_int certlen, X509 **certp)
608 {
609 	SSL *ssl;
610 	X509 *cert;
611 	struct sockaddr_storage ad;
612 	struct sockaddr *sad;
613 	char hostnam[NI_MAXHOST], path[PATH_MAX];
614 	int gethostret, ret;
615 	char *cp, *cp2;
616 	size_t len, rlen;
617 	long verfret;
618 
619 	*certp = NULL;
620 	sad = (struct sockaddr *)&ad;
621 	ssl = SSL_new(ctx);
622 	if (ssl == NULL) {
623 		rpctls_verbose_out("rpctls_connect: "
624 		    "SSL_new failed\n");
625 		return (NULL);
626 	}
627 	if (SSL_set_fd(ssl, s) != 1) {
628 		rpctls_verbose_out("rpctls_connect: "
629 		    "SSL_set_fd failed\n");
630 		SSL_free(ssl);
631 		return (NULL);
632 	}
633 
634 	/*
635 	 * If rpctls_cert is true and certname is set, a alternate certificate
636 	 * and key exists in files named <certname>.pem and <certname>key.pem
637 	 * in rpctls_certdir that is to be used for mutual authentication.
638 	 */
639 	if (rpctls_cert && certlen > 0) {
640 		len = strlcpy(path, rpctls_certdir, sizeof(path));
641 		rlen = sizeof(path) - len;
642 		if (rlen <= certlen) {
643 			SSL_free(ssl);
644 			return (NULL);
645 		}
646 		memcpy(&path[len], certname, certlen);
647 		rlen -= certlen;
648 		len += certlen;
649 		path[len] = '\0';
650 		if (strlcpy(&path[len], ".pem", rlen) != 4) {
651 			SSL_free(ssl);
652 			return (NULL);
653 		}
654 		ret = SSL_use_certificate_file(ssl, path, SSL_FILETYPE_PEM);
655 		if (ret != 1) {
656 			rpctls_verbose_out("rpctls_connect: can't use "
657 			    "certificate file path=%s ret=%d\n", path, ret);
658 			SSL_free(ssl);
659 			return (NULL);
660 		}
661 		if (strlcpy(&path[len], "key.pem", rlen) != 7) {
662 			SSL_free(ssl);
663 			return (NULL);
664 		}
665 		ret = SSL_use_PrivateKey_file(ssl, path, SSL_FILETYPE_PEM);
666 		if (ret != 1) {
667 			rpctls_verbose_out("rpctls_connect: Can't use "
668 			    "private key path=%s ret=%d\n", path, ret);
669 			SSL_free(ssl);
670 			return (NULL);
671 		}
672 	}
673 
674 	ret = SSL_connect(ssl);
675 	if (ret != 1) {
676 		rpctls_verbose_out("rpctls_connect: "
677 		    "SSL_connect failed %d: %s\n",
678 		    ret, ERR_error_string(ERR_get_error(), NULL));
679 		SSL_free(ssl);
680 		return (NULL);
681 	}
682 
683 #if OPENSSL_VERSION_NUMBER >= 0x30000000
684 	cert = SSL_get1_peer_certificate(ssl);
685 #else
686 	cert = SSL_get_peer_certificate(ssl);
687 #endif
688 	if (cert == NULL) {
689 		rpctls_verbose_out("rpctls_connect: get peer"
690 		    " certificate failed\n");
691 		SSL_free(ssl);
692 		return (NULL);
693 	}
694 	gethostret = rpctls_gethost(s, sad, hostnam, sizeof(hostnam));
695 	if (gethostret == 0)
696 		hostnam[0] = '\0';
697 	verfret = SSL_get_verify_result(ssl);
698 	if (verfret == X509_V_OK && (rpctls_verify_cafile != NULL ||
699 	    rpctls_verify_capath != NULL) && (gethostret == 0 ||
700 	    rpctls_checkhost(sad, cert, X509_CHECK_FLAG_NO_WILDCARDS) != 1))
701 		verfret = X509_V_ERR_HOSTNAME_MISMATCH;
702 	if (verfret != X509_V_OK && (rpctls_verify_cafile != NULL ||
703 	    rpctls_verify_capath != NULL)) {
704 		if (verfret != X509_V_OK) {
705 			cp = X509_NAME_oneline(X509_get_issuer_name(cert),
706 			    NULL, 0);
707 			cp2 = X509_NAME_oneline(X509_get_subject_name(cert),
708 			    NULL, 0);
709 			if (rpctls_debug_level == 0)
710 				syslog(LOG_INFO | LOG_DAEMON,
711 				    "rpctls_connect: client IP %s "
712 				    "issuerName=%s subjectName=%s verify "
713 				    "failed %s\n", hostnam, cp, cp2,
714 				    X509_verify_cert_error_string(verfret));
715 			else
716 				fprintf(stderr,
717 				    "rpctls_connect: client IP %s "
718 				    "issuerName=%s subjectName=%s verify "
719 				    "failed %s\n", hostnam, cp, cp2,
720 				    X509_verify_cert_error_string(verfret));
721 		}
722 		X509_free(cert);
723 		SSL_free(ssl);
724 		return (NULL);
725 	}
726 
727 	/* Check to see if ktls is enabled on the connection. */
728 	ret = BIO_get_ktls_send(SSL_get_wbio(ssl));
729 	rpctls_verbose_out("rpctls_connect: BIO_get_ktls_send=%d\n", ret);
730 	if (ret != 0) {
731 		ret = BIO_get_ktls_recv(SSL_get_rbio(ssl));
732 		rpctls_verbose_out("rpctls_connect: BIO_get_ktls_recv=%d\n",
733 		    ret);
734 	}
735 	if (ret == 0) {
736 		if (rpctls_debug_level == 0)
737 			syslog(LOG_ERR, "ktls not working\n");
738 		else
739 			fprintf(stderr, "ktls not working\n");
740 		X509_free(cert);
741 		SSL_free(ssl);
742 		return (NULL);
743 	}
744 	if (ret == X509_V_OK && (rpctls_verify_cafile != NULL ||
745 	    rpctls_verify_capath != NULL) && rpctls_crlfile != NULL)
746 		*certp = cert;
747 	else
748 		X509_free(cert);
749 
750 	return (ssl);
751 }
752 
753 static void
754 rpctls_huphandler(int sig __unused)
755 {
756 
757 	rpctls_gothup = true;
758 }
759