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/>.
25 #include <gnutls/x509.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
35 #include "pwmd-error.h"
38 #include "util-misc.h"
39 #include "util-string.h"
45 #define WAIT_INTERVAL 50000
46 #define TEST_TIMEOUT(timeout, ret, start, rc) \
49 if (ret == GNUTLS_E_AGAIN) \
51 time_t now = time (NULL); \
52 if (timeout && now - start >= timeout) \
54 rc = gpg_error (GPG_ERR_ETIMEDOUT); \
55 ret = GNUTLS_E_TIMEDOUT; \
58 struct timeval wtv = { 0, WAIT_INTERVAL }; \
59 select (0, NULL, NULL, NULL, &wtv); \
61 if (ret != GNUTLS_E_INTERRUPTED) \
66 static gnutls_dh_params_t dh_params
;
67 static gnutls_certificate_credentials_t x509_cred
;
70 tls_fingerprint (gnutls_session_t ses
)
72 gnutls_x509_crt_t crt
;
73 const gnutls_datum_t
*cert_list
;
75 unsigned char buf
[32];
76 size_t len
= sizeof (buf
);
78 gnutls_x509_crt_init (&crt
);
81 log_write ("%s(%i): %s(): %s", __FILE__
, __LINE__
, __FUNCTION__
,
82 gnutls_strerror (GNUTLS_E_MEMORY_ERROR
));
86 cert_list
= gnutls_certificate_get_peers (ses
, &count
);
89 gnutls_x509_crt_import (crt
, &cert_list
[0], GNUTLS_X509_FMT_DER
);
90 gnutls_x509_crt_get_fingerprint (crt
, GNUTLS_DIG_SHA256
, buf
, &len
);
91 gnutls_x509_crt_deinit (crt
);
93 return len
? bin2hex (buf
, len
) : NULL
;
96 gnutls_x509_crt_deinit (crt
);
101 verify_client_certificate (unsigned status
)
106 if (status
& GNUTLS_CERT_REVOKED
)
107 log_write (_("client certificate is revoked"));
109 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
110 log_write (_("client certificate has no signer"));
112 if (status
& GNUTLS_CERT_SIGNATURE_FAILURE
)
113 log_write (_("client certificate signature verification failed"));
115 if (status
& GNUTLS_CERT_EXPIRED
)
116 log_write (_("client certificate expired"));
118 if (status
& GNUTLS_CERT_SIGNER_NOT_CA
)
119 log_write (_("client certificate signer is not from CA"));
121 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
)
122 log_write (_("client certificate has insecure algorithm"));
124 if (status
& GNUTLS_CERT_INVALID
)
125 log_write (_("client certificate is invalid"));
127 return GNUTLS_E_CERTIFICATE_ERROR
;
131 tls_init_client (int fd
, int timeout
, const char *prio
)
135 const char *prio_error
;
136 gnutls_kx_algorithm_t kx
;
138 struct tls_s
*tls
= xcalloc (1, sizeof (struct tls_s
));
142 log_write ("%s(%i): %s: %s", __FILE__
, __LINE__
, __FUNCTION__
,
147 ret
= gnutls_init (&tls
->ses
, GNUTLS_SERVER
);
148 if (ret
!= GNUTLS_E_SUCCESS
)
151 ret
= gnutls_priority_set_direct (tls
->ses
, prio
, &prio_error
);
152 if (ret
!= GNUTLS_E_SUCCESS
)
155 ret
= gnutls_credentials_set (tls
->ses
, GNUTLS_CRD_CERTIFICATE
, x509_cred
);
156 if (ret
!= GNUTLS_E_SUCCESS
)
159 gnutls_certificate_server_set_request (tls
->ses
, GNUTLS_CERT_REQUIRE
);
160 gnutls_transport_set_ptr (tls
->ses
, (gnutls_transport_ptr_t
) fd
);
161 time_t start
= time (NULL
);
164 ret
= gnutls_handshake (tls
->ses
);
165 TEST_TIMEOUT (timeout
, ret
, start
, rc
);
169 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
171 if (ret
!= GNUTLS_E_SUCCESS
)
174 ret
= gnutls_certificate_verify_peers2 (tls
->ses
, &status
);
178 kx
= gnutls_kx_get (tls
->ses
);
179 tls
->fp
= tls_fingerprint (tls
->ses
);
180 log_write ("PROTO=%s CIPHER=%s MAC=%s KX=%s(%d) FP=%s",
181 gnutls_protocol_get_name (gnutls_protocol_get_version
183 gnutls_cipher_get_name (gnutls_cipher_get (tls
->ses
)),
184 gnutls_mac_get_name (gnutls_mac_get (tls
->ses
)),
185 gnutls_kx_get_name (kx
), gnutls_dh_get_prime_bits (tls
->ses
),
186 tls
->fp
? tls
->fp
: "N/A");
187 ret
= verify_client_certificate (status
);
194 log_write ("%s", rc
? pwmd_strerror(rc
) : gnutls_strerror (ret
));
195 gnutls_deinit (tls
->ses
);
202 tls_log (int level
, const char *msg
)
204 log_write ("TLS: %i: %s", level
, msg
);
208 tls_audit_log (gnutls_session_t s
, const char *msg
)
211 log_write ("TLS: %s", msg
);
215 tls_read_hook (assuan_context_t ctx
, assuan_fd_t fd
, void *data
, size_t len
)
217 struct client_s
*client
= assuan_get_pointer (ctx
);
218 struct tls_s
*tls
= client
->thd
->tls
;
219 time_t start
= time (NULL
);
225 struct timeval tv
= { 0, 0 };
231 n
= select (client
->thd
->fd
+1, &fds
, NULL
, NULL
, &tv
);
232 if (n
== 0 && tls
->nl
)
236 memcpy (data
, c
, sizeof(c
));
241 ret
= gnutls_record_recv (tls
->ses
, data
, len
);
242 if (ret
== GNUTLS_E_REHANDSHAKE
)
244 tls
->rehandshake
= 0;
248 ret
= gnutls_handshake (tls
->ses
);
249 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
253 while (ret
< 0 && gnutls_error_is_fatal (ret
) == 0);
257 gnutls_kx_algorithm_t kx
;
262 memcpy (data
, c
, sizeof (c
));
266 ret
= GNUTLS_E_AGAIN
;
268 kx
= gnutls_kx_get (tls
->ses
);
269 log_write ("PROTO=%s CIPHER=%s MAC=%s KX=%s(%d)",
270 gnutls_protocol_get_name (gnutls_protocol_get_version
272 gnutls_cipher_get_name (gnutls_cipher_get (tls
->ses
)),
273 gnutls_mac_get_name (gnutls_mac_get (tls
->ses
)),
274 gnutls_kx_get_name (kx
), gnutls_dh_get_prime_bits (tls
->ses
));
281 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
285 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
289 log_write ("TLS: %s", gnutls_strerror (ret
));
293 tls
->nl
= ((char *)data
)[ret
-1] == '\n';
299 tls_write_hook (assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
302 struct client_s
*client
= assuan_get_pointer (ctx
);
304 time_t start
= time (NULL
);
314 ret
= gnutls_record_send (client
->thd
->tls
->ses
, data
, len
);
315 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
319 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
322 log_write ("TLS: %s", gnutls_strerror (ret
));
332 gpg_error_t rc
= GPG_ERR_GENERAL
;
333 static pthread_mutex_t tls_mutex
= PTHREAD_MUTEX_INITIALIZER
;
338 MUTEX_LOCK(&tls_mutex
);
339 pthread_cleanup_push (release_mutex_cb
, &tls_mutex
);
340 tls_deinit_params ();
341 n
= gnutls_certificate_allocate_credentials (&x509_cred
);
342 if (n
!= GNUTLS_E_SUCCESS
)
344 log_write ("%s", gnutls_strerror (n
));
349 tmp
= str_asprintf ("%s/ca-cert.pem", homedir
);
353 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
357 n
= gnutls_certificate_set_x509_trust_file (x509_cred
, tmp
,
358 GNUTLS_X509_FMT_PEM
);
361 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
367 tmp
= str_asprintf ("%s/server-cert.pem", homedir
);
371 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
375 tmp2
= str_asprintf ("%s/server-key.pem", homedir
);
380 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
384 n
= gnutls_certificate_set_x509_key_file (x509_cred
, tmp
, tmp2
,
385 GNUTLS_X509_FMT_PEM
);
388 if (n
!= GNUTLS_E_SUCCESS
)
390 log_write ("%s", gnutls_strerror (n
));
394 tmp
= config_get_string ("global", "tls_dh_params_file");
397 tmp2
= expand_homedir (tmp
);
400 log_write ("%s: %s", tmp
, pwmd_strerror (GPG_ERR_ENOMEM
));
408 n
= gnutls_dh_params_init (&dh_params
);
413 n
= gnutls_load_file (tmp
, &data
);
416 n
= gnutls_dh_params_import_pkcs3 (dh_params
, &data
,
417 GNUTLS_X509_FMT_PEM
);
419 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
421 wipememory (data
.data
, 0, data
.size
);
426 if (n
!= GNUTLS_E_SUCCESS
)
428 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
439 tls_deinit_params ();
441 pthread_cleanup_pop (1);
450 gnutls_dh_params_deinit (dh_params
);
456 gnutls_certificate_free_credentials (x509_cred
);
462 do_tls_validate_access (struct client_s
*client
, const char *section
)
464 char **access
= config_get_list (section
, "allowed");
468 if (!access
|| !*access
)
469 return GPG_ERR_EACCES
;
471 for (p
= access
; p
&& *p
; p
++)
476 if (*fp
&& *fp
== '+' && *(fp
+ 1) == 0) // allow all connections
482 if (*fp
== '!' || *fp
== '-')
493 if (!strcasecmp (client
->thd
->tls
->fp
, fp
))
494 allowed
= not ? 0 : 1;
498 return allowed
? 0 : GPG_ERR_EACCES
;
502 tls_validate_access (struct client_s
*client
, const char *filename
)
504 gpg_error_t rc
= do_tls_validate_access (client
, "global");
507 rc
= do_tls_validate_access (client
, filename
);
517 MUTEX_LOCK (&cn_mutex
);
518 t
= slist_length (cn_thread_list
);
519 for (i
= 0; i
< t
; i
++)
521 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
523 if (thd
->remote
&& thd
->tls
)
524 thd
->tls
->rehandshake
= 1;
526 MUTEX_UNLOCK (&cn_mutex
);
530 start_stop_tls_with_protocol (int ipv6
, int term
)
532 struct addrinfo hints
, *servinfo
, *p
;
533 int port
= config_get_integer ("global", "tcp_port");
536 int *fd
= ipv6
? &tls6_fd
: &tls_fd
;
538 if (term
|| config_get_boolean ("global", "enable_tcp") == 0)
542 shutdown (tls6_fd
, SHUT_RDWR
);
549 shutdown (tls_fd
, SHUT_RDWR
);
557 memset (&hints
, 0, sizeof (hints
));
558 hints
.ai_family
= ipv6
? AF_INET6
: AF_INET
;
559 hints
.ai_socktype
= SOCK_STREAM
;
560 hints
.ai_flags
= AI_PASSIVE
;
561 snprintf (buf
, sizeof (buf
), "%i", port
);
563 if ((n
= getaddrinfo (NULL
, buf
, &hints
, &servinfo
)) == -1)
565 log_write ("getaddrinfo(): %s", gai_strerror (n
));
569 for (n
= 0, p
= servinfo
; p
!= NULL
; p
= p
->ai_next
)
573 if ((ipv6
&& p
->ai_family
!= AF_INET6
)
574 || (!ipv6
&& p
->ai_family
!= AF_INET
))
577 if ((*fd
= socket (p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
)) == -1)
579 log_write ("socket(): %s", strerror (errno
));
583 if (setsockopt (*fd
, SOL_SOCKET
, SO_REUSEADDR
, &r
, sizeof (int)) == -1)
585 log_write ("setsockopt(): %s",
586 pwmd_strerror (gpg_error_from_errno (errno
)));
587 freeaddrinfo (servinfo
);
591 if (bind (*fd
, p
->ai_addr
, p
->ai_addrlen
) == -1)
594 log_write ("bind(): %s",
595 pwmd_strerror (gpg_error_from_errno (errno
)));
603 freeaddrinfo (servinfo
);
608 #if HAVE_DECL_SO_BINDTODEVICE != 0
609 char *tmp
= config_get_string ("global", "tcp_interface");
610 if (tmp
&& setsockopt (*fd
, SOL_SOCKET
, SO_BINDTODEVICE
, tmp
,
613 log_write ("setsockopt(): %s",
614 pwmd_strerror (gpg_error_from_errno (errno
)));
622 if (listen (*fd
, 128) == -1)
624 log_write ("listen(): %s", strerror (errno
));
631 start_stop_tls_with_protocol (0, 1);
644 tls_start_stop (int term
)
646 char *s
= config_get_string ("global", "tcp_bind");
652 if (!strcmp (s
, "any"))
654 b
= start_stop_tls_with_protocol (0, term
);
656 b
= start_stop_tls_with_protocol (1, term
);
658 else if (!strcmp (s
, "ipv4"))
659 b
= start_stop_tls_with_protocol (0, term
);
660 else if (!strcmp (s
, "ipv6"))
661 b
= start_stop_tls_with_protocol (1, term
);
668 gpg_error_t rc
= tls_init_params ();
671 start_stop_tls_with_protocol (0, 1);