Add configuration parameter "tls_dh_params_file".
[libpwmd.git] / src / cache.c
blob5000274bbb8d427039df2684197f94a4da4240dc
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
3 2016
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <pthread.h>
28 #include <ctype.h>
29 #include <errno.h>
31 #include "pwmd-error.h"
32 #include <gcrypt.h>
33 #include "mutex.h"
34 #include "cache.h"
35 #include "status.h"
36 #include "mem.h"
37 #include "util-misc.h"
38 #include "util-string.h"
39 #include "agent.h"
41 #ifdef HAVE_TIME_H
42 #include <time.h>
43 #endif
45 static pthread_mutex_t cache_mutex;
46 static struct slist_s *key_cache;
47 static unsigned char *cache_iv;
48 static unsigned char *cache_key;
49 static size_t cache_blocksize;
50 static size_t cache_keysize;
51 struct agent_s *cache_agent;
53 extern void log_write (const char *fmt, ...);
55 static gpg_error_t clear_once (file_cache_t * p, int agent);
56 static int remove_entry (const char *);
57 static void free_entry (file_cache_t * p, int agent);
59 static file_cache_t *
60 get_entry (const char *filename)
62 int t = slist_length (key_cache);
63 int i;
65 if (!filename)
66 return NULL;
68 for (i = 0; i < t; i++)
70 file_cache_t *p = slist_nth_data (key_cache, i);
72 if (!strcmp (p->filename, filename))
73 return p;
76 return NULL;
79 gpg_error_t
80 cache_lock_mutex (void *ctx, const char *filename, long lock_timeout, int add,
81 int timeout)
83 MUTEX_LOCK (&cache_mutex);
84 gpg_error_t rc = 0;
85 file_cache_t *p = get_entry (filename);
87 if (p)
89 MUTEX_UNLOCK (&cache_mutex);
90 MUTEX_TRYLOCK (ctx, p->mutex, rc, lock_timeout);
92 else if (add)
94 rc = cache_add_file (filename, NULL, timeout);
95 if (!rc)
97 p = get_entry (filename);
98 MUTEX_UNLOCK (&cache_mutex);
99 MUTEX_TRYLOCK (ctx, p->mutex, rc, lock_timeout);
101 else
103 MUTEX_UNLOCK (&cache_mutex);
106 else
108 MUTEX_UNLOCK (&cache_mutex);
111 return !p && !rc ? GPG_ERR_NO_DATA : rc;
114 static int
115 remove_entry (const char *filename)
117 MUTEX_LOCK (&cache_mutex);
118 file_cache_t *p = get_entry (filename);
120 pthread_cleanup_push (cleanup_mutex_cb, &cache_mutex);
121 if (p)
123 /* Keep a refcount because another client maybe editing this same new
124 * file. The entry needs to be kept in case the other client SAVE's.
126 p->refcount--;
127 if (!p->refcount)
128 free_entry (p, 0);
131 pthread_cleanup_pop (1);
132 return p ? 1 : 0;
135 static gpg_error_t
136 free_cache_data (file_cache_t * cache)
138 gpg_error_t rc = GPG_ERR_NO_DATA;
139 int i, t;
140 struct client_thread_s *found = NULL;
141 int self = 0;
143 if (!cache->data)
144 return 0;
146 cache_lock ();
147 MUTEX_LOCK (&cn_mutex);
148 pthread_cleanup_push (cleanup_mutex_cb, &cn_mutex);
149 t = slist_length (cn_thread_list);
151 for (i = 0; i < t; i++)
153 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
155 if (!thd->cl)
156 continue;
158 self = pthread_equal (pthread_self (), thd->tid);
160 if (thd->cl->filename && !strcmp (thd->cl->filename, cache->filename))
162 if (self)
164 found = thd;
165 continue;
168 /* Continue trying to find a client who has the same file open and
169 * also has a lock. */
170 rc = cache_lock_mutex (thd->cl->ctx, thd->cl->filename, -1, 0, -1);
171 if (!rc)
173 found = thd;
174 break;
179 if (self && (!rc || rc == GPG_ERR_NO_DATA) && found)
180 rc = cache_lock_mutex (found->cl->ctx, found->cl->filename, -1, 0, -1);
182 if (exiting || !rc || rc == GPG_ERR_NO_DATA)
184 free_cache_data_once (cache->data);
185 cache->data = NULL;
186 cache->defer_clear = 0;
187 cache->timeout = -1;
189 if (found)
190 cache_unlock_mutex (found->cl->filename, 0);
192 rc = 0;
195 if (rc)
196 cache->defer_clear = 1;
198 pthread_cleanup_pop (1);
199 cache_unlock ();
200 return rc;
203 gpg_error_t
204 cache_unlock_mutex (const char *filename, int remove)
206 MUTEX_LOCK (&cache_mutex);
207 file_cache_t *p = get_entry (filename);
209 if (p)
211 MUTEX_UNLOCK (p->mutex);
212 if (remove)
213 remove_entry (filename);
216 MUTEX_UNLOCK (&cache_mutex);
217 return p ? 0 : GPG_ERR_NO_DATA;
220 static gpg_error_t
221 test_agent_cache_once (file_cache_t *p, const char *grip)
223 gpg_error_t rc;
224 char *line;
226 rc = agent_command (cache_agent, &line, NULL, "KEYINFO --data %s", grip);
227 if (!rc)
229 char **fields = str_split (line, " ", 0);
231 /* Smartcard. */
232 if (*fields[1] == 'T')
233 rc = GPG_ERR_NO_DATA;
234 else
235 rc = isdigit (*fields[4]) || *fields[5] == 'C' ? 0 : GPG_ERR_NO_DATA;
237 strv_free (fields);
238 xfree (line);
241 return rc;
244 /* Test each keygrip in data->(sig)grip for gpg-agent cache status. The file is
245 * considered cached if at least one grip is cached in the agent or one siggrip
246 * is cached in the case of 'sign'.
248 static gpg_error_t
249 test_agent_cache (file_cache_t *data, int sign)
251 gpg_error_t rc = 0;
252 char **p;
254 if ((!data->grip && !sign) || (!data->siggrip && sign))
255 return GPG_ERR_NO_DATA;
257 for (p = data->grip; !sign && p && *p; p++)
259 rc = test_agent_cache_once (data, *p);
260 if (!rc || rc != GPG_ERR_NO_DATA)
261 break;
264 if (!rc && sign)
266 for (p = data->siggrip; p && *p; p++)
268 rc = test_agent_cache_once (data, *p);
269 if (!rc || rc != GPG_ERR_NO_DATA)
270 break;
274 return rc;
277 static gpg_error_t
278 extract_keygrip_once (struct crypto_s *crypto, char **keyids, int sign,
279 char ***dst)
281 gpgme_key_t *result;
282 gpgme_error_t rc;
283 int i;
284 char **grips = NULL;
286 rc = crypto_list_keys (crypto, keyids, sign, &result);
287 if (rc)
288 return rc;
290 for (i = 0; result[i]; i++)
292 gpgme_subkey_t s;
294 for (s = result[i]->subkeys; s; s = s->next)
296 char **k;
298 if (sign && !s->can_sign)
299 continue;
301 for (k = keyids; *k; k++)
303 if (!strcmp (*k, s->keyid) && s->keygrip)
305 char **tmp;
306 size_t len = strv_length (grips);
308 tmp = xrealloc (grips, len+2 * sizeof (char *));
309 if (!tmp)
311 rc = GPG_ERR_ENOMEM;
312 break;
315 grips = tmp;
316 grips[len] = str_dup (s->keygrip);
317 if (!grips[len])
319 rc = GPG_ERR_ENOMEM;
320 break;
323 grips[++len] = NULL;
329 if (!rc)
331 strv_free (*dst);
332 *dst = grips;
334 else
336 strv_free (grips);
337 grips = NULL;
340 crypto_free_key_list (result);
341 return rc ? rc : grips ? rc : GPG_ERR_NO_DATA;
344 static gpg_error_t
345 extract_keygrips (file_cache_t *data)
347 gpg_error_t rc = 0;
348 struct crypto_s *crypto;
349 char **grips = NULL, **siggrips = NULL;
351 if (!data || !data->data)
352 return 0;
354 rc = crypto_init (&crypto, NULL, NULL, 0, NULL);
355 if (rc)
356 return rc;
358 pthread_cleanup_push ((void *)crypto_free, crypto);
360 if (data->data->pubkey)
361 rc = extract_keygrip_once (crypto, data->data->pubkey, 0, &grips);
363 if (!rc && data->data->sigkey)
364 rc = extract_keygrip_once (crypto, data->data->sigkey, 1, &siggrips);
366 if (!rc)
368 strv_free (data->grip);
369 data->grip = grips;
370 strv_free (data->siggrip);
371 data->siggrip = siggrips;
373 else
375 strv_free (grips);
376 strv_free (siggrips);
379 pthread_cleanup_pop (1);
380 return rc;
383 static gpg_error_t
384 iscached (const char *filename, int *defer, int agent, int sign)
386 file_cache_t *p = get_entry (filename);
387 gpg_error_t rc = 0;
389 if (!agent)
391 if (!p || !p->data)
392 return GPG_ERR_NO_DATA;
394 if (defer)
395 *defer = p->defer_clear;
397 return 0;
400 if (!p)
401 return GPG_ERR_NO_DATA;
403 if (defer)
404 *defer = p->defer_clear;
406 if (!rc && (p->grip || p->siggrip))
407 rc = test_agent_cache (p, sign);
408 else if (!rc)
409 rc = GPG_ERR_NO_DATA;
411 return rc;
414 unsigned
415 cache_file_count ()
417 MUTEX_LOCK (&cache_mutex);
418 unsigned total = 0, i, n;
420 pthread_cleanup_push (cleanup_mutex_cb, &cache_mutex);
421 n = slist_length (key_cache);
422 for (i = 0; i < n; i++)
424 file_cache_t *p = slist_nth_data (key_cache, i);
426 if (!iscached (p->filename, NULL, 0, 0))
427 total++;
430 pthread_cleanup_pop (1);
431 return total;
434 void
435 cache_adjust_timeout ()
437 MUTEX_LOCK (&cache_mutex);
438 int t = slist_length (key_cache);
439 int i;
441 pthread_cleanup_push (cleanup_mutex_cb, &cache_mutex);
443 for (i = 0; i < t; i++)
445 file_cache_t *p = slist_nth_data (key_cache, i);
447 if (p->timeout > 0)
448 p->timeout--;
450 if (!p->timeout || (p->defer_clear && p->timeout != -1))
452 /* The file associated with this cache entry may be locked by a
453 * client. Defer freeing this cache entry until the next timeout
454 * check. */
455 if (!clear_once (p, 1))
456 send_status_all (STATUS_CACHE, NULL);
460 pthread_cleanup_pop (1);
463 static gpg_error_t
464 set_timeout (const char *filename, int timeout, int defer, int force)
466 MUTEX_LOCK (&cache_mutex);
467 file_cache_t *p = get_entry (filename);
468 gpg_error_t rc = 0;
470 if (!p)
472 MUTEX_UNLOCK (&cache_mutex);
473 return GPG_ERR_NOT_FOUND;
476 p->reset = timeout;
477 if (!p->defer_clear && (p->timeout == -1 || force))
478 p->timeout = timeout;
480 if (!timeout || defer)
482 rc = clear_once (p, 1);
483 if (!rc)
484 send_status_all (STATUS_CACHE, NULL);
487 MUTEX_UNLOCK (&cache_mutex);
488 return rc;
491 gpg_error_t
492 cache_set_data (const char *filename, struct cache_data_s * data)
494 MUTEX_LOCK (&cache_mutex);
495 gpg_error_t rc = GPG_ERR_NO_DATA;
496 file_cache_t *p = get_entry (filename);
498 if (p)
500 p->data = data;
501 rc = set_timeout (filename, p->reset, p->defer_clear, 0);
502 if (!rc && !p->reset)
504 clear_once (p, 0);
505 send_status_all (STATUS_CACHE, NULL);
507 else if (!rc)
508 rc = extract_keygrips (p);
511 MUTEX_UNLOCK (&cache_mutex);
512 return p && !rc ? 0 : rc;
515 struct cache_data_s *
516 cache_get_data (const char *filename)
518 MUTEX_LOCK (&cache_mutex);
519 file_cache_t *p = get_entry (filename);
521 MUTEX_UNLOCK (&cache_mutex);
522 return p ? p->data : NULL;
525 gpg_error_t
526 cache_add_file (const char *filename, struct cache_data_s *data, int timeout)
528 MUTEX_LOCK (&cache_mutex);
529 file_cache_t *p = get_entry (filename);
530 struct slist_s *new;
531 gpg_error_t rc = 0;
533 if (p)
535 p->data = data;
536 p->refcount++;
537 rc = set_timeout (filename, timeout, p->defer_clear, 0);
538 MUTEX_UNLOCK (&cache_mutex);
539 send_status_all (STATUS_CACHE, NULL);
541 if (!rc)
542 rc = extract_keygrips (p);
544 return rc;
547 p = xcalloc (1, sizeof (file_cache_t));
548 if (!p)
550 MUTEX_UNLOCK (&cache_mutex);
551 return GPG_ERR_ENOMEM;
554 p->mutex = (pthread_mutex_t *) xmalloc (sizeof (pthread_mutex_t));
555 if (!p->mutex)
557 xfree (p);
558 MUTEX_UNLOCK (&cache_mutex);
559 return GPG_ERR_ENOMEM;
562 pthread_mutexattr_t attr;
563 pthread_mutexattr_init (&attr);
564 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
565 pthread_mutex_init (p->mutex, &attr);
566 pthread_mutexattr_destroy (&attr);
567 p->filename = str_dup (filename);
568 p->data = data;
569 p->refcount++;
570 new = slist_append (key_cache, p);
571 if (!new)
573 pthread_mutex_destroy (p->mutex);
574 xfree (p->mutex);
575 xfree (p);
576 MUTEX_UNLOCK (&cache_mutex);
577 return GPG_ERR_ENOMEM;
580 key_cache = new;
581 rc = cache_set_timeout(filename, timeout);
582 MUTEX_UNLOCK (&cache_mutex);
583 send_status_all (STATUS_CACHE, NULL);
585 if (!rc)
586 rc = extract_keygrips (p);
588 return rc;
591 gpg_error_t
592 cache_clear_agent_keys (const char *filename, int decrypt, int sign)
594 MUTEX_LOCK (&cache_mutex);
595 gpg_error_t rc = 0;
596 char **key;
597 file_cache_t *p = get_entry (filename);
599 if (!p)
601 MUTEX_UNLOCK (&cache_mutex);
602 return GPG_ERR_NOT_FOUND;
605 for (key = p->grip; decrypt && key && *key; key++)
607 rc = agent_command (cache_agent, NULL, NULL,
608 "CLEAR_PASSPHRASE --mode=normal %s", *key);
609 if (rc)
610 break;
613 for (key = p->siggrip; sign && key && *key; key++)
615 rc = agent_command (cache_agent, NULL, NULL,
616 "CLEAR_PASSPHRASE --mode=normal %s", *key);
617 if (rc)
618 break;
621 MUTEX_UNLOCK (&cache_mutex);
622 return rc;
625 static gpg_error_t
626 clear_once (file_cache_t *p, int agent)
628 gpg_error_t rc = 0, trc;
630 if (agent)
631 rc = cache_clear_agent_keys (p->filename, 1, 1);
633 trc = free_cache_data (p);
634 return rc ? rc : trc;
637 static void
638 free_entry (file_cache_t *p, int agent)
640 gpg_error_t rc;
642 if (!p)
643 return;
645 rc = clear_once (p, agent);
646 if (rc)
647 log_write ("%s(): %s", __FUNCTION__, pwmd_strerror (rc));
649 if (p->mutex)
651 pthread_mutex_destroy (p->mutex);
652 xfree (p->mutex);
655 strv_free (p->grip);
656 strv_free (p->siggrip);
657 xfree (p->filename);
658 key_cache = slist_remove (key_cache, p);
659 xfree (p);
662 gpg_error_t
663 cache_clear (struct client_s *client, const char *filename, int agent)
665 file_cache_t *p;
666 gpg_error_t rc = 0, all_rc = 0;
667 int i, t;
669 MUTEX_LOCK (&cache_mutex);
671 if (filename)
673 p = get_entry (filename);
674 if (!p)
676 MUTEX_UNLOCK (&cache_mutex);
677 return 0;
680 pthread_cleanup_push (cleanup_mutex_cb, &cache_mutex);
681 rc = clear_once (p, agent);
682 pthread_cleanup_pop (1);
683 if (rc)
684 log_write ("%s(): %s", __FUNCTION__, pwmd_strerror (rc));
686 return rc;
689 pthread_cleanup_push (cleanup_mutex_cb, &cache_mutex);
690 t = slist_length (key_cache);
691 for (i = 0; i < t; i++)
693 p = slist_nth_data (key_cache, i);
695 if (client)
697 assuan_peercred_t peer;
698 int n = client->no_access_param;
700 client->no_access_param = 1;
701 rc = do_validate_peer (client->ctx, p->filename, &peer);
702 client->no_access_param = n;
703 if (rc == GPG_ERR_FORBIDDEN)
704 rc = peer_is_invoker (client);
706 if (rc)
708 all_rc = !all_rc ? rc : all_rc;
709 continue;
713 clear_once (p, agent);
716 pthread_cleanup_pop (1);
717 return all_rc;
720 gpg_error_t
721 cache_iscached (const char *filename, int *defer, int agent, int sign)
723 gpg_error_t rc;
725 if (!filename)
726 return GPG_ERR_INV_PARAMETER;
728 MUTEX_LOCK (&cache_mutex);
729 pthread_cleanup_push (cleanup_mutex_cb, &cache_mutex);
730 rc = iscached (filename, defer, agent, sign);
731 pthread_cleanup_pop (1);
733 /* Test if the data file disappeared from the filesystem. */
734 if ((!rc || gpg_err_code (rc) == GPG_ERR_NO_DATA)
735 && access (filename, R_OK) == -1)
737 rc = gpg_error_from_errno (errno);
738 if (gpg_err_code (rc) == GPG_ERR_ENOENT)
740 /* Fixes clearing the cache entry that was created during OPEN when
741 * SAVE'ing a new file. */
742 if ((defer && *defer == 1) || !defer)
744 rc = cache_defer_clear (filename);
745 if (!rc && defer)
746 *defer = 1;
749 rc = GPG_ERR_ENOENT;
753 return rc;
756 gpg_error_t
757 cache_defer_clear (const char *filename)
759 MUTEX_LOCK (&cache_mutex);
760 file_cache_t *p = get_entry (filename);
761 gpg_error_t rc = 0;
763 if (!p)
764 rc = GPG_ERR_NOT_FOUND;
765 else
766 p->defer_clear = 1;
768 MUTEX_UNLOCK (&cache_mutex);
769 return rc;
772 gpg_error_t
773 cache_set_timeout (const char *filename, int timeout)
775 return set_timeout (filename, timeout, 0, 1);
778 void
779 cache_deinit ()
781 MUTEX_LOCK (&cache_mutex);
782 int i, t = slist_length (key_cache);
784 pthread_cleanup_push (cleanup_mutex_cb, &cache_mutex);
785 for (i = 0; i < t; i++)
787 file_cache_t *p = slist_nth_data (key_cache, i);
789 free_entry (p, 1);
790 t = slist_length (key_cache);
791 i = -1;
794 gcry_free (cache_key);
795 xfree (cache_iv);
796 cache_key = NULL;
797 cache_iv = NULL;
798 agent_free (cache_agent);
799 cache_agent = NULL;
800 pthread_cleanup_pop (1);
801 pthread_mutex_destroy (&cache_mutex);
804 void
805 cache_mutex_init ()
807 pthread_mutexattr_t attr;
809 pthread_mutexattr_init (&attr);
810 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
811 pthread_mutex_init (&cache_mutex, &attr);
812 pthread_mutexattr_destroy (&attr);
815 gpg_error_t
816 cache_init ()
818 gpg_error_t rc = 0;
820 rc = agent_init (&cache_agent);
821 if (rc)
823 cache_agent = NULL;
824 return rc;
827 if (!cache_key)
829 rc = gcry_cipher_algo_info (GCRY_CIPHER_AES, GCRYCTL_GET_BLKLEN, NULL,
830 &cache_blocksize);
831 if (rc)
832 return rc;
834 rc = gcry_cipher_algo_info (GCRY_CIPHER_AES, GCRYCTL_GET_KEYLEN, NULL,
835 &cache_keysize);
836 if (rc)
837 return rc;
839 cache_key = gcry_malloc (cache_keysize);
840 if (!cache_key)
841 return GPG_ERR_ENOMEM;
843 cache_iv = xmalloc (cache_blocksize);
844 if (!cache_iv)
846 gcry_free (cache_key);
847 cache_key = NULL;
848 return GPG_ERR_ENOMEM;
851 gcry_create_nonce (cache_key, cache_keysize);
852 gcry_create_nonce (cache_iv, cache_blocksize);
856 cache_mutex_init ();
857 return rc;
860 void
861 cache_lock ()
863 MUTEX_LOCK (&cache_mutex);
866 void
867 cache_unlock ()
869 MUTEX_UNLOCK (&cache_mutex);
872 void
873 free_cache_data_once (struct cache_data_s *data)
875 if (!data)
876 return;
878 strv_free (data->pubkey);
879 strv_free (data->sigkey);
880 xfree (data->crc);
881 xfree (data->doc);
882 xfree (data);
885 static void
886 cleanup_cipher (void *arg)
888 gcry_cipher_close ((gcry_cipher_hd_t) arg);
891 gpg_error_t
892 cache_encrypt (struct crypto_s *crypto)
894 gpg_error_t rc;
895 gcry_cipher_hd_t h;
896 size_t len = crypto->plaintext_size;
898 rc = gcry_cipher_open (&h, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
899 if (rc)
900 return rc;
902 if (len % cache_blocksize)
904 unsigned char *p;
906 len += cache_blocksize - (len % cache_blocksize);
907 p = xrealloc (crypto->plaintext, len * sizeof (unsigned char));
908 if (!p)
910 gcry_cipher_close (h);
911 return GPG_ERR_ENOMEM;
914 crypto->plaintext = p;
915 memset (&crypto->plaintext[crypto->plaintext_size], 0,
916 len - crypto->plaintext_size);
919 pthread_cleanup_push (cleanup_cipher, h);
920 rc = gcry_cipher_setiv (h, cache_iv, cache_blocksize);
921 if (!rc)
923 rc = gcry_cipher_setkey (h, cache_key, cache_keysize);
924 if (!rc)
926 unsigned char *buf = xmalloc (len);
928 pthread_cleanup_push (xfree, buf);
930 if (!buf)
931 rc = GPG_ERR_ENOMEM;
933 if (!rc)
935 rc = gcry_cipher_encrypt (h, buf, len, crypto->plaintext, len);
936 if (!rc)
938 xfree (crypto->plaintext);
939 crypto->plaintext = buf;
940 crypto->plaintext_size = len;
944 pthread_cleanup_pop (rc != 0);
948 pthread_cleanup_pop (1);
949 return rc;
952 gpg_error_t
953 cache_decrypt (struct crypto_s *crypto)
955 gcry_cipher_hd_t h = NULL;
956 gpg_error_t rc;
958 rc = gcry_cipher_open (&h, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 0);
959 if (rc)
960 return rc;
962 if (!rc)
963 rc = gcry_cipher_setiv (h, cache_iv, cache_blocksize);
965 if (!rc)
966 rc = gcry_cipher_setkey (h, cache_key, cache_keysize);
968 pthread_cleanup_push (cleanup_cipher, h);
970 if (!rc)
972 rc = gcry_cipher_decrypt (h, crypto->plaintext, crypto->plaintext_size,
973 NULL, 0);
974 if (rc || strncmp ((char *)crypto->plaintext, "<?xml ", 6))
976 if (!rc)
977 rc = GPG_ERR_BAD_DATA;
981 pthread_cleanup_pop (1);
982 return rc;
985 gpg_error_t
986 cache_kill_scd ()
988 gpg_error_t rc;
990 MUTEX_LOCK (&cache_mutex);
991 rc = agent_kill_scd (cache_agent);
992 MUTEX_UNLOCK (&cache_mutex);
993 return rc;