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>
33 #include "pwmd-error.h"
36 #include "util-misc.h"
37 #include "util-string.h"
42 #define WAIT_INTERVAL 50000
43 #define TEST_TIMEOUT(timeout, ret, start, rc) \
46 if (ret == GNUTLS_E_AGAIN) \
48 time_t now = time (NULL); \
49 if (timeout && now - start >= timeout) \
51 rc = gpg_error (GPG_ERR_ETIMEDOUT); \
52 ret = GNUTLS_E_TIMEDOUT; \
55 struct timeval tv = { 0, WAIT_INTERVAL }; \
56 select (0, NULL, NULL, NULL, &tv); \
58 if (ret != GNUTLS_E_INTERRUPTED) \
63 static gnutls_dh_params_t dh_params
;
64 static gnutls_certificate_credentials_t x509_cred
;
67 tls_fingerprint (gnutls_session_t ses
)
69 gnutls_x509_crt_t crt
;
70 const gnutls_datum_t
*cert_list
;
72 unsigned char buf
[32];
73 size_t len
= sizeof (buf
);
75 gnutls_x509_crt_init (&crt
);
78 log_write ("%s(%i): %s(): %s", __FILE__
, __LINE__
, __FUNCTION__
,
79 gnutls_strerror (GNUTLS_E_MEMORY_ERROR
));
83 cert_list
= gnutls_certificate_get_peers (ses
, &count
);
86 gnutls_x509_crt_import (crt
, &cert_list
[0], GNUTLS_X509_FMT_DER
);
87 gnutls_x509_crt_get_fingerprint (crt
, GNUTLS_DIG_SHA256
, buf
, &len
);
88 gnutls_x509_crt_deinit (crt
);
90 return len
? bin2hex (buf
, len
) : NULL
;
93 gnutls_x509_crt_deinit (crt
);
98 verify_client_certificate (unsigned status
)
103 if (status
& GNUTLS_CERT_REVOKED
)
104 log_write (_("client certificate is revoked"));
106 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
107 log_write (_("client certificate has no signer"));
109 if (status
& GNUTLS_CERT_SIGNATURE_FAILURE
)
110 log_write (_("client certificate signature verification failed"));
112 if (status
& GNUTLS_CERT_EXPIRED
)
113 log_write (_("client certificate expired"));
115 if (status
& GNUTLS_CERT_SIGNER_NOT_CA
)
116 log_write (_("client certificate signer is not from CA"));
118 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
)
119 log_write (_("client certificate has insecure algorithm"));
121 if (status
& GNUTLS_CERT_INVALID
)
122 log_write (_("client certificate is invalid"));
124 return GNUTLS_E_CERTIFICATE_ERROR
;
128 tls_init_client (int fd
, int timeout
, const char *prio
)
132 const char *prio_error
;
133 gnutls_kx_algorithm_t kx
;
135 struct tls_s
*tls
= xcalloc (1, sizeof (struct tls_s
));
139 log_write ("%s(%i): %s: %s", __FILE__
, __LINE__
, __FUNCTION__
,
144 ret
= gnutls_init (&tls
->ses
, GNUTLS_SERVER
);
145 if (ret
!= GNUTLS_E_SUCCESS
)
148 ret
= gnutls_priority_set_direct (tls
->ses
, prio
, &prio_error
);
149 if (ret
!= GNUTLS_E_SUCCESS
)
152 ret
= gnutls_credentials_set (tls
->ses
, GNUTLS_CRD_CERTIFICATE
, x509_cred
);
153 if (ret
!= GNUTLS_E_SUCCESS
)
156 gnutls_certificate_server_set_request (tls
->ses
, GNUTLS_CERT_REQUIRE
);
157 gnutls_transport_set_ptr (tls
->ses
, (gnutls_transport_ptr_t
) fd
);
158 time_t start
= time (NULL
);
161 ret
= gnutls_handshake (tls
->ses
);
162 TEST_TIMEOUT (timeout
, ret
, start
, rc
);
166 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
168 if (ret
!= GNUTLS_E_SUCCESS
)
171 ret
= gnutls_certificate_verify_peers2 (tls
->ses
, &status
);
175 kx
= gnutls_kx_get (tls
->ses
);
176 tls
->fp
= tls_fingerprint (tls
->ses
);
177 log_write ("PROTO=%s CIPHER=%s MAC=%s KX=%s(%d) FP=%s",
178 gnutls_protocol_get_name (gnutls_protocol_get_version
180 gnutls_cipher_get_name (gnutls_cipher_get (tls
->ses
)),
181 gnutls_mac_get_name (gnutls_mac_get (tls
->ses
)),
182 gnutls_kx_get_name (kx
), gnutls_dh_get_prime_bits (tls
->ses
),
183 tls
->fp
? tls
->fp
: "N/A");
184 ret
= verify_client_certificate (status
);
191 log_write ("%s", rc
? pwmd_strerror(rc
) : gnutls_strerror (ret
));
192 gnutls_deinit (tls
->ses
);
199 tls_log (int level
, const char *msg
)
201 log_write ("TLS: %i: %s", level
, msg
);
205 tls_audit_log (gnutls_session_t s
, const char *msg
)
208 log_write ("TLS: %s", msg
);
212 tls_read_hook (assuan_context_t ctx
, assuan_fd_t fd
, void *data
, size_t len
)
214 struct client_s
*client
= assuan_get_pointer (ctx
);
215 struct tls_s
*tls
= client
->thd
->tls
;
216 time_t start
= time (NULL
);
222 struct timeval tv
= { 0, 0 };
228 n
= select (client
->thd
->fd
+1, &fds
, NULL
, NULL
, &tv
);
229 if (n
== 0 && tls
->nl
)
233 memcpy (data
, c
, sizeof(c
));
238 ret
= gnutls_record_recv (tls
->ses
, data
, len
);
239 if (ret
== GNUTLS_E_REHANDSHAKE
)
241 tls
->rehandshake
= 0;
245 ret
= gnutls_handshake (tls
->ses
);
246 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
250 while (ret
< 0 && gnutls_error_is_fatal (ret
) == 0);
254 gnutls_kx_algorithm_t kx
;
259 memcpy (data
, c
, sizeof (c
));
263 ret
= GNUTLS_E_AGAIN
;
265 kx
= gnutls_kx_get (tls
->ses
);
266 log_write ("PROTO=%s CIPHER=%s MAC=%s KX=%s(%d)",
267 gnutls_protocol_get_name (gnutls_protocol_get_version
269 gnutls_cipher_get_name (gnutls_cipher_get (tls
->ses
)),
270 gnutls_mac_get_name (gnutls_mac_get (tls
->ses
)),
271 gnutls_kx_get_name (kx
), gnutls_dh_get_prime_bits (tls
->ses
));
278 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
282 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
286 log_write ("TLS: %s", gnutls_strerror (ret
));
290 tls
->nl
= ((char *)data
)[ret
-1] == '\n';
296 tls_write_hook (assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
299 struct client_s
*client
= assuan_get_pointer (ctx
);
301 time_t start
= time (NULL
);
307 ret
= gnutls_record_send (client
->thd
->tls
->ses
, data
, len
);
308 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
312 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
315 log_write ("TLS: %s", gnutls_strerror (ret
));
325 gpg_error_t rc
= GPG_ERR_GENERAL
;
326 static pthread_mutex_t tls_mutex
= PTHREAD_MUTEX_INITIALIZER
;
331 MUTEX_LOCK(&tls_mutex
);
332 pthread_cleanup_push (release_mutex_cb
, &tls_mutex
);
333 tls_deinit_params ();
334 n
= gnutls_certificate_allocate_credentials (&x509_cred
);
335 if (n
!= GNUTLS_E_SUCCESS
)
337 log_write ("%s", gnutls_strerror (n
));
342 tmp
= str_asprintf ("%s/ca-cert.pem", homedir
);
346 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
350 n
= gnutls_certificate_set_x509_trust_file (x509_cred
, tmp
,
351 GNUTLS_X509_FMT_PEM
);
354 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
360 tmp
= str_asprintf ("%s/server-cert.pem", homedir
);
364 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
368 tmp2
= str_asprintf ("%s/server-key.pem", homedir
);
373 log_write ("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror (rc
));
377 n
= gnutls_certificate_set_x509_key_file (x509_cred
, tmp
, tmp2
,
378 GNUTLS_X509_FMT_PEM
);
381 if (n
!= GNUTLS_E_SUCCESS
)
383 log_write ("%s", gnutls_strerror (n
));
387 tmp
= config_get_string ("global", "tls_dh_params_file");
390 tmp2
= expand_homedir (tmp
);
394 log_write ("%s: %s", tmp
, pwmd_strerror (GPG_ERR_ENOMEM
));
401 n
= gnutls_dh_params_init (&dh_params
);
406 n
= gnutls_load_file (tmp
, &data
);
409 n
= gnutls_dh_params_import_pkcs3 (dh_params
, &data
,
410 GNUTLS_X509_FMT_PEM
);
412 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
414 wipememory (data
.data
, 0, data
.size
);
419 if (n
!= GNUTLS_E_SUCCESS
)
421 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
432 tls_deinit_params ();
434 pthread_cleanup_pop (1);
443 gnutls_dh_params_deinit (dh_params
);
449 gnutls_certificate_free_credentials (x509_cred
);
455 do_tls_validate_access (struct client_s
*client
, const char *section
)
457 char **access
= config_get_list (section
, "allowed");
461 if (!access
|| !*access
)
462 return GPG_ERR_EACCES
;
464 for (p
= access
; p
&& *p
; p
++)
469 if (*fp
&& *fp
== '+' && *(fp
+ 1) == 0) // allow all connections
475 if (*fp
== '!' || *fp
== '-')
486 if (!strcasecmp (client
->thd
->tls
->fp
, fp
))
487 allowed
= not ? 0 : 1;
491 return allowed
? 0 : GPG_ERR_EACCES
;
495 tls_validate_access (struct client_s
*client
, const char *filename
)
497 gpg_error_t rc
= do_tls_validate_access (client
, "global");
500 rc
= do_tls_validate_access (client
, filename
);
510 MUTEX_LOCK (&cn_mutex
);
511 t
= slist_length (cn_thread_list
);
512 for (i
= 0; i
< t
; i
++)
514 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
516 if (thd
->remote
&& thd
->tls
)
517 thd
->tls
->rehandshake
= 1;
519 MUTEX_UNLOCK (&cn_mutex
);