2 Copyright (C) 2012, 2013, 2014, 2015
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of libpwmd.
7 Libpwmd 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 Libpwmd 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 Libpwmd. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
30 #include <sys/socket.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) \
62 struct tls_s
*tls
= pwm
->tcp
->tls
;
69 pwmd_free (tls
->cert
);
71 pwmd_free (tls
->priority
);
72 pwmd_free (tls
->server_fp
);
76 time_t start
= time (NULL
);
82 ret
= gnutls_bye (tls
->session
, GNUTLS_SHUT_WR
);
83 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
87 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
89 gnutls_deinit (tls
->session
);
91 else if (pwm
->fd
!= -1)
98 gnutls_certificate_free_credentials (tls
->x509
);
103 pwm
->tls_error
= ret
;
107 read_hook_tls (pwm_t
*pwm
, assuan_fd_t fd
, void *data
, size_t len
)
109 struct tls_s
*tls
= pwm
->tcp
->tls
;
115 time_t start
= time (NULL
);
122 ret
= gnutls_record_recv (tls
->session
, data
, len
);
123 if (ret
== GNUTLS_E_REHANDSHAKE
)
125 ret
= gnutls_rehandshake (tls
->session
);
126 if (ret
== GNUTLS_E_GOT_APPLICATION_DATA
)
128 else if (ret
!= GNUTLS_E_SUCCESS
)
130 pwm
->tls_error
= ret
;
137 ret
= gnutls_handshake (tls
->session
);
139 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
148 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
150 pwm
->tls_error
= ret
;
157 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
166 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
169 pwm
->tls_error
= ret
;
173 else if (ret
== GNUTLS_E_PREMATURE_TERMINATION
)
176 return ret
< 0 ? -1 : ret
;
180 write_hook_tls (pwm_t
*pwm
, assuan_fd_t fd
, const void *data
, size_t len
)
182 struct tls_s
*tls
= pwm
->tcp
->tls
;
184 /* This is probably the BYE command after a previous call timed
185 * out. If not done, the time(2) call seems to hang.*/
190 time_t start
= time (NULL
);
196 ret
= gnutls_record_send (tls
->session
, data
, len
);
197 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
206 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
209 pwm
->tls_error
= ret
;
213 else if (ret
== GNUTLS_E_PREMATURE_TERMINATION
)
216 return ret
< 0 ? -1 : ret
;
220 tls_fingerprint (pwm_t
*pwm
, char **result
)
222 gnutls_session_t ses
= pwm
->tcp
->tls
->session
;
223 gnutls_x509_crt_t crt
;
224 const gnutls_datum_t
*cert_list
;
226 unsigned char buf
[32];
227 size_t len
= sizeof (buf
);
230 gnutls_x509_crt_init (&crt
);
232 return GPG_ERR_ENOMEM
;
234 cert_list
= gnutls_certificate_get_peers (ses
, &count
);
237 pwm
->tls_error
= gnutls_x509_crt_import (crt
, &cert_list
[0],
238 GNUTLS_X509_FMT_DER
);
240 gnutls_x509_crt_get_fingerprint (crt
, GNUTLS_DIG_SHA256
, buf
, &len
);
243 gnutls_x509_crt_deinit (crt
);
245 *result
= bin2hex (buf
, len
);
247 return !pwm
->tls_error
? 0 : GPG_ERR_ENOMEM
;
251 verify_certificate (pwm_t
*pwm
)
254 const gnutls_datum_t
*cert_list
;
255 unsigned int cert_list_size
;
257 gnutls_x509_crt_t cert
;
260 i
= gnutls_certificate_verify_peers2 (pwm
->tcp
->tls
->session
, &status
);
264 return GPG_ERR_BAD_CERT
;
267 if (status
& GNUTLS_CERT_INVALID
)
268 return GPG_ERR_BAD_CERT
;
270 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
271 return GPG_ERR_BAD_SIGNATURE
;
273 if (status
& GNUTLS_CERT_REVOKED
)
274 return GPG_ERR_CERT_REVOKED
;
276 if (gnutls_certificate_type_get (pwm
->tcp
->tls
->session
) != GNUTLS_CRT_X509
)
277 return GPG_ERR_UNSUPPORTED_CERT
;
279 if (gnutls_x509_crt_init (&cert
) < 0)
280 return gpg_error_from_errno (ENOMEM
);
282 cert_list
= gnutls_certificate_get_peers (pwm
->tcp
->tls
->session
,
286 rc
= GPG_ERR_MISSING_CERT
;
290 for (i
= 0; i
< cert_list_size
; i
++)
292 pwm
->tls_error
= gnutls_x509_crt_import (cert
, &cert_list
[i
],
293 GNUTLS_X509_FMT_DER
);
294 if (pwm
->tls_error
< 0)
296 rc
= GPG_ERR_BAD_CERT_CHAIN
;
300 if (gnutls_x509_crt_get_expiration_time (cert
) < time (0))
302 rc
= GPG_ERR_CERT_EXPIRED
;
306 if (gnutls_x509_crt_get_activation_time (cert
) > time (0))
308 rc
= GPG_ERR_CERT_TOO_YOUNG
;
312 if (pwm
->tcp
->tls
->verify
)
314 if (!gnutls_x509_crt_check_hostname (cert
, pwm
->tcp
->host
))
316 rc
= GPG_ERR_BAD_CERT_CHAIN
;
322 if (pwm
->tcp
->tls
->server_fp
)
326 rc
= tls_fingerprint (pwm
, &result
);
329 if (!result
|| !*result
||
330 strcasecmp (result
, pwm
->tcp
->tls
->server_fp
))
331 rc
= GPG_ERR_BAD_CERT
;
338 gnutls_x509_crt_deinit (cert
);
343 tls_init (pwm_t
* pwm
)
349 ret
= gnutls_certificate_allocate_credentials (&pwm
->tcp
->tls
->x509
);
352 pwm
->tls_error
= ret
;
353 return gpg_error_from_errno (ENOMEM
);
356 /* The client certificate must be signed by the CA of the pwmd server
357 * certificate in order for the client to authenticate successfully. Man in
358 * the middle attacks are still possible if the attacker is running a pwmd
359 * that doesn't require client certificate authentication. So require the
360 * client to verify the server certificate.
362 ret
= gnutls_certificate_set_x509_trust_file (pwm
->tcp
->tls
->x509
,
364 GNUTLS_X509_FMT_PEM
);
367 rc
= GPG_ERR_INV_CERT_OBJ
;
371 ret
= gnutls_certificate_set_x509_key_file (pwm
->tcp
->tls
->x509
,
374 GNUTLS_X509_FMT_PEM
);
375 if (ret
!= GNUTLS_E_SUCCESS
)
377 pwm
->tls_error
= ret
;
378 rc
= GPG_ERR_INV_CERT_OBJ
;
382 ret
= gnutls_init (&pwm
->tcp
->tls
->session
, GNUTLS_CLIENT
);
383 if (ret
!= GNUTLS_E_SUCCESS
)
385 pwm
->tls_error
= ret
;
386 rc
= GPG_ERR_INV_CERT_OBJ
;
390 ret
= gnutls_priority_set_direct (pwm
->tcp
->tls
->session
,
391 pwm
->tcp
->tls
->priority
? pwm
->tcp
->tls
->
392 priority
: "SECURE256", &errstr
);
393 if (ret
!= GNUTLS_E_SUCCESS
)
395 pwm
->tls_error
= ret
;
396 rc
= GPG_ERR_INV_CERT_OBJ
;
400 ret
= gnutls_credentials_set (pwm
->tcp
->tls
->session
, GNUTLS_CRD_CERTIFICATE
,
401 pwm
->tcp
->tls
->x509
);
402 if (ret
!= GNUTLS_E_SUCCESS
)
404 pwm
->tls_error
= ret
;
405 rc
= GPG_ERR_INV_CERT_OBJ
;
409 gnutls_transport_set_ptr (pwm
->tcp
->tls
->session
,
410 (gnutls_transport_ptr_t
) pwm
->fd
);
411 fcntl (pwm
->fd
, F_SETFL
, O_NONBLOCK
);
412 time_t start
= time (NULL
);
415 ret
= gnutls_handshake (pwm
->tcp
->tls
->session
);
417 TEST_TIMEOUT (pwm
->tcp
->tls
->timeout
, ret
, start
, rc
);
421 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
423 if (ret
!= GNUTLS_E_SUCCESS
)
425 pwm
->tls_error
= ret
;
426 rc
= GPG_ERR_INV_CERT_OBJ
;
430 rc
= verify_certificate (pwm
);
435 /* Keep the original error since it maybe overwritten during TLS
437 ret
= pwm
->tls_error
;
439 pwm
->tls_error
= ret
;
446 _do_tls_connect (pwm_t
* pwm
, const char *host
, int port
,
447 const char *cert
, const char *key
, const char *cacert
,
448 const char *prio
, const char *server_fp
, int verify
)
450 struct tcp_s
*tcp
= pwmd_calloc (1, sizeof (struct tcp_s
));
454 return GPG_ERR_ENOMEM
;
456 if (!cert
|| !key
|| !cacert
)
459 return GPG_ERR_INV_ARG
;
464 tcp
->host
= pwmd_strdup (host
);
471 tcp
->tls
= pwmd_calloc (1, sizeof (struct tls_s
));
478 tcp
->tls
->timeout
= pwm
->socket_timeout
;
479 tcp
->tls
->verify
= verify
;
480 tcp
->tls
->cert
= pwmd_strdup (cert
);
487 tcp
->tls
->key
= pwmd_strdup (key
);
494 tcp
->tls
->ca
= pwmd_strdup (cacert
);
503 tcp
->tls
->priority
= pwmd_strdup (prio
);
504 if (!tcp
->tls
->priority
)
513 tcp
->tls
->server_fp
= pwmd_strdup (server_fp
);
514 if (!tcp
->tls
->server_fp
)
521 rc
= tcp_connect_common (pwm
);
527 pwm
->tcp
->fd
= pwm
->tcp
->tls
->fd
= &pwm
->fd
;
528 rc
= assuan_socket_connect_fd (pwm
->ctx
, pwm
->fd
, 0);
540 * tls[46]://[hostname][:port]
543 _parse_tls_url (const char *str
, char **host
, int *port
)
546 return parse_hostname_common (str
, host
, port
);