2 Copyright (C) 2012, 2013, 2014
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); \
52 if (ret != GNUTLS_E_INTERRUPTED) \
54 struct timeval tv = { 0, WAIT_INTERVAL }; \
55 select (0, NULL, NULL, NULL, &tv); \
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
);
93 gnutls_certificate_free_credentials (tls
->x509
);
102 read_hook_tls (pwm_t
*pwm
, assuan_fd_t fd
, void *data
, size_t len
)
104 struct tls_s
*tls
= pwm
->tcp
->tls
;
110 time_t start
= time (NULL
);
117 ret
= gnutls_record_recv (tls
->session
, data
, len
);
118 if (ret
== GNUTLS_E_REHANDSHAKE
)
120 ret
= gnutls_rehandshake (tls
->session
);
121 if (ret
== GNUTLS_E_GOT_APPLICATION_DATA
)
123 else if (ret
!= GNUTLS_E_SUCCESS
)
125 pwm
->tls_error
= ret
;
132 ret
= gnutls_handshake (tls
->session
);
134 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
143 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
145 pwm
->tls_error
= ret
;
152 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
161 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
164 pwm
->tls_error
= ret
;
168 else if (ret
== GNUTLS_E_PREMATURE_TERMINATION
)
171 return ret
< 0 ? -1 : ret
;
175 write_hook_tls (pwm_t
*pwm
, assuan_fd_t fd
, const void *data
, size_t len
)
177 struct tls_s
*tls
= pwm
->tcp
->tls
;
179 /* This is probably the BYE command after a previous call timed
180 * out. If not done, the time(2) call seems to hang.*/
185 time_t start
= time (NULL
);
191 ret
= gnutls_record_send (tls
->session
, data
, len
);
192 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
201 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
204 pwm
->tls_error
= ret
;
208 else if (ret
== GNUTLS_E_PREMATURE_TERMINATION
)
211 return ret
< 0 ? -1 : ret
;
215 tls_fingerprint (pwm_t
*pwm
, char **result
)
217 gnutls_session_t ses
= pwm
->tcp
->tls
->session
;
218 gnutls_x509_crt_t crt
;
219 const gnutls_datum_t
*cert_list
;
221 unsigned char buf
[32];
222 size_t len
= sizeof (buf
);
225 gnutls_x509_crt_init (&crt
);
227 return GPG_ERR_ENOMEM
;
229 cert_list
= gnutls_certificate_get_peers (ses
, &count
);
232 pwm
->tls_error
= gnutls_x509_crt_import (crt
, &cert_list
[0],
233 GNUTLS_X509_FMT_DER
);
235 gnutls_x509_crt_get_fingerprint (crt
, GNUTLS_DIG_SHA256
, buf
, &len
);
238 gnutls_x509_crt_deinit (crt
);
240 *result
= bin2hex (buf
, len
);
242 return !pwm
->tls_error
? 0 : GPG_ERR_ENOMEM
;
246 verify_certificate (pwm_t
*pwm
)
249 const gnutls_datum_t
*cert_list
;
250 unsigned int cert_list_size
;
252 gnutls_x509_crt_t cert
;
255 i
= gnutls_certificate_verify_peers2 (pwm
->tcp
->tls
->session
, &status
);
260 return GPG_ERR_BAD_CERT
;
263 if (status
& GNUTLS_CERT_INVALID
)
264 return GPG_ERR_BAD_CERT
;
266 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
267 return GPG_ERR_BAD_SIGNATURE
;
269 if (status
& GNUTLS_CERT_REVOKED
)
270 return GPG_ERR_CERT_REVOKED
;
272 if (gnutls_certificate_type_get (pwm
->tcp
->tls
->session
) != GNUTLS_CRT_X509
)
273 return GPG_ERR_UNSUPPORTED_CERT
;
275 if (gnutls_x509_crt_init (&cert
) < 0)
276 return gpg_error_from_errno (ENOMEM
);
278 cert_list
= gnutls_certificate_get_peers (pwm
->tcp
->tls
->session
,
282 rc
= GPG_ERR_MISSING_CERT
;
286 for (i
= 0; i
< cert_list_size
; i
++)
288 pwm
->tls_error
= gnutls_x509_crt_import (cert
, &cert_list
[i
],
289 GNUTLS_X509_FMT_DER
);
290 if (pwm
->tls_error
< 0)
292 rc
= GPG_ERR_BAD_CERT_CHAIN
;
296 if (gnutls_x509_crt_get_expiration_time (cert
) < time (0))
298 rc
= GPG_ERR_CERT_EXPIRED
;
302 if (gnutls_x509_crt_get_activation_time (cert
) > time (0))
304 rc
= GPG_ERR_CERT_TOO_YOUNG
;
308 if (pwm
->tcp
->tls
->verify
)
310 if (!gnutls_x509_crt_check_hostname (cert
, pwm
->tcp
->host
))
312 rc
= GPG_ERR_BAD_CERT_CHAIN
;
318 if (pwm
->tcp
->tls
->server_fp
)
322 rc
= tls_fingerprint (pwm
, &result
);
325 if (!result
|| !*result
||
326 strcasecmp (result
, pwm
->tcp
->tls
->server_fp
))
327 rc
= GPG_ERR_BAD_CERT
;
334 gnutls_x509_crt_deinit (cert
);
339 tls_init (pwm_t
* pwm
)
345 ret
= gnutls_certificate_allocate_credentials (&pwm
->tcp
->tls
->x509
);
348 pwm
->tls_error
= ret
;
349 return gpg_error_from_errno (ENOMEM
);
352 /* The client certificate must be signed by the CA of the pwmd server
353 * certificate in order for the client to authenticate successfully. Man in
354 * the middle attacks are still possible if the attacker is running a pwmd
355 * that doesn't require client certificate authentication. So require the
356 * client to verify the server certificate.
358 ret
= gnutls_certificate_set_x509_trust_file (pwm
->tcp
->tls
->x509
,
360 GNUTLS_X509_FMT_PEM
);
364 rc
= GPG_ERR_INV_CERT_OBJ
;
368 ret
= gnutls_certificate_set_x509_key_file (pwm
->tcp
->tls
->x509
,
371 GNUTLS_X509_FMT_PEM
);
372 if (ret
!= GNUTLS_E_SUCCESS
)
374 pwm
->tls_error
= ret
;
376 rc
= GPG_ERR_INV_CERT_OBJ
;
380 ret
= gnutls_init (&pwm
->tcp
->tls
->session
, GNUTLS_CLIENT
);
381 if (ret
!= GNUTLS_E_SUCCESS
)
383 pwm
->tls_error
= ret
;
385 rc
= GPG_ERR_INV_CERT_OBJ
;
389 ret
= gnutls_priority_set_direct (pwm
->tcp
->tls
->session
,
390 pwm
->tcp
->tls
->priority
? pwm
->tcp
->tls
->
391 priority
: "SECURE256", &errstr
);
392 if (ret
!= GNUTLS_E_SUCCESS
)
394 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
;
406 rc
= GPG_ERR_INV_CERT_OBJ
;
410 gnutls_transport_set_ptr (pwm
->tcp
->tls
->session
,
411 (gnutls_transport_ptr_t
) pwm
->fd
);
412 fcntl (pwm
->fd
, F_SETFL
, O_NONBLOCK
);
413 time_t start
= time (NULL
);
416 ret
= gnutls_handshake (pwm
->tcp
->tls
->session
);
418 TEST_TIMEOUT (pwm
->tcp
->tls
->timeout
, ret
, start
, rc
);
422 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
424 if (ret
!= GNUTLS_E_SUCCESS
)
426 pwm
->tls_error
= ret
;
428 rc
= GPG_ERR_INV_CERT_OBJ
;
432 rc
= verify_certificate (pwm
);
442 _do_tls_connect (pwm_t
* pwm
, const char *host
, int port
,
443 const char *cert
, const char *key
, const char *cacert
,
444 const char *prio
, const char *server_fp
, int verify
)
446 struct tcp_s
*tcp
= pwmd_calloc (1, sizeof (struct tcp_s
));
450 return GPG_ERR_ENOMEM
;
452 if (!cert
|| !key
|| !cacert
)
455 return GPG_ERR_INV_ARG
;
460 tcp
->host
= pwmd_strdup (host
);
467 tcp
->tls
= pwmd_calloc (1, sizeof (struct tls_s
));
474 tcp
->tls
->timeout
= pwm
->socket_timeout
;
475 tcp
->tls
->verify
= verify
;
476 tcp
->tls
->cert
= pwmd_strdup (cert
);
483 tcp
->tls
->key
= pwmd_strdup (key
);
490 tcp
->tls
->ca
= pwmd_strdup (cacert
);
499 tcp
->tls
->priority
= pwmd_strdup (prio
);
500 if (!tcp
->tls
->priority
)
509 tcp
->tls
->server_fp
= pwmd_strdup (server_fp
);
510 if (!tcp
->tls
->server_fp
)
517 rc
= tcp_connect_common (pwm
);
523 pwm
->tcp
->fd
= pwm
->tcp
->tls
->fd
= &pwm
->fd
;
524 rc
= assuan_socket_connect_fd (pwm
->ctx
, pwm
->fd
, 0);
536 * tls[46]://[hostname][:port]
539 _parse_tls_url (const char *str
, char **host
, int *port
)
542 return parse_hostname_common (str
, host
, port
);