2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
20 #include <gnutls/x509.h>
32 #include "pwmd-error.h"
34 #include "util-misc.h"
35 #include "util-string.h"
40 #define WAIT_INTERVAL 50000
41 #define TEST_TIMEOUT(timeout, ret, start, rc) \
43 if (ret == GNUTLS_E_AGAIN) \
45 time_t now = time (NULL); \
46 if (timeout && now - start >= timeout) \
48 rc = gpg_error (GPG_ERR_ETIMEDOUT); \
51 struct timeval tv = { 0, WAIT_INTERVAL }; \
52 select (0, NULL, NULL, NULL, &tv); \
54 if (ret != GNUTLS_E_INTERRUPTED) \
60 tls_fingerprint (gnutls_session_t ses
)
62 gnutls_x509_crt_t crt
;
63 const gnutls_datum_t
*cert_list
;
65 unsigned char buf
[32];
66 size_t len
= sizeof (buf
);
68 gnutls_x509_crt_init (&crt
);
71 log_write ("%s(%i): %s(): %s", __FILE__
, __LINE__
, __FUNCTION__
,
72 gnutls_strerror (GNUTLS_E_MEMORY_ERROR
));
76 cert_list
= gnutls_certificate_get_peers (ses
, &count
);
79 gnutls_x509_crt_import (crt
, &cert_list
[0], GNUTLS_X509_FMT_DER
);
80 gnutls_x509_crt_get_fingerprint (crt
, GNUTLS_DIG_SHA256
, buf
, &len
);
81 gnutls_x509_crt_deinit (crt
);
83 return len
? bin2hex (buf
, len
) : NULL
;
86 gnutls_x509_crt_deinit (crt
);
91 verify_client_certificate (unsigned status
)
96 if (status
& GNUTLS_CERT_REVOKED
)
97 log_write (_("client certificate is revoked"));
99 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
100 log_write (_("client certificate has no signer"));
102 if (status
& GNUTLS_CERT_SIGNATURE_FAILURE
)
103 log_write (_("client certificate signature verification failed"));
105 if (status
& GNUTLS_CERT_EXPIRED
)
106 log_write (_("client certificate expired"));
108 if (status
& GNUTLS_CERT_SIGNER_NOT_CA
)
109 log_write (_("client certificate signer is not from CA"));
111 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
)
112 log_write (_("client certificate has insecure algorithm"));
114 if (status
& GNUTLS_CERT_INVALID
)
115 log_write (_("client certificate is invalid"));
117 return GNUTLS_E_CERTIFICATE_ERROR
;
121 tls_init (int fd
, int timeout
, const char *prio
)
123 struct tls_s
*tls
= xcalloc (1, sizeof (struct tls_s
));
126 const char *prio_error
;
127 gnutls_kx_algorithm_t kx
;
132 log_write ("%s(%i): %s: %s", __FILE__
, __LINE__
, __FUNCTION__
,
137 ret
= gnutls_init (&tls
->ses
, GNUTLS_SERVER
);
138 if (ret
!= GNUTLS_E_SUCCESS
)
141 ret
= gnutls_priority_set_direct (tls
->ses
, prio
, &prio_error
);
142 if (ret
!= GNUTLS_E_SUCCESS
)
145 ret
= gnutls_credentials_set (tls
->ses
, GNUTLS_CRD_CERTIFICATE
, x509_cred
);
146 if (ret
!= GNUTLS_E_SUCCESS
)
149 gnutls_certificate_server_set_request (tls
->ses
, GNUTLS_CERT_REQUIRE
);
150 gnutls_transport_set_ptr (tls
->ses
, (gnutls_transport_ptr_t
) fd
);
151 time_t start
= time (NULL
);
154 ret
= gnutls_handshake (tls
->ses
);
155 TEST_TIMEOUT (timeout
, ret
, start
, rc
);
159 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
161 if (ret
!= GNUTLS_E_SUCCESS
)
164 ret
= gnutls_certificate_verify_peers2 (tls
->ses
, &status
);
168 kx
= gnutls_kx_get (tls
->ses
);
169 tls
->fp
= tls_fingerprint (tls
->ses
);
170 log_write ("PROTO=%s CIPHER=%s MAC=%s KX=%s(%d) FP=%s",
171 gnutls_protocol_get_name (gnutls_protocol_get_version
173 gnutls_cipher_get_name (gnutls_cipher_get (tls
->ses
)),
174 gnutls_mac_get_name (gnutls_mac_get (tls
->ses
)),
175 gnutls_kx_get_name (kx
), gnutls_dh_get_prime_bits (tls
->ses
),
176 tls
->fp
? tls
->fp
: "N/A");
177 ret
= verify_client_certificate (status
);
184 log_write ("%s", rc
? gpg_strerror(rc
) : gnutls_strerror (ret
));
185 gnutls_deinit (tls
->ses
);
192 tls_log (int level
, const char *msg
)
194 log_write ("TLS: %i: %s", level
, msg
);
198 tls_audit_log (gnutls_session_t s
, const char *msg
)
201 log_write ("TLS: %s", msg
);
205 tls_read_hook (assuan_context_t ctx
, assuan_fd_t fd
, void *data
, size_t len
)
207 struct client_s
*client
= assuan_get_pointer (ctx
);
208 time_t start
= time (NULL
);
216 ret
= gnutls_record_recv (client
->thd
->tls
->ses
, data
, len
);
217 if (ret
== GNUTLS_E_REHANDSHAKE
)
219 ret
= gnutls_rehandshake (client
->thd
->tls
->ses
);
220 if (ret
== GNUTLS_E_GOT_APPLICATION_DATA
)
222 else if (ret
!= GNUTLS_E_SUCCESS
)
224 log_write ("%s", gnutls_strerror (ret
));
231 ret
= gnutls_handshake (client
->thd
->tls
->ses
);
233 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
236 log_write ("%s", gnutls_strerror (ret
));
238 close (client
->thd
->fd
);
239 client
->thd
->fd
= -1;
243 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
251 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
255 close (client
->thd
->fd
);
256 client
->thd
->fd
= -1;
260 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
264 log_write ("%s", gnutls_strerror (ret
));
272 tls_write_hook (assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
275 struct client_s
*client
= assuan_get_pointer (ctx
);
277 time_t start
= time (NULL
);
283 ret
= gnutls_record_send (client
->thd
->tls
->ses
, data
, len
);
284 TEST_TIMEOUT (client
->thd
->timeout
, ret
, start
, rc
);
288 close (client
->thd
->fd
);
289 client
->thd
->fd
= -1;
293 while (ret
== GNUTLS_E_INTERRUPTED
|| ret
== GNUTLS_E_AGAIN
);
299 parse_dh_sec_level (const char *str
, gpg_error_t
*rc
)
304 return gnutls_sec_param_to_pk_bits (GNUTLS_PK_DH
, GNUTLS_SEC_PARAM_MEDIUM
);
306 if (!strcasecmp (str
, "low"))
307 return gnutls_sec_param_to_pk_bits (GNUTLS_PK_DH
, GNUTLS_SEC_PARAM_LOW
);
308 else if (!strcasecmp (str
, "medium"))
309 return gnutls_sec_param_to_pk_bits (GNUTLS_PK_DH
, GNUTLS_SEC_PARAM_MEDIUM
);
310 else if (!strcasecmp (str
, "high"))
311 return gnutls_sec_param_to_pk_bits (GNUTLS_PK_DH
, GNUTLS_SEC_PARAM_HIGH
);
312 else if (!strcasecmp (str
, "ultra"))
313 return gnutls_sec_param_to_pk_bits (GNUTLS_PK_DH
, GNUTLS_SEC_PARAM_ULTRA
);
315 *rc
= GPG_ERR_INV_VALUE
;
316 return GNUTLS_SEC_PARAM_UNKNOWN
;
320 tls_init_params (const char *dh_sec_level
)
324 gpg_error_t rc
= GPG_ERR_UNKNOWN_ERRNO
;
325 int bits
= parse_dh_sec_level (dh_sec_level
, &rc
);
330 rc
= GPG_ERR_UNKNOWN_ERRNO
;
331 n
= gnutls_certificate_allocate_credentials (&x509_cred
);
332 if (n
!= GNUTLS_E_SUCCESS
)
334 log_write ("%s", gnutls_strerror (n
));
339 tmp
= str_asprintf ("%s/ca-cert.pem", homedir
);
346 n
= gnutls_certificate_set_x509_trust_file (x509_cred
, tmp
,
347 GNUTLS_X509_FMT_PEM
);
350 log_write ("%s: %s", tmp
, gnutls_strerror (n
));
356 tmp
= str_asprintf ("%s/server-cert.pem", homedir
);
363 tmp2
= str_asprintf ("%s/server-key.pem", homedir
);
367 log_write ("%s(%i): %s", __FILE__
, __LINE__
, strerror (ENOMEM
));
371 n
= gnutls_certificate_set_x509_key_file (x509_cred
, tmp
, tmp2
,
372 GNUTLS_X509_FMT_PEM
);
375 if (n
!= GNUTLS_E_SUCCESS
)
377 log_write ("%s", gnutls_strerror (n
));
381 log_write ("%s", _("Generating key exchange parameters..."));
382 n
= gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM
, 0);
385 log_write ("%s", pwmd_strerror (n
));
389 n
= gnutls_dh_params_init (&dh_params
);
390 if (n
!= GNUTLS_E_SUCCESS
)
392 log_write ("%s", gnutls_strerror (n
));
396 n
= gnutls_dh_params_generate2 (dh_params
, bits
);
397 if (n
!= GNUTLS_E_SUCCESS
)
399 log_write ("%s", gnutls_strerror (n
));
403 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
415 gnutls_dh_params_deinit (dh_params
);
421 gnutls_certificate_free_credentials (x509_cred
);
427 do_tls_validate_access (struct client_s
*client
, const char *section
)
429 char **access
= config_get_list (section
, "allowed");
433 if (!access
|| !*access
)
434 return GPG_ERR_EACCES
;
436 for (p
= access
; p
&& *p
; p
++)
441 if (*fp
&& *fp
== '+' && *(fp
+ 1) == 0) // allow all connections
447 if (*fp
== '!' || *fp
== '-')
458 if (!strcasecmp (client
->thd
->tls
->fp
, fp
))
459 allowed
= not ? 0 : 1;
463 return allowed
? 0 : GPG_ERR_EACCES
;
467 tls_validate_access (struct client_s
*client
, const char *filename
)
469 gpg_error_t rc
= do_tls_validate_access (client
, "global");
472 rc
= do_tls_validate_access (client
, filename
);