Fix Coverity issue #100363.
[libpwmd.git] / src / tls.c
blob8c479322a3b96d52c0b742a341757711f93b8437
1 /*
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/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <fcntl.h>
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
37 #include "types.h"
38 #include "misc.h"
40 #define WAIT_INTERVAL 50000
41 #define TEST_TIMEOUT(timeout, ret, start, rc) \
42 do { \
43 if (ret == GNUTLS_E_AGAIN) \
44 { \
45 time_t now = time (NULL); \
46 if (timeout && now - start >= timeout) \
47 { \
48 rc = gpg_error (GPG_ERR_ETIMEDOUT); \
49 break; \
50 } \
51 struct timeval tv = { 0, WAIT_INTERVAL }; \
52 select (0, NULL, NULL, NULL, &tv); \
53 } \
54 if (ret != GNUTLS_E_INTERRUPTED) \
55 break; \
56 } \
57 while (0)
59 void
60 tls_free (pwm_t *pwm)
62 struct tls_s *tls = pwm->tcp->tls;
63 int ret = 0;
65 if (!tls)
66 return;
68 pwmd_free (tls->ca);
69 pwmd_free (tls->cert);
70 pwmd_free (tls->key);
71 pwmd_free (tls->priority);
72 pwmd_free (tls->server_fp);
74 if (tls->session)
76 time_t start = time (NULL);
80 gpg_error_t rc = 0;
82 ret = gnutls_bye (tls->session, GNUTLS_SHUT_WR);
83 TEST_TIMEOUT (tls->timeout, ret, start, rc);
84 if (rc)
85 break;
87 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
89 gnutls_deinit (tls->session);
91 else if (pwm->fd != -1)
93 close (pwm->fd);
94 pwm->fd = -1;
97 if (tls->x509)
98 gnutls_certificate_free_credentials (tls->x509);
100 tls->session = NULL;
101 tls->x509 = NULL;
102 pwmd_free (tls);
103 pwm->tls_error = ret;
106 ssize_t
107 read_hook_tls (pwm_t *pwm, assuan_fd_t fd, void *data, size_t len)
109 struct tls_s *tls = pwm->tcp->tls;
111 if (*tls->fd == -1)
112 return -1;
114 ssize_t ret;
115 time_t start = time (NULL);
117 rehandshake:
120 gpg_error_t rc = 0;
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)
127 goto rehandshake;
128 else if (ret != GNUTLS_E_SUCCESS)
130 pwm->tls_error = ret;
131 ret = 0;
132 break;
137 ret = gnutls_handshake (tls->session);
138 rc = 0;
139 TEST_TIMEOUT (tls->timeout, ret, start, rc);
140 if (rc)
142 close (*tls->fd);
143 *tls->fd = -1;
144 errno = ETIMEDOUT;
145 return -1;
148 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
150 pwm->tls_error = ret;
151 continue;
154 TEST_TIMEOUT (tls->timeout, ret, start, rc);
155 if (rc)
157 close (*tls->fd);
158 *tls->fd = -1;
159 errno = ETIMEDOUT;
160 return -1;
163 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
165 if (ret < 0)
166 pwm->tls_error = ret;
168 if (!ret)
169 return 0;
170 else if (ret == GNUTLS_E_PREMATURE_TERMINATION)
171 errno = EPIPE;
173 return ret < 0 ? -1 : ret;
176 ssize_t
177 write_hook_tls (pwm_t *pwm, assuan_fd_t fd, const void *data, size_t len)
179 struct tls_s *tls = pwm->tcp->tls;
181 /* This is probably the BYE command after a previous call timed
182 * out. If not done, the time(2) call seems to hang.*/
183 if (*tls->fd == -1)
184 return -1;
186 ssize_t ret;
187 time_t start = time (NULL);
191 gpg_error_t rc = 0;
193 ret = gnutls_record_send (tls->session, data, len);
194 TEST_TIMEOUT (tls->timeout, ret, start, rc);
195 if (rc)
197 close (*tls->fd);
198 *tls->fd = -1;
199 errno = ETIMEDOUT;
200 return -1;
203 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
205 if (ret < 0)
206 pwm->tls_error = ret;
208 if (!ret)
209 return 0;
210 else if (ret == GNUTLS_E_PREMATURE_TERMINATION)
211 errno = EPIPE;
213 return ret < 0 ? -1 : ret;
216 static gpg_error_t
217 tls_fingerprint (pwm_t *pwm, char **result)
219 gnutls_session_t ses = pwm->tcp->tls->session;
220 gnutls_x509_crt_t crt;
221 const gnutls_datum_t *cert_list;
222 unsigned count;
223 unsigned char buf[32];
224 size_t len = sizeof (buf);
226 *result = NULL;
227 gnutls_x509_crt_init (&crt);
228 if (!crt)
229 return GPG_ERR_ENOMEM;
231 cert_list = gnutls_certificate_get_peers (ses, &count);
232 if (count)
234 pwm->tls_error = gnutls_x509_crt_import (crt, &cert_list[0],
235 GNUTLS_X509_FMT_DER);
236 if (!pwm->tls_error)
237 gnutls_x509_crt_get_fingerprint (crt, GNUTLS_DIG_SHA256, buf, &len);
240 gnutls_x509_crt_deinit (crt);
241 if (!count)
243 pwm->tls_error = GNUTLS_CERT_INVALID;
244 return GPG_ERR_MISSING_CERT;
247 if (!pwm->tls_error)
248 *result = bin2hex (buf, len);
250 return !pwm->tls_error ? 0 : GPG_ERR_ENOMEM;
253 static gpg_error_t
254 verify_certificate (pwm_t *pwm)
256 unsigned int status;
257 const gnutls_datum_t *cert_list;
258 unsigned int cert_list_size;
259 int i;
260 gnutls_x509_crt_t cert;
261 gpg_error_t rc = 0;
263 i = gnutls_certificate_verify_peers2 (pwm->tcp->tls->session, &status);
264 if (i < 0)
266 pwm->tls_error = i;
267 return GPG_ERR_BAD_CERT;
270 if (status & GNUTLS_CERT_INVALID)
271 return GPG_ERR_BAD_CERT;
273 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
274 return GPG_ERR_BAD_SIGNATURE;
276 if (status & GNUTLS_CERT_REVOKED)
277 return GPG_ERR_CERT_REVOKED;
279 if (gnutls_certificate_type_get (pwm->tcp->tls->session) != GNUTLS_CRT_X509)
280 return GPG_ERR_UNSUPPORTED_CERT;
282 if (gnutls_x509_crt_init (&cert) < 0)
283 return gpg_error_from_errno (ENOMEM);
285 cert_list = gnutls_certificate_get_peers (pwm->tcp->tls->session,
286 &cert_list_size);
287 if (!cert_list)
289 rc = GPG_ERR_MISSING_CERT;
290 goto done;
293 for (i = 0; i < cert_list_size; i++)
295 pwm->tls_error = gnutls_x509_crt_import (cert, &cert_list[i],
296 GNUTLS_X509_FMT_DER);
297 if (pwm->tls_error < 0)
299 rc = GPG_ERR_BAD_CERT_CHAIN;
300 goto done;
303 if (gnutls_x509_crt_get_expiration_time (cert) < time (0))
305 rc = GPG_ERR_CERT_EXPIRED;
306 goto done;
309 if (gnutls_x509_crt_get_activation_time (cert) > time (0))
311 rc = GPG_ERR_CERT_TOO_YOUNG;
312 goto done;
315 if (pwm->tcp->tls->verify)
317 if (!gnutls_x509_crt_check_hostname (cert, pwm->tcp->host))
319 rc = GPG_ERR_BAD_CERT_CHAIN;
320 goto done;
325 if (pwm->tcp->tls->server_fp)
327 char *result;
329 rc = tls_fingerprint (pwm, &result);
330 if (!rc)
332 if (!result || !*result ||
333 strcasecmp (result, pwm->tcp->tls->server_fp))
334 rc = GPG_ERR_BAD_CERT;
336 pwmd_free (result);
340 done:
341 gnutls_x509_crt_deinit (cert);
342 return rc;
345 static gpg_error_t
346 tls_init (pwm_t * pwm)
348 gpg_error_t rc;
349 int ret;
350 const char *errstr;
352 ret = gnutls_certificate_allocate_credentials (&pwm->tcp->tls->x509);
353 if (ret)
355 pwm->tls_error = ret;
356 return gpg_error_from_errno (ENOMEM);
359 /* The client certificate must be signed by the CA of the pwmd server
360 * certificate in order for the client to authenticate successfully. Man in
361 * the middle attacks are still possible if the attacker is running a pwmd
362 * that doesn't require client certificate authentication. So require the
363 * client to verify the server certificate.
365 ret = gnutls_certificate_set_x509_trust_file (pwm->tcp->tls->x509,
366 pwm->tcp->tls->ca,
367 GNUTLS_X509_FMT_PEM);
368 if (ret == -1)
370 rc = GPG_ERR_INV_CERT_OBJ;
371 goto fail;
374 ret = gnutls_certificate_set_x509_key_file (pwm->tcp->tls->x509,
375 pwm->tcp->tls->cert,
376 pwm->tcp->tls->key,
377 GNUTLS_X509_FMT_PEM);
378 if (ret != GNUTLS_E_SUCCESS)
380 pwm->tls_error = ret;
381 rc = GPG_ERR_INV_CERT_OBJ;
382 goto fail;
385 ret = gnutls_init (&pwm->tcp->tls->session, GNUTLS_CLIENT);
386 if (ret != GNUTLS_E_SUCCESS)
388 pwm->tls_error = ret;
389 rc = GPG_ERR_INV_CERT_OBJ;
390 goto fail;
393 ret = gnutls_priority_set_direct (pwm->tcp->tls->session,
394 pwm->tcp->tls->priority ? pwm->tcp->tls->
395 priority : "SECURE256", &errstr);
396 if (ret != GNUTLS_E_SUCCESS)
398 pwm->tls_error = ret;
399 rc = GPG_ERR_INV_CERT_OBJ;
400 goto fail;
403 ret = gnutls_credentials_set (pwm->tcp->tls->session, GNUTLS_CRD_CERTIFICATE,
404 pwm->tcp->tls->x509);
405 if (ret != GNUTLS_E_SUCCESS)
407 pwm->tls_error = ret;
408 rc = GPG_ERR_INV_CERT_OBJ;
409 goto fail;
412 gnutls_transport_set_ptr (pwm->tcp->tls->session,
413 (gnutls_transport_ptr_t) pwm->fd);
414 if (fcntl (pwm->fd, F_SETFL, O_NONBLOCK) == -1)
416 rc = gpg_error_from_errno (errno);
417 goto fail;
420 time_t start = time (NULL);
423 ret = gnutls_handshake (pwm->tcp->tls->session);
424 rc = 0;
425 TEST_TIMEOUT (pwm->tcp->tls->timeout, ret, start, rc);
426 if (rc)
427 goto fail;
429 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
431 if (ret != GNUTLS_E_SUCCESS)
433 pwm->tls_error = ret;
434 rc = GPG_ERR_INV_CERT_OBJ;
435 goto fail;
438 rc = verify_certificate (pwm);
440 fail:
441 if (rc)
443 /* Keep the original error since it maybe overwritten during TLS
444 * shutdown. */
445 ret = pwm->tls_error;
446 free_tcp (pwm);
447 pwm->tls_error = ret;
450 return rc;
453 gpg_error_t
454 _do_tls_connect (pwm_t * pwm, const char *host, int port,
455 const char *cert, const char *key, const char *cacert,
456 const char *prio, const char *server_fp, int verify)
458 struct tcp_s *tcp = pwmd_calloc (1, sizeof (struct tcp_s));
459 gpg_error_t rc;
461 if (!tcp)
462 return GPG_ERR_ENOMEM;
464 if (!cert || !key || !cacert)
466 pwmd_free (tcp);
467 return GPG_ERR_INV_ARG;
470 pwm->tcp = tcp;
471 tcp->port = port;
472 tcp->host = pwmd_strdup (host);
473 if (!tcp->host)
475 rc = GPG_ERR_ENOMEM;
476 goto fail;
479 tcp->tls = pwmd_calloc (1, sizeof (struct tls_s));
480 if (!tcp->tls)
482 rc = GPG_ERR_ENOMEM;
483 goto fail;
486 tcp->tls->timeout = pwm->socket_timeout;
487 tcp->tls->verify = verify;
488 tcp->tls->cert = pwmd_strdup (cert);
489 if (!tcp->tls->cert)
491 rc = GPG_ERR_ENOMEM;
492 goto fail;
495 tcp->tls->key = pwmd_strdup (key);
496 if (!tcp->tls->key)
498 rc = GPG_ERR_ENOMEM;
499 goto fail;
502 tcp->tls->ca = pwmd_strdup (cacert);
503 if (!tcp->tls->ca)
505 rc = GPG_ERR_ENOMEM;
506 goto fail;
509 if (prio)
511 tcp->tls->priority = pwmd_strdup (prio);
512 if (!tcp->tls->priority)
514 rc = GPG_ERR_ENOMEM;
515 goto fail;
519 if (server_fp)
521 tcp->tls->server_fp = pwmd_strdup (server_fp);
522 if (!tcp->tls->server_fp)
524 rc = GPG_ERR_ENOMEM;
525 goto fail;
529 rc = tcp_connect_common (pwm);
530 if (!rc)
532 rc = tls_init (pwm);
533 if (!rc)
535 pwm->tcp->fd = pwm->tcp->tls->fd = &pwm->fd;
536 rc = assuan_socket_connect_fd (pwm->ctx, pwm->fd, 0);
540 fail:
541 if (rc)
542 free_tcp (pwm);
544 return rc;
548 * tls[46]://[hostname][:port]
550 gpg_error_t
551 _parse_tls_url (const char *str, char **host, int *port)
553 *port = 6466;
554 return parse_hostname_common (str, host, port);