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); \
60 tls_free (struct tls_s
*tls
)
66 pwmd_free (tls
->cert
);
68 pwmd_free (tls
->priority
);
69 pwmd_free (tls
->server_fp
);
74 time_t start
= time (NULL
);
80 ret
= gnutls_bye (tls
->session
, GNUTLS_SHUT_WR
);
81 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
85 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
87 gnutls_deinit (tls
->session
);
91 gnutls_certificate_free_credentials (tls
->x509
);
99 read_hook_tls (struct tls_s
*tls
, assuan_fd_t fd
, void *data
, size_t len
)
105 time_t start
= time (NULL
);
112 ret
= gnutls_record_recv (tls
->session
, data
, len
);
113 if (ret
== GNUTLS_E_REHANDSHAKE
)
115 ret
= gnutls_rehandshake (tls
->session
);
116 if (ret
== GNUTLS_E_GOT_APPLICATION_DATA
)
118 else if (ret
!= GNUTLS_E_SUCCESS
)
126 ret
= gnutls_handshake (tls
->session
);
128 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
137 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
145 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
154 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
158 else if (ret
== GNUTLS_E_PREMATURE_TERMINATION
)
161 return ret
< 0 ? -1 : ret
;
165 write_hook_tls (struct tls_s
* tls
, assuan_fd_t fd
, const void *data
,
168 /* This is probably the BYE command after a previous call timed
169 * out. If not done, the time(2) call seems to hang.*/
174 time_t start
= time (NULL
);
180 ret
= gnutls_record_send (tls
->session
, data
, len
);
181 TEST_TIMEOUT (tls
->timeout
, ret
, start
, rc
);
190 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
194 else if (ret
== GNUTLS_E_PREMATURE_TERMINATION
)
197 return ret
< 0 ? -1 : ret
;
201 tls_fingerprint (gnutls_session_t ses
, char **result
)
203 gnutls_x509_crt_t crt
;
204 const gnutls_datum_t
*cert_list
;
206 unsigned char buf
[32];
207 size_t len
= sizeof (buf
);
210 gnutls_x509_crt_init (&crt
);
212 return GPG_ERR_ENOMEM
;
214 cert_list
= gnutls_certificate_get_peers (ses
, &count
);
217 gnutls_x509_crt_import (crt
, &cert_list
[0], GNUTLS_X509_FMT_DER
);
218 gnutls_x509_crt_get_fingerprint (crt
, GNUTLS_DIG_SHA256
, buf
, &len
);
221 gnutls_x509_crt_deinit (crt
);
222 *result
= bin2hex (buf
, len
);
223 return *result
? 0 : GPG_ERR_ENOMEM
;
227 verify_certificate (pwm_t
* pwm
)
230 const gnutls_datum_t
*cert_list
;
231 unsigned int cert_list_size
;
233 gnutls_x509_crt_t cert
;
236 i
= gnutls_certificate_verify_peers2 (pwm
->tcp
->tls
->session
, &status
);
240 return GPG_ERR_BAD_CERT
;
243 if (status
& GNUTLS_CERT_INVALID
)
244 return GPG_ERR_BAD_CERT
;
246 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
247 return GPG_ERR_BAD_SIGNATURE
;
249 if (status
& GNUTLS_CERT_REVOKED
)
250 return GPG_ERR_CERT_REVOKED
;
252 if (gnutls_certificate_type_get (pwm
->tcp
->tls
->session
) != GNUTLS_CRT_X509
)
253 return GPG_ERR_UNSUPPORTED_CERT
;
255 if (gnutls_x509_crt_init (&cert
) < 0)
256 return gpg_error_from_errno (ENOMEM
);
258 cert_list
= gnutls_certificate_get_peers (pwm
->tcp
->tls
->session
,
262 rc
= GPG_ERR_MISSING_CERT
;
266 for (i
= 0; i
< cert_list_size
; i
++)
268 if (gnutls_x509_crt_import (cert
, &cert_list
[i
], GNUTLS_X509_FMT_DER
) <
271 rc
= GPG_ERR_BAD_CERT_CHAIN
;
275 if (gnutls_x509_crt_get_expiration_time (cert
) < time (0))
277 rc
= GPG_ERR_CERT_EXPIRED
;
281 if (gnutls_x509_crt_get_activation_time (cert
) > time (0))
283 rc
= GPG_ERR_CERT_TOO_YOUNG
;
287 if (pwm
->tcp
->tls
->verify
)
289 if (!gnutls_x509_crt_check_hostname (cert
, pwm
->tcp
->host
))
291 rc
= GPG_ERR_BAD_CERT_CHAIN
;
297 if (pwm
->tcp
->tls
->server_fp
)
301 rc
= tls_fingerprint (pwm
->tcp
->tls
->session
, &result
);
304 if (!result
|| !*result
||
305 strcasecmp (result
, pwm
->tcp
->tls
->server_fp
))
306 rc
= GPG_ERR_BAD_CERT
;
313 gnutls_x509_crt_deinit (cert
);
318 tls_init (pwm_t
* pwm
)
324 ret
= gnutls_certificate_allocate_credentials (&pwm
->tcp
->tls
->x509
);
326 return gpg_error_from_errno (ENOMEM
);
328 /* The client certificate must be signed by the CA of the pwmd server
329 * certificate in order for the client to authenticate successfully. Man in
330 * the middle attacks are still possible if the attacker is running a pwmd
331 * that doesn't require client certificate authentication. So require the
332 * client to verify the server certificate.
334 ret
= gnutls_certificate_set_x509_trust_file (pwm
->tcp
->tls
->x509
,
336 GNUTLS_X509_FMT_PEM
);
340 rc
= GPG_ERR_INV_CERT_OBJ
;
344 ret
= gnutls_certificate_set_x509_key_file (pwm
->tcp
->tls
->x509
,
347 GNUTLS_X509_FMT_PEM
);
348 if (ret
!= GNUTLS_E_SUCCESS
)
351 rc
= GPG_ERR_INV_CERT_OBJ
;
355 ret
= gnutls_init (&pwm
->tcp
->tls
->session
, GNUTLS_CLIENT
);
356 if (ret
!= GNUTLS_E_SUCCESS
)
359 rc
= GPG_ERR_INV_CERT_OBJ
;
363 ret
= gnutls_priority_set_direct (pwm
->tcp
->tls
->session
,
364 pwm
->tcp
->tls
->priority
? pwm
->tcp
->tls
->
365 priority
: "SECURE256", &errstr
);
366 if (ret
!= GNUTLS_E_SUCCESS
)
369 rc
= GPG_ERR_INV_CERT_OBJ
;
373 ret
= gnutls_credentials_set (pwm
->tcp
->tls
->session
, GNUTLS_CRD_CERTIFICATE
,
374 pwm
->tcp
->tls
->x509
);
375 if (ret
!= GNUTLS_E_SUCCESS
)
378 rc
= GPG_ERR_INV_CERT_OBJ
;
382 gnutls_transport_set_ptr (pwm
->tcp
->tls
->session
,
383 (gnutls_transport_ptr_t
) pwm
->fd
);
384 fcntl (pwm
->fd
, F_SETFL
, O_NONBLOCK
);
385 time_t start
= time (NULL
);
388 ret
= gnutls_handshake (pwm
->tcp
->tls
->session
);
390 TEST_TIMEOUT (pwm
->tcp
->tls
->timeout
, ret
, start
, rc
);
394 while (ret
== GNUTLS_E_AGAIN
|| ret
== GNUTLS_E_INTERRUPTED
);
396 if (ret
!= GNUTLS_E_SUCCESS
)
399 rc
= GPG_ERR_INV_CERT_OBJ
;
403 rc
= verify_certificate (pwm
);
416 _do_tls_connect (pwm_t
* pwm
, const char *host
, int port
,
417 const char *cert
, const char *key
, const char *cacert
,
418 const char *prio
, const char *server_fp
, int verify
)
420 struct tcp_s
*tcp
= pwmd_calloc (1, sizeof (struct tcp_s
));
424 return GPG_ERR_ENOMEM
;
426 if (!cert
|| !key
|| !cacert
)
429 return GPG_ERR_INV_ARG
;
434 tcp
->host
= pwmd_strdup (host
);
441 tcp
->tls
= pwmd_calloc (1, sizeof (struct tls_s
));
448 tcp
->tls
->timeout
= pwm
->socket_timeout
;
449 tcp
->tls
->verify
= verify
;
450 tcp
->tls
->cert
= pwmd_strdup (cert
);
457 tcp
->tls
->key
= pwmd_strdup (key
);
464 tcp
->tls
->ca
= pwmd_strdup (cacert
);
473 tcp
->tls
->priority
= pwmd_strdup (prio
);
474 if (!tcp
->tls
->priority
)
483 tcp
->tls
->server_fp
= pwmd_strdup (server_fp
);
484 if (!tcp
->tls
->server_fp
)
491 rc
= tcp_connect_common (pwm
);
497 pwm
->tcp
->fd
= pwm
->tcp
->tls
->fd
= &pwm
->fd
;
498 rc
= assuan_socket_connect_fd (pwm
->ctx
, pwm
->fd
, 0);
513 * tls[46]://[hostname][:port]
516 _parse_tls_url (const char *str
, char **host
, int *port
)
519 return parse_hostname_common (str
, host
, port
);