2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Rick Macklem
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/queue.h>
31 #include <sys/syslog.h>
32 #include <sys/select.h>
43 #include <openssl/opensslconf.h>
44 #include <openssl/bio.h>
45 #include <openssl/ssl.h>
46 #include <openssl/err.h>
47 #include <openssl/x509v3.h>
49 #include "rpc.tlscommon.h"
52 * How long to delay a reload of the CRL when there are RPC request(s)
53 * to process, in usec. Must be less than 1second.
55 #define RELOADDELAY 250000
63 uint64_t curtime, nexttime;
67 /* Expand svc_run() here so that we can call rpctls_loadcrlfile(). */
68 curtime = nexttime = 0;
69 sigemptyset(&sighup_mask);
70 sigaddset(&sighup_mask, SIGHUP);
72 clock_gettime(CLOCK_MONOTONIC, &tp);
74 curtime = curtime * 1000000 + tp.tv_nsec / 1000;
75 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
76 if (rpctls_gothup && curtime >= nexttime) {
77 rpctls_gothup = false;
78 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
79 ret = rpctls_loadcrlfile(rpctls_ctx);
83 rpctls_verbose_out("rpc.tlsservd: Can't "
85 clock_gettime(CLOCK_MONOTONIC, &tp);
87 nexttime = nexttime * 1000000 + tp.tv_nsec / 1000 +
90 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
93 * If a reload is pending, poll for received request(s),
94 * otherwise set a RELOADDELAY timeout, since a SIGHUP
95 * could be processed between the got_sighup test and
96 * the select() system call.
102 tv.tv_usec = RELOADDELAY;
104 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, &tv)) {
106 if (errno == EINTR) {
107 /* Allow a reload now. */
111 syslog(LOG_ERR, "rpc.tls daemon died: select: %m");
114 /* Allow a reload now. */
118 svc_getreqset(&readfds);
124 * (re)load the CRLfile into the certificate verification store.
127 rpctls_loadcrlfile(SSL_CTX *ctx)
129 X509_STORE *certstore;
130 X509_LOOKUP *certlookup;
133 if ((rpctls_verify_cafile != NULL ||
134 rpctls_verify_capath != NULL) &&
135 rpctls_crlfile != NULL) {
136 certstore = SSL_CTX_get_cert_store(ctx);
137 certlookup = X509_STORE_add_lookup(
138 certstore, X509_LOOKUP_file());
140 if (certlookup != NULL)
141 ret = X509_load_crl_file(certlookup,
142 rpctls_crlfile, X509_FILETYPE_PEM);
144 ret = X509_STORE_set_flags(certstore,
145 X509_V_FLAG_CRL_CHECK |
146 X509_V_FLAG_CRL_CHECK_ALL);
149 "rpctls_loadcrlfile: Can't"
150 " load CRLfile=%s\n",
159 * Read the CRL file and check for any extant connections
160 * that might now be revoked.
163 rpctls_checkcrl(void)
165 struct ssl_entry *slp;
168 X509_REVOKED *revoked;
169 char *cp, *cp2, nullstr[1];
172 if (rpctls_crlfile == NULL || (rpctls_verify_cafile == NULL &&
173 rpctls_verify_capath == NULL))
175 infile = BIO_new(BIO_s_file());
176 if (infile == NULL) {
177 rpctls_verbose_out("rpctls_checkcrl: Cannot BIO_new\n");
180 ret = BIO_read_filename(infile, rpctls_crlfile);
182 rpctls_verbose_out("rpctls_checkcrl: Cannot read CRL file\n");
188 for (crl = PEM_read_bio_X509_CRL(infile, NULL, NULL, nullstr);
189 crl != NULL; crl = PEM_read_bio_X509_CRL(infile, NULL, NULL,
191 LIST_FOREACH(slp, &rpctls_ssllist, next) {
192 if (slp->cert != NULL) {
193 ret = X509_CRL_get0_by_cert(crl, &revoked,
196 * Do a shutdown on the socket, so that it
197 * can no longer be used. The kernel RPC
198 * code will notice the socket is disabled
199 * and will do a disconnect upcall, which will
203 cp2 = X509_NAME_oneline(
204 X509_get_subject_name(slp->cert),
206 cp = X509_NAME_oneline(
207 X509_get_issuer_name(slp->cert),
209 if (rpctls_debug_level == 0)
210 syslog(LOG_INFO | LOG_DAEMON,
211 "rpctls_daemon: Certificate"
215 "TCP connection closed",
219 "rpctls_daemon: Certificate"
223 "TCP connection closed",
225 shutdown(slp->s, SHUT_WR);
236 rpctls_verbose_out(const char *fmt, ...)
240 if (rpctls_verbose) {
242 if (rpctls_debug_level == 0)
243 vsyslog(LOG_INFO | LOG_DAEMON, fmt, ap);
245 vfprintf(stderr, fmt, ap);
251 * Check a IP address against any host address in the
252 * certificate. Basically getnameinfo(3) and
256 rpctls_checkhost(struct sockaddr *sad, X509 *cert, unsigned int wildcard)
258 char hostnam[NI_MAXHOST];
261 if (getnameinfo((const struct sockaddr *)sad,
262 sad->sa_len, hostnam, sizeof(hostnam),
263 NULL, 0, NI_NAMEREQD) != 0)
265 rpctls_verbose_out("rpctls_checkhost: DNS %s\n",
267 ret = X509_check_host(cert, hostnam, strlen(hostnam),
273 * Get the peer's IP address.
276 rpctls_gethost(int s, struct sockaddr *sad, char *hostip, size_t hostlen)
281 slen = sizeof(struct sockaddr_storage);
282 if (getpeername(s, sad, &slen) < 0)
285 if (getnameinfo((const struct sockaddr *)sad,
286 sad->sa_len, hostip, hostlen,
287 NULL, 0, NI_NUMERICHOST) == 0) {
288 rpctls_verbose_out("rpctls_gethost: %s\n",