2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
21 #include <gnutls/x509.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
36 #include "pwmd-error.h"
39 #include "util-misc.h"
40 #include "util-string.h"
46 #define WAIT_INTERVAL 50000
47 #define TEST_TIMEOUT(timeout, ret, start, rc) \
50 if (ret == GNUTLS_E_AGAIN) \
52 time_t now = time (NULL); \
53 if (timeout && now - start >= timeout) \
55 rc = gpg_error (GPG_ERR_ETIMEDOUT); \
56 ret = GNUTLS_E_TIMEDOUT; \
59 struct timeval wtv = { 0, WAIT_INTERVAL }; \
60 select (0, NULL, NULL, NULL, &wtv); \
62 if (ret != GNUTLS_E_INTERRUPTED) \
67 static gnutls_dh_params_t dh_params
;
68 static gnutls_certificate_credentials_t x509_cred
;
71 tls_fingerprint (gnutls_session_t ses
)
73 gnutls_x509_crt_t crt
;
74 const gnutls_datum_t
*cert_list
;
76 unsigned char buf
[32];
77 size_t len
= sizeof (buf
);
79 gnutls_x509_crt_init (&crt
);
82 log_write ("%s(%i): %s(): %s", __FILE__
, __LINE__
, __FUNCTION__
,
83 gnutls_strerror (GNUTLS_E_MEMORY_ERROR
));
87 cert_list
= gnutls_certificate_get_peers (ses
, &count
);
90 gnutls_x509_crt_import (crt
, &cert_list
[0], GNUTLS_X509_FMT_DER
);
91 gnutls_x509_crt_get_fingerprint (crt
, GNUTLS_DIG_SHA256
, buf
, &len
);
92 gnutls_x509_crt_deinit (crt
);
94 return len
? bin2hex (buf
, len
) : NULL
;
97 gnutls_x509_crt_deinit (crt
);
102 verify_client_certificate (unsigned status
)
107 if (status
& GNUTLS_CERT_REVOKED
)
108 log_write (_("client certificate is revoked"));
110 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
111 log_write (_("client certificate has no signer"));
113 if (status
& GNUTLS_CERT_SIGNATURE_FAILURE
)
114 log_write (_("client certificate signature verification failed"));
116 if (status
& GNUTLS_CERT_EXPIRED
)
117 log_write (_("client certificate expired"));
119 if (status
& GNUTLS_CERT_SIGNER_NOT_CA
)
120 log_write (_("client certificate signer is not from CA"));
122 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
)
123 log_write (_("client certificate has insecure algorithm"));
125 if (status
& GNUTLS_CERT_INVALID
)
126 log_write (_("client certificate is invalid"));
128 return GNUTLS_E_CERTIFICATE_ERROR
;
132 tls_init_client (int fd
, int timeout
, const char *prio
)
136 const char *prio_error
;
137 gnutls_kx_algorithm_t kx
;
139 struct tls_s
*tls
= xcalloc (1, sizeof (struct tls_s
));
143 log_write ("%s(%i): %s: %s", __FILE__
, __LINE__
, __FUNCTION__
,
148 ret
= gnutls_init (&tls
->ses
, GNUTLS_SERVER
);
149 if (ret
!= GNUTLS_E_SUCCESS
)
152 ret
= gnutls_priority_set_direct (tls
->ses
, prio
, &prio_error
);
153 if (ret
!= GNUTLS_E_SUCCESS
)
156 ret
= gnutls_credentials_set (tls
->ses
, GNUTLS_CRD_CERTIFICATE
, x509_cred
);
157 if (ret
!= GNUTLS_E_SUCCESS
)
160 gnutls_certificate_server_set_request (tls
->ses
, GNUTLS_CERT_REQUIRE
);
161 gnutls_transport_set_ptr (tls
->ses
, (gnutls_transport_ptr_t
) fd
);
162 time_t start
= time (NULL
);
165 ret
= gnutls_handshake (tls
->ses
);
166 TEST_TIMEOUT (timeout
, ret
, start
, rc
);
170 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
172 if (ret
!= GNUTLS_E_SUCCESS
)
175 ret
= gnutls_certificate_verify_peers2 (tls
->ses
, &status
);
179 kx
= gnutls_kx_get (tls
->ses
);
180 tls
->fp
= tls_fingerprint (tls
->ses
);
181 log_write ("PROTO=%s CIPHER=%s MAC=%s KX=%s(%d) FP=%s",
182 gnutls_protocol_get_name (gnutls_protocol_get_version
184 gnutls_cipher_get_name (gnutls_cipher_get (tls
->ses
)),
185 gnutls_mac_get_name (gnutls_mac_get (tls
->ses
)),
186 gnutls_kx_get_name (kx
), gnutls_dh_get_prime_bits (tls
->ses
),
187 tls
->fp
? tls
->fp
: "N/A");
188 ret
= verify_client_certificate (status
);
195 log_write ("%s", rc
? pwmd_strerror(rc
) : gnutls_strerror (ret
));
196 gnutls_deinit (tls
->ses
);
203 tls_log (int level
, const char *msg
)
205 log_write ("TLS: %i: %s", level
, msg
);
209 tls_audit_log (gnutls_session_t s
, const char *msg
)
212 log_write ("TLS: %s", msg
);
216 tls_read_hook (assuan_context_t ctx
, assuan_fd_t fd
, void *data
, size_t len
)
218 struct client_s
*client
= assuan_get_pointer (ctx
);
219 struct tls_s
*tls
= client
->thd
->tls
;
220 time_t start
= time (NULL
);
226 struct timeval tv
= { 0, 0 };
232 n
= select (client
->thd
->fd
+1, &fds
, NULL
, NULL
, &tv
);
233 if (n
== 0 && tls
->nl
)
237 memcpy (data
, c
, sizeof(c
));
242 ret
= gnutls_record_recv (tls
->ses
, data
, len
);
243 if (ret
== GNUTLS_E_REHANDSHAKE
)
245 tls
->rehandshake
= 0;
249 ret
= gnutls_handshake (tls
->ses
);
250 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
254 while (ret
< 0 && gnutls_error_is_fatal (ret
) == 0);
258 gnutls_kx_algorithm_t kx
;
263 memcpy (data
, c
, sizeof (c
));
267 ret
= GNUTLS_E_AGAIN
;
269 kx
= gnutls_kx_get (tls
->ses
);
270 log_write ("PROTO=%s CIPHER=%s MAC=%s KX=%s(%d)",
271 gnutls_protocol_get_name (gnutls_protocol_get_version
273 gnutls_cipher_get_name (gnutls_cipher_get (tls
->ses
)),
274 gnutls_mac_get_name (gnutls_mac_get (tls
->ses
)),
275 gnutls_kx_get_name (kx
), gnutls_dh_get_prime_bits (tls
->ses
));
282 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
286 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
290 log_write ("TLS: %s", gnutls_strerror (ret
));
294 tls
->nl
= ((char *)data
)[ret
-1] == '\n';
300 tls_write_hook (assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
303 struct client_s
*client
= assuan_get_pointer (ctx
);
305 time_t start
= time (NULL
);
315 ret
= gnutls_record_send (client
->thd
->tls
->ses
, data
, len
);
316 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
320 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
323 log_write ("TLS: %s", gnutls_strerror (ret
));
333 gpg_error_t rc
= GPG_ERR_GENERAL
;
334 static pthread_mutex_t tls_mutex
= PTHREAD_MUTEX_INITIALIZER
;
339 MUTEX_LOCK(&tls_mutex
);
340 pthread_cleanup_push (release_mutex_cb
, &tls_mutex
);
341 tls_deinit_params ();
342 n
= gnutls_certificate_allocate_credentials (&x509_cred
);
343 if (n
!= GNUTLS_E_SUCCESS
)
345 log_write ("%s", gnutls_strerror (n
));
350 tmp
= str_asprintf ("%s/ca-cert.pem", homedir
);
354 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
358 n
= gnutls_certificate_set_x509_trust_file (x509_cred
, tmp
,
359 GNUTLS_X509_FMT_PEM
);
362 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
368 tmp
= str_asprintf ("%s/server-cert.pem", homedir
);
372 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
376 tmp2
= str_asprintf ("%s/server-key.pem", homedir
);
381 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
385 n
= gnutls_certificate_set_x509_key_file (x509_cred
, tmp
, tmp2
,
386 GNUTLS_X509_FMT_PEM
);
389 if (n
!= GNUTLS_E_SUCCESS
)
391 log_write ("%s", gnutls_strerror (n
));
395 tmp
= config_get_string ("global", "tls_dh_params_file");
398 tmp2
= expand_homedir (tmp
);
401 log_write ("%s: %s", tmp
, pwmd_strerror (GPG_ERR_ENOMEM
));
409 n
= gnutls_dh_params_init (&dh_params
);
414 n
= gnutls_load_file (tmp
, &data
);
417 n
= gnutls_dh_params_import_pkcs3 (dh_params
, &data
,
418 GNUTLS_X509_FMT_PEM
);
420 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
422 wipememory (data
.data
, 0, data
.size
);
427 if (n
!= GNUTLS_E_SUCCESS
)
429 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
440 tls_deinit_params ();
442 pthread_cleanup_pop (1);
451 gnutls_dh_params_deinit (dh_params
);
457 gnutls_certificate_free_credentials (x509_cred
);
463 do_tls_validate_access (struct client_s
*client
, const char *section
)
465 char **access
= config_get_list (section
, "allowed");
469 if (!access
|| !*access
)
470 return GPG_ERR_EACCES
;
472 for (p
= access
; p
&& *p
; p
++)
477 if (*fp
&& *fp
== '+' && *(fp
+ 1) == 0) // allow all connections
483 if (*fp
== '!' || *fp
== '-')
494 if (!strcasecmp (client
->thd
->tls
->fp
, fp
))
495 allowed
= not ? 0 : 1;
499 return allowed
? 0 : GPG_ERR_EACCES
;
503 tls_validate_access (struct client_s
*client
, const char *filename
)
505 gpg_error_t rc
= do_tls_validate_access (client
, "global");
508 rc
= do_tls_validate_access (client
, filename
);
518 MUTEX_LOCK (&cn_mutex
);
519 t
= slist_length (cn_thread_list
);
520 for (i
= 0; i
< t
; i
++)
522 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
524 if (thd
->remote
&& thd
->tls
)
525 thd
->tls
->rehandshake
= 1;
527 MUTEX_UNLOCK (&cn_mutex
);
531 start_stop_tls_with_protocol (int ipv6
, int term
)
533 struct addrinfo hints
, *servinfo
, *p
;
534 int port
= config_get_integer ("global", "tcp_port");
537 int *fd
= ipv6
? &tls6_fd
: &tls_fd
;
539 if (term
|| config_get_boolean ("global", "enable_tcp") == 0)
543 shutdown (tls6_fd
, SHUT_RDWR
);
550 shutdown (tls_fd
, SHUT_RDWR
);
558 memset (&hints
, 0, sizeof (hints
));
559 hints
.ai_family
= ipv6
? AF_INET6
: AF_INET
;
560 hints
.ai_socktype
= SOCK_STREAM
;
561 hints
.ai_flags
= AI_PASSIVE
;
562 snprintf (buf
, sizeof (buf
), "%i", port
);
564 if ((n
= getaddrinfo (NULL
, buf
, &hints
, &servinfo
)) == -1)
566 log_write ("getaddrinfo(): %s", gai_strerror (n
));
570 for (n
= 0, p
= servinfo
; p
!= NULL
; p
= p
->ai_next
)
574 if ((ipv6
&& p
->ai_family
!= AF_INET6
)
575 || (!ipv6
&& p
->ai_family
!= AF_INET
))
578 if ((*fd
= socket (p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
)) == -1)
580 log_write ("socket(): %s", strerror (errno
));
584 if (setsockopt (*fd
, SOL_SOCKET
, SO_REUSEADDR
, &r
, sizeof (int)) == -1)
586 log_write ("setsockopt(): %s",
587 pwmd_strerror (gpg_error_from_errno (errno
)));
588 freeaddrinfo (servinfo
);
592 if (bind (*fd
, p
->ai_addr
, p
->ai_addrlen
) == -1)
595 log_write ("bind(): %s",
596 pwmd_strerror (gpg_error_from_errno (errno
)));
604 freeaddrinfo (servinfo
);
609 #if HAVE_DECL_SO_BINDTODEVICE != 0
610 char *tmp
= config_get_string ("global", "tcp_interface");
611 if (tmp
&& setsockopt (*fd
, SOL_SOCKET
, SO_BINDTODEVICE
, tmp
,
614 log_write ("setsockopt(): %s",
615 pwmd_strerror (gpg_error_from_errno (errno
)));
623 if (listen (*fd
, 128) == -1)
625 log_write ("listen(): %s", strerror (errno
));
632 start_stop_tls_with_protocol (0, 1);
645 tls_start_stop (int term
)
647 char *s
= config_get_string ("global", "tcp_bind");
653 if (!strcmp (s
, "any"))
655 b
= start_stop_tls_with_protocol (0, term
);
657 b
= start_stop_tls_with_protocol (1, term
);
659 else if (!strcmp (s
, "ipv4"))
660 b
= start_stop_tls_with_protocol (0, term
);
661 else if (!strcmp (s
, "ipv6"))
662 b
= start_stop_tls_with_protocol (1, term
);
669 gpg_error_t rc
= tls_init_params ();
672 start_stop_tls_with_protocol (0, 1);