2 Copyright (C) 2006-2018 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Pwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
23 #include <gnutls/x509.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
33 #include "pwmd-error.h"
36 #include "util-misc.h"
37 #include "util-string.h"
43 #define WAIT_INTERVAL 50000
44 #define TEST_TIMEOUT(timeout, ret, start, rc) \
47 if (ret == GNUTLS_E_AGAIN) \
49 time_t now = time (NULL); \
50 if (timeout && now - start >= timeout) \
52 rc = gpg_error (GPG_ERR_ETIMEDOUT); \
53 ret = GNUTLS_E_TIMEDOUT; \
56 struct timeval wtv = { 0, WAIT_INTERVAL }; \
57 select (0, NULL, NULL, NULL, &wtv); \
59 if (ret != GNUTLS_E_INTERRUPTED) \
64 static gnutls_dh_params_t dh_params
;
65 static gnutls_certificate_credentials_t x509_cred
;
68 tls_fingerprint (gnutls_session_t ses
)
70 gnutls_x509_crt_t crt
;
71 const gnutls_datum_t
*cert_list
;
73 unsigned char buf
[32];
74 size_t len
= sizeof (buf
);
76 gnutls_x509_crt_init (&crt
);
79 log_write ("%s(%i): %s(): %s", __FILE__
, __LINE__
, __FUNCTION__
,
80 gnutls_strerror (GNUTLS_E_MEMORY_ERROR
));
84 cert_list
= gnutls_certificate_get_peers (ses
, &count
);
87 gnutls_x509_crt_import (crt
, &cert_list
[0], GNUTLS_X509_FMT_DER
);
88 gnutls_x509_crt_get_fingerprint (crt
, GNUTLS_DIG_SHA256
, buf
, &len
);
89 gnutls_x509_crt_deinit (crt
);
91 return len
? bin2hex (buf
, len
) : NULL
;
94 gnutls_x509_crt_deinit (crt
);
99 verify_client_certificate (unsigned status
)
104 if (status
& GNUTLS_CERT_REVOKED
)
105 log_write (_("client certificate is revoked"));
107 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
108 log_write (_("client certificate has no signer"));
110 if (status
& GNUTLS_CERT_SIGNATURE_FAILURE
)
111 log_write (_("client certificate signature verification failed"));
113 if (status
& GNUTLS_CERT_EXPIRED
)
114 log_write (_("client certificate expired"));
116 if (status
& GNUTLS_CERT_SIGNER_NOT_CA
)
117 log_write (_("client certificate signer is not from CA"));
119 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
)
120 log_write (_("client certificate has insecure algorithm"));
122 if (status
& GNUTLS_CERT_INVALID
)
123 log_write (_("client certificate is invalid"));
125 return GNUTLS_E_CERTIFICATE_ERROR
;
129 tls_init_client (int fd
, int timeout
, const char *prio
)
133 const char *prio_error
;
134 gnutls_kx_algorithm_t kx
;
136 struct tls_s
*tls
= xcalloc (1, sizeof (struct tls_s
));
140 log_write ("%s(%i): %s: %s", __FILE__
, __LINE__
, __FUNCTION__
,
145 ret
= gnutls_init (&tls
->ses
, GNUTLS_SERVER
);
146 if (ret
!= GNUTLS_E_SUCCESS
)
149 ret
= gnutls_priority_set_direct (tls
->ses
, prio
, &prio_error
);
150 if (ret
!= GNUTLS_E_SUCCESS
)
153 ret
= gnutls_credentials_set (tls
->ses
, GNUTLS_CRD_CERTIFICATE
, x509_cred
);
154 if (ret
!= GNUTLS_E_SUCCESS
)
157 gnutls_certificate_server_set_request (tls
->ses
, GNUTLS_CERT_REQUIRE
);
158 gnutls_transport_set_ptr (tls
->ses
, (gnutls_transport_ptr_t
) fd
);
159 time_t start
= time (NULL
);
162 ret
= gnutls_handshake (tls
->ses
);
163 TEST_TIMEOUT (timeout
, ret
, start
, rc
);
167 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
169 if (ret
!= GNUTLS_E_SUCCESS
)
172 ret
= gnutls_certificate_verify_peers2 (tls
->ses
, &status
);
176 kx
= gnutls_kx_get (tls
->ses
);
177 tls
->fp
= tls_fingerprint (tls
->ses
);
178 log_write ("PROTO=%s CIPHER=%s MAC=%s KX=%s(%d) FP=%s",
179 gnutls_protocol_get_name (gnutls_protocol_get_version
181 gnutls_cipher_get_name (gnutls_cipher_get (tls
->ses
)),
182 gnutls_mac_get_name (gnutls_mac_get (tls
->ses
)),
183 gnutls_kx_get_name (kx
), gnutls_dh_get_prime_bits (tls
->ses
),
184 tls
->fp
? tls
->fp
: "N/A");
185 ret
= verify_client_certificate (status
);
192 log_write ("%s", rc
? pwmd_strerror(rc
) : gnutls_strerror (ret
));
193 gnutls_deinit (tls
->ses
);
200 tls_log (int level
, const char *msg
)
202 log_write ("TLS: %i: %s", level
, msg
);
206 tls_audit_log (gnutls_session_t s
, const char *msg
)
209 log_write ("TLS: %s", msg
);
213 tls_read_hook (assuan_context_t ctx
, assuan_fd_t fd
, void *data
, size_t len
)
215 struct client_s
*client
= assuan_get_pointer (ctx
);
216 struct tls_s
*tls
= client
->thd
->tls
;
217 time_t start
= time (NULL
);
223 struct timeval tv
= { 0, 0 };
229 n
= select (client
->thd
->fd
+1, &fds
, NULL
, NULL
, &tv
);
230 if (n
== 0 && tls
->nl
)
234 memcpy (data
, c
, sizeof(c
));
239 ret
= gnutls_record_recv (tls
->ses
, data
, len
);
240 if (ret
== GNUTLS_E_REHANDSHAKE
)
242 tls
->rehandshake
= 0;
246 ret
= gnutls_handshake (tls
->ses
);
247 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
251 while (ret
< 0 && gnutls_error_is_fatal (ret
) == 0);
255 gnutls_kx_algorithm_t kx
;
260 memcpy (data
, c
, sizeof (c
));
264 ret
= GNUTLS_E_AGAIN
;
266 kx
= gnutls_kx_get (tls
->ses
);
267 log_write ("PROTO=%s CIPHER=%s MAC=%s KX=%s(%d)",
268 gnutls_protocol_get_name (gnutls_protocol_get_version
270 gnutls_cipher_get_name (gnutls_cipher_get (tls
->ses
)),
271 gnutls_mac_get_name (gnutls_mac_get (tls
->ses
)),
272 gnutls_kx_get_name (kx
), gnutls_dh_get_prime_bits (tls
->ses
));
279 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
283 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
287 log_write ("TLS: %s", gnutls_strerror (ret
));
291 tls
->nl
= ((char *)data
)[ret
-1] == '\n';
297 tls_write_hook (assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
300 struct client_s
*client
= assuan_get_pointer (ctx
);
302 time_t start
= time (NULL
);
312 ret
= gnutls_record_send (client
->thd
->tls
->ses
, data
, len
);
313 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
317 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
320 log_write ("TLS: %s", gnutls_strerror (ret
));
330 gpg_error_t rc
= GPG_ERR_GENERAL
;
331 static pthread_mutex_t tls_mutex
= PTHREAD_MUTEX_INITIALIZER
;
336 MUTEX_LOCK(&tls_mutex
);
337 pthread_cleanup_push (release_mutex_cb
, &tls_mutex
);
338 tls_deinit_params ();
339 n
= gnutls_certificate_allocate_credentials (&x509_cred
);
340 if (n
!= GNUTLS_E_SUCCESS
)
342 log_write ("%s", gnutls_strerror (n
));
347 MUTEX_LOCK (&rcfile_mutex
);
348 if (config_get_boolean ("global", "tls_use_crl"))
350 MUTEX_UNLOCK (&rcfile_mutex
);
351 tmp
= str_asprintf ("%s/crl.pem", homedir
);
358 if (access (tmp
, R_OK
) == -1 && errno
== ENOENT
)
360 rc
= gpg_error_from_errno (errno
);
361 log_write ("%s: %s", tmp
, pwmd_strerror (rc
));
366 n
= gnutls_certificate_set_x509_crl_file (x509_cred
, tmp
,
367 GNUTLS_X509_FMT_PEM
);
370 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
379 MUTEX_UNLOCK (&rcfile_mutex
);
381 tmp
= str_asprintf ("%s/ca-cert.pem", homedir
);
385 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
389 n
= gnutls_certificate_set_x509_trust_file (x509_cred
, tmp
,
390 GNUTLS_X509_FMT_PEM
);
393 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
399 tmp
= str_asprintf ("%s/server-cert.pem", homedir
);
403 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
407 tmp2
= str_asprintf ("%s/server-key.pem", homedir
);
412 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
416 n
= gnutls_certificate_set_x509_key_file (x509_cred
, tmp
, tmp2
,
417 GNUTLS_X509_FMT_PEM
);
420 if (n
!= GNUTLS_E_SUCCESS
)
422 log_write ("%s", gnutls_strerror (n
));
426 tmp
= config_get_string ("global", "tls_dh_params_file");
429 tmp2
= expand_homedir (tmp
);
432 log_write ("%s: %s", tmp
, pwmd_strerror (GPG_ERR_ENOMEM
));
440 n
= gnutls_dh_params_init (&dh_params
);
445 n
= gnutls_load_file (tmp
, &data
);
448 n
= gnutls_dh_params_import_pkcs3 (dh_params
, &data
,
449 GNUTLS_X509_FMT_PEM
);
451 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
453 wipememory (data
.data
, 0, data
.size
);
458 if (n
!= GNUTLS_E_SUCCESS
)
460 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
471 tls_deinit_params ();
473 pthread_cleanup_pop (1);
482 gnutls_dh_params_deinit (dh_params
);
488 gnutls_certificate_free_credentials (x509_cred
);
494 do_tls_validate_access (struct client_s
*client
, const char *section
)
496 char **access
= config_get_list (section
, "allowed");
500 if (!access
|| !*access
)
501 return GPG_ERR_EACCES
;
503 for (p
= access
; p
&& *p
; p
++)
508 if (*fp
&& *fp
== '+' && *(fp
+ 1) == 0) // allow all connections
514 if (*fp
== '!' || *fp
== '-')
525 if (!strcasecmp (client
->thd
->tls
->fp
, fp
))
526 allowed
= not ? 0 : 1;
530 return allowed
? 0 : GPG_ERR_EACCES
;
534 tls_validate_access (struct client_s
*client
, const char *filename
)
536 gpg_error_t rc
= do_tls_validate_access (client
, "global");
539 rc
= do_tls_validate_access (client
, filename
);
549 MUTEX_LOCK (&cn_mutex
);
550 t
= slist_length (cn_thread_list
);
551 for (i
= 0; i
< t
; i
++)
553 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
555 if (thd
->remote
&& thd
->tls
)
556 thd
->tls
->rehandshake
= 1;
558 MUTEX_UNLOCK (&cn_mutex
);
562 start_stop_tls_with_protocol (int ipv6
, int term
)
564 struct addrinfo hints
, *servinfo
, *p
;
565 int port
= config_get_integer ("global", "tcp_port");
568 int *fd
= ipv6
? &tls6_fd
: &tls_fd
;
570 if (term
|| config_get_boolean ("global", "enable_tcp") == 0)
574 shutdown (tls6_fd
, SHUT_RDWR
);
581 shutdown (tls_fd
, SHUT_RDWR
);
589 memset (&hints
, 0, sizeof (hints
));
590 hints
.ai_family
= ipv6
? AF_INET6
: AF_INET
;
591 hints
.ai_socktype
= SOCK_STREAM
;
592 hints
.ai_flags
= AI_PASSIVE
;
593 snprintf (buf
, sizeof (buf
), "%i", port
);
595 if ((n
= getaddrinfo (NULL
, buf
, &hints
, &servinfo
)) == -1)
597 log_write ("getaddrinfo(): %s", gai_strerror (n
));
601 for (n
= 0, p
= servinfo
; p
!= NULL
; p
= p
->ai_next
)
605 if ((ipv6
&& p
->ai_family
!= AF_INET6
)
606 || (!ipv6
&& p
->ai_family
!= AF_INET
))
609 if ((*fd
= socket (p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
)) == -1)
611 log_write ("socket(): %s", strerror (errno
));
615 if (setsockopt (*fd
, SOL_SOCKET
, SO_REUSEADDR
, &r
, sizeof (int)) == -1)
617 log_write ("setsockopt(): %s",
618 pwmd_strerror (gpg_error_from_errno (errno
)));
619 freeaddrinfo (servinfo
);
623 if (bind (*fd
, p
->ai_addr
, p
->ai_addrlen
) == -1)
626 log_write ("bind(): port %u: %s", port
,
627 pwmd_strerror (gpg_error_from_errno (errno
)));
635 freeaddrinfo (servinfo
);
640 #if HAVE_DECL_SO_BINDTODEVICE != 0
641 char *tmp
= config_get_string ("global", "tcp_interface");
642 if (tmp
&& setsockopt (*fd
, SOL_SOCKET
, SO_BINDTODEVICE
, tmp
,
645 log_write ("setsockopt(): %s",
646 pwmd_strerror (gpg_error_from_errno (errno
)));
654 if (listen (*fd
, 128) == -1)
656 log_write ("listen(): %s", strerror (errno
));
663 start_stop_tls_with_protocol (0, 1);
676 tls_start_stop (int term
)
678 char *s
= config_get_string ("global", "tcp_bind");
684 if (!strcmp (s
, "any"))
686 b
= start_stop_tls_with_protocol (0, term
);
688 b
= start_stop_tls_with_protocol (1, term
);
690 else if (!strcmp (s
, "ipv4"))
691 b
= start_stop_tls_with_protocol (0, term
);
692 else if (!strcmp (s
, "ipv6"))
693 b
= start_stop_tls_with_protocol (1, term
);
700 gpg_error_t rc
= tls_init_params ();
703 start_stop_tls_with_protocol (0, 1);