Implement the server comamnd DECRYPT.
[gnupg.git] / g10 / trustdb.c
blob2db97152689930998c00b16fbb84d09d5b352ee0
1 /* trustdb.c
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 * 2008 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG 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 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG 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 this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
27 #ifndef DISABLE_REGEX
28 #include <sys/types.h>
29 #include <regex.h>
30 #endif /* !DISABLE_REGEX */
32 #include "gpg.h"
33 #include "status.h"
34 #include "iobuf.h"
35 #include "keydb.h"
36 #include "util.h"
37 #include "options.h"
38 #include "packet.h"
39 #include "main.h"
40 #include "i18n.h"
41 #include "tdbio.h"
42 #include "trustdb.h"
46 * A structure to store key identification as well as some stuff needed
47 * for validation
49 struct key_item {
50 struct key_item *next;
51 unsigned int ownertrust,min_ownertrust;
52 byte trust_depth;
53 byte trust_value;
54 char *trust_regexp;
55 u32 kid[2];
59 typedef struct key_item **KeyHashTable; /* see new_key_hash_table() */
62 * Structure to keep track of keys, this is used as an array wherre
63 * the item right after the last one has a keyblock set to NULL.
64 * Maybe we can drop this thing and replace it by key_item
66 struct key_array {
67 KBNODE keyblock;
71 /* control information for the trust DB */
72 static struct {
73 int init;
74 int level;
75 char *dbname;
76 } trustdb_args;
78 /* some globals */
79 static struct key_item *user_utk_list; /* temp. used to store --trusted-keys */
80 static struct key_item *utk_list; /* all ultimately trusted keys */
82 static int pending_check_trustdb;
84 static int validate_keys (int interactive);
87 /**********************************************
88 ************* some helpers *******************
89 **********************************************/
91 static struct key_item *
92 new_key_item (void)
94 struct key_item *k;
96 k = xmalloc_clear (sizeof *k);
97 return k;
100 static void
101 release_key_items (struct key_item *k)
103 struct key_item *k2;
105 for (; k; k = k2)
107 k2 = k->next;
108 xfree (k->trust_regexp);
109 xfree (k);
114 * For fast keylook up we need a hash table. Each byte of a KeyIDs
115 * should be distributed equally over the 256 possible values (except
116 * for v3 keyIDs but we consider them as not important here). So we
117 * can just use 10 bits to index a table of 1024 key items.
118 * Possible optimization: Don not use key_items but other hash_table when the
119 * duplicates lists gets too large.
121 static KeyHashTable
122 new_key_hash_table (void)
124 struct key_item **tbl;
126 tbl = xmalloc_clear (1024 * sizeof *tbl);
127 return tbl;
130 static void
131 release_key_hash_table (KeyHashTable tbl)
133 int i;
135 if (!tbl)
136 return;
137 for (i=0; i < 1024; i++)
138 release_key_items (tbl[i]);
139 xfree (tbl);
143 * Returns: True if the keyID is in the given hash table
145 static int
146 test_key_hash_table (KeyHashTable tbl, u32 *kid)
148 struct key_item *k;
150 for (k = tbl[(kid[1] & 0x03ff)]; k; k = k->next)
151 if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
152 return 1;
153 return 0;
157 * Add a new key to the hash table. The key is identified by its key ID.
159 static void
160 add_key_hash_table (KeyHashTable tbl, u32 *kid)
162 struct key_item *k, *kk;
164 for (k = tbl[(kid[1] & 0x03ff)]; k; k = k->next)
165 if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
166 return; /* already in table */
168 kk = new_key_item ();
169 kk->kid[0] = kid[0];
170 kk->kid[1] = kid[1];
171 kk->next = tbl[(kid[1] & 0x03ff)];
172 tbl[(kid[1] & 0x03ff)] = kk;
176 * Release a key_array
178 static void
179 release_key_array ( struct key_array *keys )
181 struct key_array *k;
183 if (keys) {
184 for (k=keys; k->keyblock; k++)
185 release_kbnode (k->keyblock);
186 xfree (keys);
191 /*********************************************
192 ********** Initialization *****************
193 *********************************************/
198 * Used to register extra ultimately trusted keys - this has to be done
199 * before initializing the validation module.
200 * FIXME: Should be replaced by a function to add those keys to the trustdb.
202 void
203 register_trusted_keyid(u32 *keyid)
205 struct key_item *k;
207 k = new_key_item ();
208 k->kid[0] = keyid[0];
209 k->kid[1] = keyid[1];
210 k->next = user_utk_list;
211 user_utk_list = k;
214 void
215 register_trusted_key( const char *string )
217 KEYDB_SEARCH_DESC desc;
219 if (classify_user_id (string, &desc) != KEYDB_SEARCH_MODE_LONG_KID )
221 log_error(_("`%s' is not a valid long keyID\n"), string );
222 return;
225 register_trusted_keyid(desc.u.kid);
229 * Helper to add a key to the global list of ultimately trusted keys.
230 * Retruns: true = inserted, false = already in in list.
232 static int
233 add_utk (u32 *kid)
235 struct key_item *k;
237 for (k = utk_list; k; k = k->next)
239 if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
241 return 0;
245 k = new_key_item ();
246 k->kid[0] = kid[0];
247 k->kid[1] = kid[1];
248 k->ownertrust = TRUST_ULTIMATE;
249 k->next = utk_list;
250 utk_list = k;
251 if( opt.verbose > 1 )
252 log_info(_("key %s: accepted as trusted key\n"), keystr(kid));
253 return 1;
257 /****************
258 * Verify that all our secret keys are usable and put them into the utk_list.
260 static void
261 verify_own_keys(void)
263 TRUSTREC rec;
264 ulong recnum;
265 int rc;
266 struct key_item *k;
268 if (utk_list)
269 return;
271 /* scan the trustdb to find all ultimately trusted keys */
272 for (recnum=1; !tdbio_read_record (recnum, &rec, 0); recnum++ )
274 if ( rec.rectype == RECTYPE_TRUST
275 && (rec.r.trust.ownertrust & TRUST_MASK) == TRUST_ULTIMATE)
277 byte *fpr = rec.r.trust.fingerprint;
278 int fprlen;
279 u32 kid[2];
281 /* Problem: We do only use fingerprints in the trustdb but
282 * we need the keyID here to indetify the key; we can only
283 * use that ugly hack to distinguish between 16 and 20
284 * butes fpr - it does not work always so we better change
285 * the whole validation code to only work with
286 * fingerprints */
287 fprlen = (!fpr[16] && !fpr[17] && !fpr[18] && !fpr[19])? 16:20;
288 keyid_from_fingerprint (fpr, fprlen, kid);
289 if (!add_utk (kid))
290 log_info(_("key %s occurs more than once in the trustdb\n"),
291 keystr(kid));
295 /* Put any --trusted-key keys into the trustdb */
296 for (k = user_utk_list; k; k = k->next)
298 if ( add_utk (k->kid) )
299 { /* not yet in trustDB as ultimately trusted */
300 PKT_public_key pk;
302 memset (&pk, 0, sizeof pk);
303 rc = get_pubkey (&pk, k->kid);
304 if (rc)
305 log_info(_("key %s: no public key for trusted key - skipped\n"),
306 keystr(k->kid));
307 else
309 update_ownertrust (&pk,
310 ((get_ownertrust (&pk) & ~TRUST_MASK)
311 | TRUST_ULTIMATE ));
312 release_public_key_parts (&pk);
315 log_info (_("key %s marked as ultimately trusted\n"),keystr(k->kid));
319 /* release the helper table table */
320 release_key_items (user_utk_list);
321 user_utk_list = NULL;
322 return;
326 /*********************************************
327 *********** TrustDB stuff *******************
328 *********************************************/
331 * Read a record but die if it does not exist
333 static void
334 read_record (ulong recno, TRUSTREC *rec, int rectype )
336 int rc = tdbio_read_record (recno, rec, rectype);
337 if (rc)
339 log_error(_("trust record %lu, req type %d: read failed: %s\n"),
340 recno, rec->rectype, g10_errstr(rc) );
341 tdbio_invalid();
343 if (rectype != rec->rectype)
345 log_error(_("trust record %lu is not of requested type %d\n"),
346 rec->recnum, rectype);
347 tdbio_invalid();
352 * Write a record and die on error
354 static void
355 write_record (TRUSTREC *rec)
357 int rc = tdbio_write_record (rec);
358 if (rc)
360 log_error(_("trust record %lu, type %d: write failed: %s\n"),
361 rec->recnum, rec->rectype, g10_errstr(rc) );
362 tdbio_invalid();
367 * sync the TrustDb and die on error
369 static void
370 do_sync(void)
372 int rc = tdbio_sync ();
373 if(rc)
375 log_error (_("trustdb: sync failed: %s\n"), g10_errstr(rc) );
376 g10_exit(2);
380 static const char *
381 trust_model_string(void)
383 switch(opt.trust_model)
385 case TM_CLASSIC: return "classic";
386 case TM_PGP: return "PGP";
387 case TM_EXTERNAL: return "external";
388 case TM_ALWAYS: return "always";
389 case TM_DIRECT: return "direct";
390 default: return "unknown";
394 /****************
395 * Perform some checks over the trustdb
396 * level 0: only open the db
397 * 1: used for initial program startup
400 setup_trustdb( int level, const char *dbname )
402 /* just store the args */
403 if( trustdb_args.init )
404 return 0;
405 trustdb_args.level = level;
406 trustdb_args.dbname = dbname? xstrdup(dbname): NULL;
407 return 0;
410 void
411 how_to_fix_the_trustdb ()
413 const char *name = trustdb_args.dbname;
415 if (!name)
416 name = "trustdb.gpg";
418 log_info (_("You may try to re-create the trustdb using the commands:\n"));
419 log_info (" cd %s\n", default_homedir ());
420 log_info (" gpg2 --export-ownertrust > otrust.tmp\n");
421 #ifdef HAVE_W32_SYSTEM
422 log_info (" del %s\n", name);
423 #else
424 log_info (" rm %s\n", name);
425 #endif
426 log_info (" gpg2 --import-ownertrust < otrust.tmp\n");
427 log_info (_("If that does not work, please consult the manual\n"));
431 void
432 init_trustdb()
434 int level = trustdb_args.level;
435 const char* dbname = trustdb_args.dbname;
437 if( trustdb_args.init )
438 return;
440 trustdb_args.init = 1;
442 if(level==0 || level==1)
444 int rc = tdbio_set_dbname( dbname, !!level );
445 if( rc )
446 log_fatal("can't init trustdb: %s\n", g10_errstr(rc) );
448 else
449 BUG();
451 if(opt.trust_model==TM_AUTO)
453 /* Try and set the trust model off of whatever the trustdb says
454 it is. */
455 opt.trust_model=tdbio_read_model();
457 /* Sanity check this ;) */
458 if(opt.trust_model!=TM_CLASSIC
459 && opt.trust_model!=TM_PGP
460 && opt.trust_model!=TM_EXTERNAL)
462 log_info(_("unable to use unknown trust model (%d) - "
463 "assuming %s trust model\n"),opt.trust_model,"PGP");
464 opt.trust_model=TM_PGP;
467 if(opt.verbose)
468 log_info(_("using %s trust model\n"),trust_model_string());
471 if(opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
473 /* Verify the list of ultimately trusted keys and move the
474 --trusted-keys list there as well. */
475 if(level==1)
476 verify_own_keys();
478 if(!tdbio_db_matches_options())
479 pending_check_trustdb=1;
484 /***********************************************
485 ************* Print helpers ****************
486 ***********************************************/
488 /****************
489 * This function returns a letter for a trustvalue Trust flags
490 * are ignore.
492 static int
493 trust_letter (unsigned int value)
495 switch( (value & TRUST_MASK) )
497 case TRUST_UNKNOWN: return '-';
498 case TRUST_EXPIRED: return 'e';
499 case TRUST_UNDEFINED: return 'q';
500 case TRUST_NEVER: return 'n';
501 case TRUST_MARGINAL: return 'm';
502 case TRUST_FULLY: return 'f';
503 case TRUST_ULTIMATE: return 'u';
504 default: return '?';
508 /* NOTE TO TRANSLATOR: these strings are similar to those in
509 trust_value_to_string(), but are a fixed length. This is needed to
510 make attractive information listings where columns line up
511 properly. The value "10" should be the length of the strings you
512 choose to translate to. This is the length in printable columns.
513 It gets passed to atoi() so everything after the number is
514 essentially a comment and need not be translated. Either key and
515 uid are both NULL, or neither are NULL. */
516 const char *
517 uid_trust_string_fixed(PKT_public_key *key,PKT_user_id *uid)
519 if(!key && !uid)
520 return _("10 translator see trustdb.c:uid_trust_string_fixed");
521 else if(uid->is_revoked || (key && key->is_revoked))
522 return _("[ revoked]");
523 else if(uid->is_expired)
524 return _("[ expired]");
525 else if(key)
526 switch(get_validity(key,uid)&TRUST_MASK)
528 case TRUST_UNKNOWN: return _("[ unknown]");
529 case TRUST_EXPIRED: return _("[ expired]");
530 case TRUST_UNDEFINED: return _("[ undef ]");
531 case TRUST_MARGINAL: return _("[marginal]");
532 case TRUST_FULLY: return _("[ full ]");
533 case TRUST_ULTIMATE: return _("[ultimate]");
536 return "err";
539 /* The strings here are similar to those in
540 pkclist.c:do_edit_ownertrust() */
541 const char *
542 trust_value_to_string (unsigned int value)
544 switch( (value & TRUST_MASK) )
546 case TRUST_UNKNOWN: return _("unknown");
547 case TRUST_EXPIRED: return _("expired");
548 case TRUST_UNDEFINED: return _("undefined");
549 case TRUST_NEVER: return _("never");
550 case TRUST_MARGINAL: return _("marginal");
551 case TRUST_FULLY: return _("full");
552 case TRUST_ULTIMATE: return _("ultimate");
553 default: return "err";
558 string_to_trust_value (const char *str)
560 if(ascii_strcasecmp(str,"undefined")==0)
561 return TRUST_UNDEFINED;
562 else if(ascii_strcasecmp(str,"never")==0)
563 return TRUST_NEVER;
564 else if(ascii_strcasecmp(str,"marginal")==0)
565 return TRUST_MARGINAL;
566 else if(ascii_strcasecmp(str,"full")==0)
567 return TRUST_FULLY;
568 else if(ascii_strcasecmp(str,"ultimate")==0)
569 return TRUST_ULTIMATE;
570 else
571 return -1;
574 /****************
575 * Recreate the WoT but do not ask for new ownertrusts. Special
576 * feature: In batch mode and without a forced yes, this is only done
577 * when a check is due. This can be used to run the check from a crontab
579 void
580 check_trustdb ()
582 init_trustdb();
583 if(opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
585 if (opt.batch && !opt.answer_yes)
587 ulong scheduled;
589 scheduled = tdbio_read_nextcheck ();
590 if (!scheduled)
592 log_info (_("no need for a trustdb check\n"));
593 return;
596 if (scheduled > make_timestamp ())
598 log_info (_("next trustdb check due at %s\n"),
599 strtimestamp (scheduled));
600 return;
604 validate_keys (0);
606 else
607 log_info (_("no need for a trustdb check with `%s' trust model\n"),
608 trust_model_string());
613 * Recreate the WoT.
615 void
616 update_trustdb()
618 init_trustdb();
619 if(opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
620 validate_keys (1);
621 else
622 log_info (_("no need for a trustdb update with `%s' trust model\n"),
623 trust_model_string());
626 void
627 revalidation_mark (void)
629 init_trustdb();
630 /* we simply set the time for the next check to 1 (far back in 1970)
631 * so that a --update-trustdb will be scheduled */
632 if (tdbio_write_nextcheck (1))
633 do_sync ();
634 pending_check_trustdb = 1;
638 trustdb_pending_check(void)
640 return pending_check_trustdb;
643 /* If the trustdb is dirty, and we're interactive, update it.
644 Otherwise, check it unless no-auto-check-trustdb is set. */
645 void
646 trustdb_check_or_update(void)
648 if(trustdb_pending_check())
650 if(opt.interactive)
651 update_trustdb();
652 else if(!opt.no_auto_check_trustdb)
653 check_trustdb();
657 void
658 read_trust_options(byte *trust_model,ulong *created,ulong *nextcheck,
659 byte *marginals,byte *completes,byte *cert_depth)
661 TRUSTREC opts;
663 init_trustdb();
665 read_record(0,&opts,RECTYPE_VER);
667 if(trust_model)
668 *trust_model=opts.r.ver.trust_model;
669 if(created)
670 *created=opts.r.ver.created;
671 if(nextcheck)
672 *nextcheck=opts.r.ver.nextcheck;
673 if(marginals)
674 *marginals=opts.r.ver.marginals;
675 if(completes)
676 *completes=opts.r.ver.completes;
677 if(cert_depth)
678 *cert_depth=opts.r.ver.cert_depth;
681 /***********************************************
682 *********** Ownertrust et al. ****************
683 ***********************************************/
685 static int
686 read_trust_record (PKT_public_key *pk, TRUSTREC *rec)
688 int rc;
690 init_trustdb();
691 rc = tdbio_search_trust_bypk (pk, rec);
692 if (rc == -1)
693 return -1; /* no record yet */
694 if (rc)
696 log_error ("trustdb: searching trust record failed: %s\n",
697 g10_errstr (rc));
698 return rc;
701 if (rec->rectype != RECTYPE_TRUST)
703 log_error ("trustdb: record %lu is not a trust record\n",
704 rec->recnum);
705 return G10ERR_TRUSTDB;
708 return 0;
711 /****************
712 * Return the assigned ownertrust value for the given public key.
713 * The key should be the primary key.
715 unsigned int
716 get_ownertrust ( PKT_public_key *pk)
718 TRUSTREC rec;
719 int rc;
721 rc = read_trust_record (pk, &rec);
722 if (rc == -1)
723 return TRUST_UNKNOWN; /* no record yet */
724 if (rc)
726 tdbio_invalid ();
727 return rc; /* actually never reached */
730 return rec.r.trust.ownertrust;
733 unsigned int
734 get_min_ownertrust (PKT_public_key *pk)
736 TRUSTREC rec;
737 int rc;
739 rc = read_trust_record (pk, &rec);
740 if (rc == -1)
741 return TRUST_UNKNOWN; /* no record yet */
742 if (rc)
744 tdbio_invalid ();
745 return rc; /* actually never reached */
748 return rec.r.trust.min_ownertrust;
752 * Same as get_ownertrust but this takes the minimum ownertrust value
753 * into into account, and will bump up the value as needed.
755 static int
756 get_ownertrust_with_min (PKT_public_key *pk)
758 unsigned int otrust,otrust_min;
760 otrust = (get_ownertrust (pk) & TRUST_MASK);
761 otrust_min = get_min_ownertrust (pk);
762 if(otrust<otrust_min)
764 /* If the trust that the user has set is less than the trust
765 that was calculated from a trust signature chain, use the
766 higher of the two. We do this here and not in
767 get_ownertrust since the underlying ownertrust should not
768 really be set - just the appearance of the ownertrust. */
770 otrust=otrust_min;
773 return otrust;
777 * Same as get_ownertrust but return a trust letter instead of an
778 * value. This takes the minimum ownertrust value into account.
781 get_ownertrust_info (PKT_public_key *pk)
783 return trust_letter(get_ownertrust_with_min(pk));
787 * Same as get_ownertrust but return a trust string instead of an
788 * value. This takes the minimum ownertrust value into account.
790 const char *
791 get_ownertrust_string (PKT_public_key *pk)
793 return trust_value_to_string(get_ownertrust_with_min(pk));
797 * Set the trust value of the given public key to the new value.
798 * The key should be a primary one.
800 void
801 update_ownertrust (PKT_public_key *pk, unsigned int new_trust )
803 TRUSTREC rec;
804 int rc;
806 rc = read_trust_record (pk, &rec);
807 if (!rc)
809 if (DBG_TRUST)
810 log_debug ("update ownertrust from %u to %u\n",
811 (unsigned int)rec.r.trust.ownertrust, new_trust );
812 if (rec.r.trust.ownertrust != new_trust)
814 rec.r.trust.ownertrust = new_trust;
815 write_record( &rec );
816 revalidation_mark ();
817 do_sync ();
820 else if (rc == -1)
821 { /* no record yet - create a new one */
822 size_t dummy;
824 if (DBG_TRUST)
825 log_debug ("insert ownertrust %u\n", new_trust );
827 memset (&rec, 0, sizeof rec);
828 rec.recnum = tdbio_new_recnum ();
829 rec.rectype = RECTYPE_TRUST;
830 fingerprint_from_pk (pk, rec.r.trust.fingerprint, &dummy);
831 rec.r.trust.ownertrust = new_trust;
832 write_record (&rec);
833 revalidation_mark ();
834 do_sync ();
835 rc = 0;
837 else
839 tdbio_invalid ();
843 static void
844 update_min_ownertrust (u32 *kid, unsigned int new_trust )
846 PKT_public_key *pk;
847 TRUSTREC rec;
848 int rc;
850 pk = xmalloc_clear (sizeof *pk);
851 rc = get_pubkey (pk, kid);
852 if (rc)
854 log_error(_("public key %s not found: %s\n"),keystr(kid),g10_errstr(rc));
855 return;
858 rc = read_trust_record (pk, &rec);
859 if (!rc)
861 if (DBG_TRUST)
862 log_debug ("key %08lX%08lX: update min_ownertrust from %u to %u\n",
863 (ulong)kid[0],(ulong)kid[1],
864 (unsigned int)rec.r.trust.min_ownertrust,
865 new_trust );
866 if (rec.r.trust.min_ownertrust != new_trust)
868 rec.r.trust.min_ownertrust = new_trust;
869 write_record( &rec );
870 revalidation_mark ();
871 do_sync ();
874 else if (rc == -1)
875 { /* no record yet - create a new one */
876 size_t dummy;
878 if (DBG_TRUST)
879 log_debug ("insert min_ownertrust %u\n", new_trust );
881 memset (&rec, 0, sizeof rec);
882 rec.recnum = tdbio_new_recnum ();
883 rec.rectype = RECTYPE_TRUST;
884 fingerprint_from_pk (pk, rec.r.trust.fingerprint, &dummy);
885 rec.r.trust.min_ownertrust = new_trust;
886 write_record (&rec);
887 revalidation_mark ();
888 do_sync ();
889 rc = 0;
891 else
893 tdbio_invalid ();
897 /* Clear the ownertrust and min_ownertrust values. Return true if a
898 change actually happened. */
900 clear_ownertrusts (PKT_public_key *pk)
902 TRUSTREC rec;
903 int rc;
905 rc = read_trust_record (pk, &rec);
906 if (!rc)
908 if (DBG_TRUST)
910 log_debug ("clearing ownertrust (old value %u)\n",
911 (unsigned int)rec.r.trust.ownertrust);
912 log_debug ("clearing min_ownertrust (old value %u)\n",
913 (unsigned int)rec.r.trust.min_ownertrust);
915 if (rec.r.trust.ownertrust || rec.r.trust.min_ownertrust)
917 rec.r.trust.ownertrust = 0;
918 rec.r.trust.min_ownertrust = 0;
919 write_record( &rec );
920 revalidation_mark ();
921 do_sync ();
922 return 1;
925 else if (rc != -1)
927 tdbio_invalid ();
929 return 0;
933 * Note: Caller has to do a sync
935 static void
936 update_validity (PKT_public_key *pk, PKT_user_id *uid,
937 int depth, int validity)
939 TRUSTREC trec, vrec;
940 int rc;
941 ulong recno;
943 namehash_from_uid(uid);
945 rc = read_trust_record (pk, &trec);
946 if (rc && rc != -1)
948 tdbio_invalid ();
949 return;
951 if (rc == -1) /* no record yet - create a new one */
953 size_t dummy;
955 rc = 0;
956 memset (&trec, 0, sizeof trec);
957 trec.recnum = tdbio_new_recnum ();
958 trec.rectype = RECTYPE_TRUST;
959 fingerprint_from_pk (pk, trec.r.trust.fingerprint, &dummy);
960 trec.r.trust.ownertrust = 0;
963 /* locate an existing one */
964 recno = trec.r.trust.validlist;
965 while (recno)
967 read_record (recno, &vrec, RECTYPE_VALID);
968 if ( !memcmp (vrec.r.valid.namehash, uid->namehash, 20) )
969 break;
970 recno = vrec.r.valid.next;
973 if (!recno) /* insert a new validity record */
975 memset (&vrec, 0, sizeof vrec);
976 vrec.recnum = tdbio_new_recnum ();
977 vrec.rectype = RECTYPE_VALID;
978 memcpy (vrec.r.valid.namehash, uid->namehash, 20);
979 vrec.r.valid.next = trec.r.trust.validlist;
980 trec.r.trust.validlist = vrec.recnum;
982 vrec.r.valid.validity = validity;
983 vrec.r.valid.full_count = uid->help_full_count;
984 vrec.r.valid.marginal_count = uid->help_marginal_count;
985 write_record (&vrec);
986 trec.r.trust.depth = depth;
987 write_record (&trec);
991 /***********************************************
992 ********* Query trustdb values **************
993 ***********************************************/
995 /* Return true if key is disabled */
997 cache_disabled_value(PKT_public_key *pk)
999 int rc;
1000 TRUSTREC trec;
1001 int disabled=0;
1003 if(pk->is_disabled)
1004 return (pk->is_disabled==2);
1006 init_trustdb();
1008 rc = read_trust_record (pk, &trec);
1009 if (rc && rc != -1)
1011 tdbio_invalid ();
1012 goto leave;
1014 if (rc == -1) /* no record found, so assume not disabled */
1015 goto leave;
1017 if(trec.r.trust.ownertrust & TRUST_FLAG_DISABLED)
1018 disabled=1;
1020 /* Cache it for later so we don't need to look at the trustdb every
1021 time */
1022 if(disabled)
1023 pk->is_disabled=2;
1024 else
1025 pk->is_disabled=1;
1027 leave:
1028 return disabled;
1031 void
1032 check_trustdb_stale(void)
1034 static int did_nextcheck=0;
1036 init_trustdb ();
1037 if (!did_nextcheck
1038 && (opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC))
1040 ulong scheduled;
1042 did_nextcheck = 1;
1043 scheduled = tdbio_read_nextcheck ();
1044 if (scheduled && scheduled <= make_timestamp ())
1046 if (opt.no_auto_check_trustdb)
1048 pending_check_trustdb = 1;
1049 log_info (_("please do a --check-trustdb\n"));
1051 else
1053 log_info (_("checking the trustdb\n"));
1054 validate_keys (0);
1061 * Return the validity information for PK. If the namehash is not
1062 * NULL, the validity of the corresponsing user ID is returned,
1063 * otherwise, a reasonable value for the entire key is returned.
1065 unsigned int
1066 get_validity (PKT_public_key *pk, PKT_user_id *uid)
1068 TRUSTREC trec, vrec;
1069 int rc;
1070 ulong recno;
1071 unsigned int validity;
1072 u32 kid[2];
1073 PKT_public_key *main_pk;
1075 if(uid)
1076 namehash_from_uid(uid);
1078 init_trustdb ();
1079 check_trustdb_stale();
1081 keyid_from_pk (pk, kid);
1082 if (pk->main_keyid[0] != kid[0] || pk->main_keyid[1] != kid[1])
1083 { /* this is a subkey - get the mainkey */
1084 main_pk = xmalloc_clear (sizeof *main_pk);
1085 rc = get_pubkey (main_pk, pk->main_keyid);
1086 if (rc)
1088 char *tempkeystr=xstrdup(keystr(pk->main_keyid));
1089 log_error ("error getting main key %s of subkey %s: %s\n",
1090 tempkeystr, keystr(kid), g10_errstr(rc));
1091 xfree(tempkeystr);
1092 validity = TRUST_UNKNOWN;
1093 goto leave;
1096 else
1097 main_pk = pk;
1099 if(opt.trust_model==TM_DIRECT)
1101 /* Note that this happens BEFORE any user ID stuff is checked.
1102 The direct trust model applies to keys as a whole. */
1103 validity=get_ownertrust(main_pk);
1104 goto leave;
1107 rc = read_trust_record (main_pk, &trec);
1108 if (rc && rc != -1)
1110 tdbio_invalid ();
1111 return 0;
1113 if (rc == -1) /* no record found */
1115 validity = TRUST_UNKNOWN;
1116 goto leave;
1119 /* loop over all user IDs */
1120 recno = trec.r.trust.validlist;
1121 validity = 0;
1122 while (recno)
1124 read_record (recno, &vrec, RECTYPE_VALID);
1126 if(uid)
1128 /* If a user ID is given we return the validity for that
1129 user ID ONLY. If the namehash is not found, then there
1130 is no validity at all (i.e. the user ID wasn't
1131 signed). */
1132 if(memcmp(vrec.r.valid.namehash,uid->namehash,20)==0)
1134 validity=(vrec.r.valid.validity & TRUST_MASK);
1135 break;
1138 else
1140 /* If no namehash is given, we take the maximum validity
1141 over all user IDs */
1142 if ( validity < (vrec.r.valid.validity & TRUST_MASK) )
1143 validity = (vrec.r.valid.validity & TRUST_MASK);
1146 recno = vrec.r.valid.next;
1149 if ( (trec.r.trust.ownertrust & TRUST_FLAG_DISABLED) )
1151 validity |= TRUST_FLAG_DISABLED;
1152 pk->is_disabled=2;
1154 else
1155 pk->is_disabled=1;
1157 leave:
1158 /* set some flags direct from the key */
1159 if (main_pk->is_revoked)
1160 validity |= TRUST_FLAG_REVOKED;
1161 if (main_pk != pk && pk->is_revoked)
1162 validity |= TRUST_FLAG_SUB_REVOKED;
1163 /* Note: expiration is a trust value and not a flag - don't know why
1164 * I initially designed it that way */
1165 if (main_pk->has_expired || pk->has_expired)
1166 validity = (validity & ~TRUST_MASK) | TRUST_EXPIRED;
1168 if (pending_check_trustdb)
1169 validity |= TRUST_FLAG_PENDING_CHECK;
1171 if (main_pk != pk)
1172 free_public_key (main_pk);
1173 return validity;
1177 get_validity_info (PKT_public_key *pk, PKT_user_id *uid)
1179 int trustlevel;
1181 if (!pk)
1182 return '?'; /* Just in case a NULL PK is passed. */
1184 trustlevel = get_validity (pk, uid);
1185 if ( (trustlevel & TRUST_FLAG_REVOKED) )
1186 return 'r';
1187 return trust_letter (trustlevel);
1190 const char *
1191 get_validity_string (PKT_public_key *pk, PKT_user_id *uid)
1193 int trustlevel;
1195 if (!pk)
1196 return "err"; /* Just in case a NULL PK is passed. */
1198 trustlevel = get_validity (pk, uid);
1199 if( trustlevel & TRUST_FLAG_REVOKED )
1200 return _("revoked");
1201 return trust_value_to_string(trustlevel);
1204 static void
1205 get_validity_counts (PKT_public_key *pk, PKT_user_id *uid)
1207 TRUSTREC trec, vrec;
1208 ulong recno;
1210 if(pk==NULL || uid==NULL)
1211 BUG();
1213 namehash_from_uid(uid);
1215 uid->help_marginal_count=uid->help_full_count=0;
1217 init_trustdb ();
1219 if(read_trust_record (pk, &trec)!=0)
1220 return;
1222 /* loop over all user IDs */
1223 recno = trec.r.trust.validlist;
1224 while (recno)
1226 read_record (recno, &vrec, RECTYPE_VALID);
1228 if(memcmp(vrec.r.valid.namehash,uid->namehash,20)==0)
1230 uid->help_marginal_count=vrec.r.valid.marginal_count;
1231 uid->help_full_count=vrec.r.valid.full_count;
1232 /* printf("Fetched marginal %d, full %d\n",uid->help_marginal_count,uid->help_full_count); */
1233 break;
1236 recno = vrec.r.valid.next;
1240 void
1241 list_trust_path( const char *username )
1243 (void)username;
1246 /****************
1247 * Enumerate all keys, which are needed to build all trust paths for
1248 * the given key. This function does not return the key itself or
1249 * the ultimate key (the last point in cerificate chain). Only
1250 * certificate chains which ends up at an ultimately trusted key
1251 * are listed. If ownertrust or validity is not NULL, the corresponding
1252 * value for the returned LID is also returned in these variable(s).
1254 * 1) create a void pointer and initialize it to NULL
1255 * 2) pass this void pointer by reference to this function.
1256 * Set lid to the key you want to enumerate and pass it by reference.
1257 * 3) call this function as long as it does not return -1
1258 * to indicate EOF. LID does contain the next key used to build the web
1259 * 4) Always call this function a last time with LID set to NULL,
1260 * so that it can free its context.
1262 * Returns: -1 on EOF or the level of the returned LID
1265 enum_cert_paths( void **context, ulong *lid,
1266 unsigned *ownertrust, unsigned *validity )
1268 (void)context;
1269 (void)lid;
1270 (void)ownertrust;
1271 (void)validity;
1272 return -1;
1276 /****************
1277 * Print the current path
1279 void
1280 enum_cert_paths_print (void **context, FILE *fp,
1281 int refresh, ulong selected_lid)
1283 (void)context;
1284 (void)fp;
1285 (void)refresh;
1286 (void)selected_lid;
1291 /****************************************
1292 *********** NEW NEW NEW ****************
1293 ****************************************/
1295 static int
1296 ask_ownertrust (u32 *kid,int minimum)
1298 PKT_public_key *pk;
1299 int rc;
1300 int ot;
1302 pk = xmalloc_clear (sizeof *pk);
1303 rc = get_pubkey (pk, kid);
1304 if (rc)
1306 log_error (_("public key %s not found: %s\n"),
1307 keystr(kid), g10_errstr(rc) );
1308 return TRUST_UNKNOWN;
1311 if(opt.force_ownertrust)
1313 log_info("force trust for key %s to %s\n",
1314 keystr(kid),trust_value_to_string(opt.force_ownertrust));
1315 update_ownertrust(pk,opt.force_ownertrust);
1316 ot=opt.force_ownertrust;
1318 else
1320 ot=edit_ownertrust(pk,0);
1321 if(ot>0)
1322 ot = get_ownertrust (pk);
1323 else if(ot==0)
1324 ot = minimum?minimum:TRUST_UNDEFINED;
1325 else
1326 ot = -1; /* quit */
1329 free_public_key( pk );
1331 return ot;
1335 static void
1336 mark_keyblock_seen (KeyHashTable tbl, KBNODE node)
1338 for ( ;node; node = node->next )
1339 if (node->pkt->pkttype == PKT_PUBLIC_KEY
1340 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1342 u32 aki[2];
1344 keyid_from_pk (node->pkt->pkt.public_key, aki);
1345 add_key_hash_table (tbl, aki);
1350 static void
1351 dump_key_array (int depth, struct key_array *keys)
1353 struct key_array *kar;
1355 for (kar=keys; kar->keyblock; kar++)
1357 KBNODE node = kar->keyblock;
1358 u32 kid[2];
1360 keyid_from_pk(node->pkt->pkt.public_key, kid);
1361 printf ("%d:%08lX%08lX:K::%c::::\n",
1362 depth, (ulong)kid[0], (ulong)kid[1], '?');
1364 for (; node; node = node->next)
1366 if (node->pkt->pkttype == PKT_USER_ID)
1368 int len = node->pkt->pkt.user_id->len;
1370 if (len > 30)
1371 len = 30;
1372 printf ("%d:%08lX%08lX:U:::%c:::",
1373 depth, (ulong)kid[0], (ulong)kid[1],
1374 (node->flag & 4)? 'f':
1375 (node->flag & 2)? 'm':
1376 (node->flag & 1)? 'q':'-');
1377 print_string (stdout, node->pkt->pkt.user_id->name, len, ':');
1378 putchar (':');
1379 putchar ('\n');
1386 static void
1387 store_validation_status (int depth, KBNODE keyblock, KeyHashTable stored)
1389 KBNODE node;
1390 int status;
1391 int any = 0;
1393 for (node=keyblock; node; node = node->next)
1395 if (node->pkt->pkttype == PKT_USER_ID)
1397 PKT_user_id *uid = node->pkt->pkt.user_id;
1398 if (node->flag & 4)
1399 status = TRUST_FULLY;
1400 else if (node->flag & 2)
1401 status = TRUST_MARGINAL;
1402 else if (node->flag & 1)
1403 status = TRUST_UNDEFINED;
1404 else
1405 status = 0;
1407 if (status)
1409 update_validity (keyblock->pkt->pkt.public_key,
1410 uid, depth, status);
1412 mark_keyblock_seen(stored,keyblock);
1414 any = 1;
1419 if (any)
1420 do_sync ();
1424 * check whether the signature sig is in the klist k
1426 static struct key_item *
1427 is_in_klist (struct key_item *k, PKT_signature *sig)
1429 for (; k; k = k->next)
1431 if (k->kid[0] == sig->keyid[0] && k->kid[1] == sig->keyid[1])
1432 return k;
1434 return NULL;
1438 * Mark the signature of the given UID which are used to certify it.
1439 * To do this, we first revmove all signatures which are not valid and
1440 * from the remain ones we look for the latest one. If this is not a
1441 * certification revocation signature we mark the signature by setting
1442 * node flag bit 8. Revocations are marked with flag 11, and sigs
1443 * from unavailable keys are marked with flag 12. Note that flag bits
1444 * 9 and 10 are used for internal purposes.
1446 static void
1447 mark_usable_uid_certs (KBNODE keyblock, KBNODE uidnode,
1448 u32 *main_kid, struct key_item *klist,
1449 u32 curtime, u32 *next_expire)
1451 KBNODE node;
1452 PKT_signature *sig;
1454 /* first check all signatures */
1455 for (node=uidnode->next; node; node = node->next)
1457 int rc;
1459 node->flag &= ~(1<<8 | 1<<9 | 1<<10 | 1<<11 | 1<<12);
1460 if (node->pkt->pkttype == PKT_USER_ID
1461 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1462 break; /* ready */
1463 if (node->pkt->pkttype != PKT_SIGNATURE)
1464 continue;
1465 sig = node->pkt->pkt.signature;
1466 if (main_kid
1467 && sig->keyid[0] == main_kid[0] && sig->keyid[1] == main_kid[1])
1468 continue; /* ignore self-signatures if we pass in a main_kid */
1469 if (!IS_UID_SIG(sig) && !IS_UID_REV(sig))
1470 continue; /* we only look at these signature classes */
1471 if(sig->sig_class>=0x11 && sig->sig_class<=0x13 &&
1472 sig->sig_class-0x10<opt.min_cert_level)
1473 continue; /* treat anything under our min_cert_level as an
1474 invalid signature */
1475 if (klist && !is_in_klist (klist, sig))
1476 continue; /* no need to check it then */
1477 if ((rc=check_key_signature (keyblock, node, NULL)))
1479 /* we ignore anything that won't verify, but tag the
1480 no_pubkey case */
1481 if(rc==G10ERR_NO_PUBKEY)
1482 node->flag |= 1<<12;
1483 continue;
1485 node->flag |= 1<<9;
1487 /* reset the remaining flags */
1488 for (; node; node = node->next)
1489 node->flag &= ~(1<<8 | 1<<9 | 1<<10 | 1<<11 | 1<<12);
1491 /* kbnode flag usage: bit 9 is here set for signatures to consider,
1492 * bit 10 will be set by the loop to keep track of keyIDs already
1493 * processed, bit 8 will be set for the usable signatures, and bit
1494 * 11 will be set for usable revocations. */
1496 /* for each cert figure out the latest valid one */
1497 for (node=uidnode->next; node; node = node->next)
1499 KBNODE n, signode;
1500 u32 kid[2];
1501 u32 sigdate;
1503 if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1504 break;
1505 if ( !(node->flag & (1<<9)) )
1506 continue; /* not a node to look at */
1507 if ( (node->flag & (1<<10)) )
1508 continue; /* signature with a keyID already processed */
1509 node->flag |= (1<<10); /* mark this node as processed */
1510 sig = node->pkt->pkt.signature;
1511 signode = node;
1512 sigdate = sig->timestamp;
1513 kid[0] = sig->keyid[0]; kid[1] = sig->keyid[1];
1515 /* Now find the latest and greatest signature */
1516 for (n=uidnode->next; n; n = n->next)
1518 if (n->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1519 break;
1520 if ( !(n->flag & (1<<9)) )
1521 continue;
1522 if ( (n->flag & (1<<10)) )
1523 continue; /* shortcut already processed signatures */
1524 sig = n->pkt->pkt.signature;
1525 if (kid[0] != sig->keyid[0] || kid[1] != sig->keyid[1])
1526 continue;
1527 n->flag |= (1<<10); /* mark this node as processed */
1529 /* If signode is nonrevocable and unexpired and n isn't,
1530 then take signode (skip). It doesn't matter which is
1531 older: if signode was older then we don't want to take n
1532 as signode is nonrevocable. If n was older then we're
1533 automatically fine. */
1535 if(((IS_UID_SIG(signode->pkt->pkt.signature) &&
1536 !signode->pkt->pkt.signature->flags.revocable &&
1537 (signode->pkt->pkt.signature->expiredate==0 ||
1538 signode->pkt->pkt.signature->expiredate>curtime))) &&
1539 (!(IS_UID_SIG(n->pkt->pkt.signature) &&
1540 !n->pkt->pkt.signature->flags.revocable &&
1541 (n->pkt->pkt.signature->expiredate==0 ||
1542 n->pkt->pkt.signature->expiredate>curtime))))
1543 continue;
1545 /* If n is nonrevocable and unexpired and signode isn't,
1546 then take n. Again, it doesn't matter which is older: if
1547 n was older then we don't want to take signode as n is
1548 nonrevocable. If signode was older then we're
1549 automatically fine. */
1551 if((!(IS_UID_SIG(signode->pkt->pkt.signature) &&
1552 !signode->pkt->pkt.signature->flags.revocable &&
1553 (signode->pkt->pkt.signature->expiredate==0 ||
1554 signode->pkt->pkt.signature->expiredate>curtime))) &&
1555 ((IS_UID_SIG(n->pkt->pkt.signature) &&
1556 !n->pkt->pkt.signature->flags.revocable &&
1557 (n->pkt->pkt.signature->expiredate==0 ||
1558 n->pkt->pkt.signature->expiredate>curtime))))
1560 signode = n;
1561 sigdate = sig->timestamp;
1562 continue;
1565 /* At this point, if it's newer, it goes in as the only
1566 remaining possibilities are signode and n are both either
1567 revocable or expired or both nonrevocable and unexpired.
1568 If the timestamps are equal take the later ordered
1569 packet, presuming that the key packets are hopefully in
1570 their original order. */
1572 if (sig->timestamp >= sigdate)
1574 signode = n;
1575 sigdate = sig->timestamp;
1579 sig = signode->pkt->pkt.signature;
1580 if (IS_UID_SIG (sig))
1581 { /* this seems to be a usable one which is not revoked.
1582 * Just need to check whether there is an expiration time,
1583 * We do the expired certification after finding a suitable
1584 * certification, the assumption is that a signator does not
1585 * want that after the expiration of his certificate the
1586 * system falls back to an older certification which has a
1587 * different expiration time */
1588 const byte *p;
1589 u32 expire;
1591 p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_SIG_EXPIRE, NULL );
1592 expire = p? sig->timestamp + buffer_to_u32(p) : 0;
1594 if (expire==0 || expire > curtime )
1596 signode->flag |= (1<<8); /* yeah, found a good cert */
1597 if (next_expire && expire && expire < *next_expire)
1598 *next_expire = expire;
1601 else
1602 signode->flag |= (1<<11);
1606 static int
1607 clean_sigs_from_uid(KBNODE keyblock,KBNODE uidnode,int noisy,int self_only)
1609 int deleted=0;
1610 KBNODE node;
1611 u32 keyid[2];
1613 assert(keyblock->pkt->pkttype==PKT_PUBLIC_KEY);
1615 keyid_from_pk(keyblock->pkt->pkt.public_key,keyid);
1617 /* Passing in a 0 for current time here means that we'll never weed
1618 out an expired sig. This is correct behavior since we want to
1619 keep the most recent expired sig in a series. */
1620 mark_usable_uid_certs(keyblock,uidnode,NULL,NULL,0,NULL);
1622 /* What we want to do here is remove signatures that are not
1623 considered as part of the trust calculations. Thus, all invalid
1624 signatures are out, as are any signatures that aren't the last of
1625 a series of uid sigs or revocations It breaks down like this:
1626 coming out of mark_usable_uid_certs, if a sig is unflagged, it is
1627 not even a candidate. If a sig has flag 9 or 10, that means it
1628 was selected as a candidate and vetted. If a sig has flag 8 it
1629 is a usable signature. If a sig has flag 11 it is a usable
1630 revocation. If a sig has flag 12 it was issued by an unavailable
1631 key. "Usable" here means the most recent valid
1632 signature/revocation in a series from a particular signer.
1634 Delete everything that isn't a usable uid sig (which might be
1635 expired), a usable revocation, or a sig from an unavailable
1636 key. */
1638 for(node=uidnode->next;
1639 node && node->pkt->pkttype==PKT_SIGNATURE;
1640 node=node->next)
1642 int keep=self_only?(node->pkt->pkt.signature->keyid[0]==keyid[0]
1643 && node->pkt->pkt.signature->keyid[1]==keyid[1]):1;
1645 /* Keep usable uid sigs ... */
1646 if((node->flag & (1<<8)) && keep)
1647 continue;
1649 /* ... and usable revocations... */
1650 if((node->flag & (1<<11)) && keep)
1651 continue;
1653 /* ... and sigs from unavailable keys. */
1654 /* disabled for now since more people seem to want sigs from
1655 unavailable keys removed altogether. */
1657 if(node->flag & (1<<12))
1658 continue;
1661 /* Everything else we delete */
1663 /* At this point, if 12 is set, the signing key was unavailable.
1664 If 9 or 10 is set, it's superceded. Otherwise, it's
1665 invalid. */
1667 if(noisy)
1668 log_info("removing signature from key %s on user ID \"%s\": %s\n",
1669 keystr(node->pkt->pkt.signature->keyid),
1670 uidnode->pkt->pkt.user_id->name,
1671 node->flag&(1<<12)?"key unavailable":
1672 node->flag&(1<<9)?"signature superceded":"invalid signature");
1674 delete_kbnode(node);
1675 deleted++;
1678 return deleted;
1681 /* This is substantially easier than clean_sigs_from_uid since we just
1682 have to establish if the uid has a valid self-sig, is not revoked,
1683 and is not expired. Note that this does not take into account
1684 whether the uid has a trust path to it - just whether the keyholder
1685 themselves has certified the uid. Returns true if the uid was
1686 compacted. To "compact" a user ID, we simply remove ALL signatures
1687 except the self-sig that caused the user ID to be remove-worthy.
1688 We don't actually remove the user ID packet itself since it might
1689 be ressurected in a later merge. Note that this function requires
1690 that the caller has already done a merge_keys_and_selfsig().
1692 TODO: change the import code to allow importing a uid with only a
1693 revocation if the uid already exists on the keyring. */
1695 static int
1696 clean_uid_from_key(KBNODE keyblock,KBNODE uidnode,int noisy)
1698 KBNODE node;
1699 PKT_user_id *uid=uidnode->pkt->pkt.user_id;
1700 int deleted=0;
1702 assert(keyblock->pkt->pkttype==PKT_PUBLIC_KEY);
1703 assert(uidnode->pkt->pkttype==PKT_USER_ID);
1705 /* Skip valid user IDs, compacted user IDs, and non-self-signed user
1706 IDs if --allow-non-selfsigned-uid is set. */
1707 if(uid->created || uid->flags.compacted
1708 || (!uid->is_expired && !uid->is_revoked
1709 && opt.allow_non_selfsigned_uid))
1710 return 0;
1712 for(node=uidnode->next;
1713 node && node->pkt->pkttype==PKT_SIGNATURE;
1714 node=node->next)
1715 if(!node->pkt->pkt.signature->flags.chosen_selfsig)
1717 delete_kbnode(node);
1718 deleted=1;
1719 uidnode->pkt->pkt.user_id->flags.compacted=1;
1722 if(noisy)
1724 const char *reason;
1725 char *user=utf8_to_native(uid->name,uid->len,0);
1727 if(uid->is_revoked)
1728 reason=_("revoked");
1729 else if(uid->is_expired)
1730 reason=_("expired");
1731 else
1732 reason=_("invalid");
1734 log_info("compacting user ID \"%s\" on key %s: %s\n",
1735 user,keystr_from_pk(keyblock->pkt->pkt.public_key),
1736 reason);
1738 xfree(user);
1741 return deleted;
1744 /* Needs to be called after a merge_keys_and_selfsig() */
1745 void
1746 clean_one_uid(KBNODE keyblock,KBNODE uidnode,int noisy,int self_only,
1747 int *uids_cleaned,int *sigs_cleaned)
1749 int dummy;
1751 assert(keyblock->pkt->pkttype==PKT_PUBLIC_KEY);
1752 assert(uidnode->pkt->pkttype==PKT_USER_ID);
1754 if(!uids_cleaned)
1755 uids_cleaned=&dummy;
1757 if(!sigs_cleaned)
1758 sigs_cleaned=&dummy;
1760 /* Do clean_uid_from_key first since if it fires off, we don't
1761 have to bother with the other */
1762 *uids_cleaned+=clean_uid_from_key(keyblock,uidnode,noisy);
1763 if(!uidnode->pkt->pkt.user_id->flags.compacted)
1764 *sigs_cleaned+=clean_sigs_from_uid(keyblock,uidnode,noisy,self_only);
1767 void
1768 clean_key(KBNODE keyblock,int noisy,int self_only,
1769 int *uids_cleaned,int *sigs_cleaned)
1771 KBNODE uidnode;
1773 merge_keys_and_selfsig(keyblock);
1775 for(uidnode=keyblock->next;
1776 uidnode && uidnode->pkt->pkttype!=PKT_PUBLIC_SUBKEY;
1777 uidnode=uidnode->next)
1778 if(uidnode->pkt->pkttype==PKT_USER_ID)
1779 clean_one_uid(keyblock,uidnode,noisy,self_only,
1780 uids_cleaned,sigs_cleaned);
1783 /* Returns a sanitized copy of the regexp (which might be "", but not
1784 NULL). */
1785 #ifndef DISABLE_REGEX
1786 static char *
1787 sanitize_regexp(const char *old)
1789 size_t start=0,len=strlen(old),idx=0;
1790 int escaped=0,standard_bracket=0;
1791 char *new=xmalloc((len*2)+1); /* enough to \-escape everything if we
1792 have to */
1794 /* There are basically two commonly-used regexps here. GPG and most
1795 versions of PGP use "<[^>]+[@.]example\.com>$" and PGP (9)
1796 command line uses "example.com" (i.e. whatever the user specfies,
1797 and we can't expect users know to use "\." instead of "."). So
1798 here are the rules: we're allowed to start with "<[^>]+[@.]" and
1799 end with ">$" or start and end with nothing. In between, the
1800 only legal regex character is ".", and everything else gets
1801 escaped. Part of the gotcha here is that some regex packages
1802 allow more than RFC-4880 requires. For example, 4880 has no "{}"
1803 operator, but GNU regex does. Commenting removes these operators
1804 from consideration. A possible future enhancement is to use
1805 commenting to effectively back off a given regex to the Henry
1806 Spencer syntax in 4880. -dshaw */
1808 /* Are we bracketed between "<[^>]+[@.]" and ">$" ? */
1809 if(len>=12 && strncmp(old,"<[^>]+[@.]",10)==0
1810 && old[len-2]=='>' && old[len-1]=='$')
1812 strcpy(new,"<[^>]+[@.]");
1813 idx=strlen(new);
1814 standard_bracket=1;
1815 start+=10;
1816 len-=2;
1819 /* Walk the remaining characters and ensure that everything that is
1820 left is not an operational regex character. */
1821 for(;start<len;start++)
1823 if(!escaped && old[start]=='\\')
1824 escaped=1;
1825 else if(!escaped && old[start]!='.')
1826 new[idx++]='\\';
1827 else
1828 escaped=0;
1830 new[idx++]=old[start];
1833 new[idx]='\0';
1835 /* Note that the (sub)string we look at might end with a bare "\".
1836 If it does, leave it that way. If the regexp actually ended with
1837 ">$", then it was escaping the ">" and is fine. If the regexp
1838 actually ended with the bare "\", then it's an illegal regexp and
1839 regcomp should kick it out. */
1841 if(standard_bracket)
1842 strcat(new,">$");
1844 return new;
1846 #endif /*!DISABLE_REGEX*/
1848 /* Used by validate_one_keyblock to confirm a regexp within a trust
1849 signature. Returns 1 for match, and 0 for no match or regex
1850 error. */
1851 static int
1852 check_regexp(const char *expr,const char *string)
1854 #ifdef DISABLE_REGEX
1855 /* When DISABLE_REGEX is defined, assume all regexps do not
1856 match. */
1857 return 0;
1858 #else
1859 int ret;
1860 char *regexp;
1862 regexp=sanitize_regexp(expr);
1864 #ifdef __riscos__
1865 ret=riscos_check_regexp(expr, string, DBG_TRUST);
1866 #else
1868 regex_t pat;
1870 ret=regcomp(&pat,regexp,REG_ICASE|REG_NOSUB|REG_EXTENDED);
1871 if(ret==0)
1873 ret=regexec(&pat,string,0,NULL,0);
1874 regfree(&pat);
1875 ret=(ret==0);
1878 #endif
1880 if(DBG_TRUST)
1881 log_debug("regexp `%s' (`%s') on `%s': %s\n",
1882 regexp,expr,string,ret==0?"YES":"NO");
1884 xfree(regexp);
1886 return ret;
1887 #endif
1891 * Return true if the key is signed by one of the keys in the given
1892 * key ID list. User IDs with a valid signature are marked by node
1893 * flags as follows:
1894 * flag bit 0: There is at least one signature
1895 * 1: There is marginal confidence that this is a legitimate uid
1896 * 2: There is full confidence that this is a legitimate uid.
1897 * 8: Used for internal purposes.
1898 * 9: Ditto (in mark_usable_uid_certs())
1899 * 10: Ditto (ditto)
1900 * This function assumes that all kbnode flags are cleared on entry.
1902 static int
1903 validate_one_keyblock (KBNODE kb, struct key_item *klist,
1904 u32 curtime, u32 *next_expire)
1906 struct key_item *kr;
1907 KBNODE node, uidnode=NULL;
1908 PKT_user_id *uid=NULL;
1909 PKT_public_key *pk = kb->pkt->pkt.public_key;
1910 u32 main_kid[2];
1911 int issigned=0, any_signed = 0;
1913 keyid_from_pk(pk, main_kid);
1914 for (node=kb; node; node = node->next)
1916 /* A bit of discussion here: is it better for the web of trust
1917 to be built among only self-signed uids? On the one hand, a
1918 self-signed uid is a statement that the key owner definitely
1919 intended that uid to be there, but on the other hand, a
1920 signed (but not self-signed) uid does carry trust, of a sort,
1921 even if it is a statement being made by people other than the
1922 key owner "through" the uids on the key owner's key. I'm
1923 going with the latter. However, if the user ID was
1924 explicitly revoked, or passively allowed to expire, that
1925 should stop validity through the user ID until it is
1926 resigned. -dshaw */
1928 if (node->pkt->pkttype == PKT_USER_ID
1929 && !node->pkt->pkt.user_id->is_revoked
1930 && !node->pkt->pkt.user_id->is_expired)
1932 if (uidnode && issigned)
1934 if (uid->help_full_count >= opt.completes_needed
1935 || uid->help_marginal_count >= opt.marginals_needed )
1936 uidnode->flag |= 4;
1937 else if (uid->help_full_count || uid->help_marginal_count)
1938 uidnode->flag |= 2;
1939 uidnode->flag |= 1;
1940 any_signed = 1;
1942 uidnode = node;
1943 uid=uidnode->pkt->pkt.user_id;
1945 /* If the selfsig is going to expire... */
1946 if(uid->expiredate && uid->expiredate<*next_expire)
1947 *next_expire = uid->expiredate;
1949 issigned = 0;
1950 get_validity_counts(pk,uid);
1951 mark_usable_uid_certs (kb, uidnode, main_kid, klist,
1952 curtime, next_expire);
1954 else if (node->pkt->pkttype == PKT_SIGNATURE
1955 && (node->flag & (1<<8)) && uid)
1957 /* Note that we are only seeing unrevoked sigs here */
1958 PKT_signature *sig = node->pkt->pkt.signature;
1960 kr = is_in_klist (klist, sig);
1961 /* If the trust_regexp does not match, it's as if the sig
1962 did not exist. This is safe for non-trust sigs as well
1963 since we don't accept a regexp on the sig unless it's a
1964 trust sig. */
1965 if (kr && (!kr->trust_regexp
1966 || opt.trust_model != TM_PGP
1967 || (uidnode
1968 && check_regexp(kr->trust_regexp,
1969 uidnode->pkt->pkt.user_id->name))))
1971 /* Are we part of a trust sig chain? We always favor
1972 the latest trust sig, rather than the greater or
1973 lesser trust sig or value. I could make a decent
1974 argument for any of these cases, but this seems to be
1975 what PGP does, and I'd like to be compatible. -dms */
1976 if (opt.trust_model == TM_PGP
1977 && sig->trust_depth
1978 && pk->trust_timestamp <= sig->timestamp)
1980 unsigned char depth;
1982 /* If the depth on the signature is less than the
1983 chain currently has, then use the signature depth
1984 so we don't increase the depth beyond what the
1985 signer wanted. If the depth on the signature is
1986 more than the chain currently has, then use the
1987 chain depth so we use as much of the signature
1988 depth as the chain will permit. An ultimately
1989 trusted signature can restart the depth to
1990 whatever level it likes. */
1992 if (sig->trust_depth < kr->trust_depth
1993 || kr->ownertrust == TRUST_ULTIMATE)
1994 depth = sig->trust_depth;
1995 else
1996 depth = kr->trust_depth;
1998 if (depth)
2000 if(DBG_TRUST)
2001 log_debug ("trust sig on %s, sig depth is %d,"
2002 " kr depth is %d\n",
2003 uidnode->pkt->pkt.user_id->name,
2004 sig->trust_depth,
2005 kr->trust_depth);
2007 /* If we got here, we know that:
2009 this is a trust sig.
2011 it's a newer trust sig than any previous trust
2012 sig on this key (not uid).
2014 it is legal in that it was either generated by an
2015 ultimate key, or a key that was part of a trust
2016 chain, and the depth does not violate the
2017 original trust sig.
2019 if there is a regexp attached, it matched
2020 successfully.
2023 if (DBG_TRUST)
2024 log_debug ("replacing trust value %d with %d and "
2025 "depth %d with %d\n",
2026 pk->trust_value,sig->trust_value,
2027 pk->trust_depth,depth);
2029 pk->trust_value = sig->trust_value;
2030 pk->trust_depth = depth-1;
2032 /* If the trust sig contains a regexp, record it
2033 on the pk for the next round. */
2034 if (sig->trust_regexp)
2035 pk->trust_regexp = sig->trust_regexp;
2039 if (kr->ownertrust == TRUST_ULTIMATE)
2040 uid->help_full_count = opt.completes_needed;
2041 else if (kr->ownertrust == TRUST_FULLY)
2042 uid->help_full_count++;
2043 else if (kr->ownertrust == TRUST_MARGINAL)
2044 uid->help_marginal_count++;
2045 issigned = 1;
2050 if (uidnode && issigned)
2052 if (uid->help_full_count >= opt.completes_needed
2053 || uid->help_marginal_count >= opt.marginals_needed )
2054 uidnode->flag |= 4;
2055 else if (uid->help_full_count || uid->help_marginal_count)
2056 uidnode->flag |= 2;
2057 uidnode->flag |= 1;
2058 any_signed = 1;
2061 return any_signed;
2065 static int
2066 search_skipfnc (void *opaque, u32 *kid, PKT_user_id *dummy)
2068 (void)dummy;
2069 return test_key_hash_table ((KeyHashTable)opaque, kid);
2074 * Scan all keys and return a key_array of all suitable keys from
2075 * kllist. The caller has to pass keydb handle so that we don't use
2076 * to create our own. Returns either a key_array or NULL in case of
2077 * an error. No results found are indicated by an empty array.
2078 * Caller hast to release the returned array.
2080 static struct key_array *
2081 validate_key_list (KEYDB_HANDLE hd, KeyHashTable full_trust,
2082 struct key_item *klist, u32 curtime, u32 *next_expire)
2084 KBNODE keyblock = NULL;
2085 struct key_array *keys = NULL;
2086 size_t nkeys, maxkeys;
2087 int rc;
2088 KEYDB_SEARCH_DESC desc;
2090 maxkeys = 1000;
2091 keys = xmalloc ((maxkeys+1) * sizeof *keys);
2092 nkeys = 0;
2094 rc = keydb_search_reset (hd);
2095 if (rc)
2097 log_error ("keydb_search_reset failed: %s\n", g10_errstr(rc));
2098 xfree (keys);
2099 return NULL;
2102 memset (&desc, 0, sizeof desc);
2103 desc.mode = KEYDB_SEARCH_MODE_FIRST;
2104 desc.skipfnc = search_skipfnc;
2105 desc.skipfncvalue = full_trust;
2106 rc = keydb_search (hd, &desc, 1);
2107 if (rc == -1)
2109 keys[nkeys].keyblock = NULL;
2110 return keys;
2112 if (rc)
2114 log_error ("keydb_search_first failed: %s\n", g10_errstr(rc));
2115 xfree (keys);
2116 return NULL;
2119 desc.mode = KEYDB_SEARCH_MODE_NEXT; /* change mode */
2122 PKT_public_key *pk;
2124 rc = keydb_get_keyblock (hd, &keyblock);
2125 if (rc)
2127 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc));
2128 xfree (keys);
2129 return NULL;
2132 if ( keyblock->pkt->pkttype != PKT_PUBLIC_KEY)
2134 log_debug ("ooops: invalid pkttype %d encountered\n",
2135 keyblock->pkt->pkttype);
2136 dump_kbnode (keyblock);
2137 release_kbnode(keyblock);
2138 continue;
2141 /* prepare the keyblock for further processing */
2142 merge_keys_and_selfsig (keyblock);
2143 clear_kbnode_flags (keyblock);
2144 pk = keyblock->pkt->pkt.public_key;
2145 if (pk->has_expired || pk->is_revoked)
2147 /* it does not make sense to look further at those keys */
2148 mark_keyblock_seen (full_trust, keyblock);
2150 else if (validate_one_keyblock (keyblock, klist, curtime, next_expire))
2152 KBNODE node;
2154 if (pk->expiredate && pk->expiredate >= curtime
2155 && pk->expiredate < *next_expire)
2156 *next_expire = pk->expiredate;
2158 if (nkeys == maxkeys) {
2159 maxkeys += 1000;
2160 keys = xrealloc (keys, (maxkeys+1) * sizeof *keys);
2162 keys[nkeys++].keyblock = keyblock;
2164 /* Optimization - if all uids are fully trusted, then we
2165 never need to consider this key as a candidate again. */
2167 for (node=keyblock; node; node = node->next)
2168 if (node->pkt->pkttype == PKT_USER_ID && !(node->flag & 4))
2169 break;
2171 if(node==NULL)
2172 mark_keyblock_seen (full_trust, keyblock);
2174 keyblock = NULL;
2177 release_kbnode (keyblock);
2178 keyblock = NULL;
2180 while ( !(rc = keydb_search (hd, &desc, 1)) );
2181 if (rc && rc != -1)
2183 log_error ("keydb_search_next failed: %s\n", g10_errstr(rc));
2184 xfree (keys);
2185 return NULL;
2188 keys[nkeys].keyblock = NULL;
2189 return keys;
2192 /* Caller must sync */
2193 static void
2194 reset_trust_records(void)
2196 TRUSTREC rec;
2197 ulong recnum;
2198 int count = 0, nreset = 0;
2200 for (recnum=1; !tdbio_read_record (recnum, &rec, 0); recnum++ )
2202 if(rec.rectype==RECTYPE_TRUST)
2204 count++;
2205 if(rec.r.trust.min_ownertrust)
2207 rec.r.trust.min_ownertrust=0;
2208 write_record(&rec);
2212 else if(rec.rectype==RECTYPE_VALID
2213 && ((rec.r.valid.validity&TRUST_MASK)
2214 || rec.r.valid.marginal_count
2215 || rec.r.valid.full_count))
2217 rec.r.valid.validity &= ~TRUST_MASK;
2218 rec.r.valid.marginal_count=rec.r.valid.full_count=0;
2219 nreset++;
2220 write_record(&rec);
2225 if (opt.verbose)
2226 log_info (_("%d keys processed (%d validity counts cleared)\n"),
2227 count, nreset);
2231 * Run the key validation procedure.
2233 * This works this way:
2234 * Step 1: Find all ultimately trusted keys (UTK).
2235 * mark them all as seen and put them into klist.
2236 * Step 2: loop max_cert_times
2237 * Step 3: if OWNERTRUST of any key in klist is undefined
2238 * ask user to assign ownertrust
2239 * Step 4: Loop over all keys in the keyDB which are not marked seen
2240 * Step 5: if key is revoked or expired
2241 * mark key as seen
2242 * continue loop at Step 4
2243 * Step 6: For each user ID of that key signed by a key in klist
2244 * Calculate validity by counting trusted signatures.
2245 * Set validity of user ID
2246 * Step 7: If any signed user ID was found
2247 * mark key as seen
2248 * End Loop
2249 * Step 8: Build a new klist from all fully trusted keys from step 6
2250 * End Loop
2251 * Ready
2254 static int
2255 validate_keys (int interactive)
2257 int rc = 0;
2258 int quit=0;
2259 struct key_item *klist = NULL;
2260 struct key_item *k;
2261 struct key_array *keys = NULL;
2262 struct key_array *kar;
2263 KEYDB_HANDLE kdb = NULL;
2264 KBNODE node;
2265 int depth;
2266 int ot_unknown, ot_undefined, ot_never, ot_marginal, ot_full, ot_ultimate;
2267 KeyHashTable stored,used,full_trust;
2268 u32 start_time, next_expire;
2270 /* Make sure we have all sigs cached. TODO: This is going to
2271 require some architectual re-thinking, as it is agonizingly slow.
2272 Perhaps combine this with reset_trust_records(), or only check
2273 the caches on keys that are actually involved in the web of
2274 trust. */
2275 keydb_rebuild_caches(0);
2277 start_time = make_timestamp ();
2278 next_expire = 0xffffffff; /* set next expire to the year 2106 */
2279 stored = new_key_hash_table ();
2280 used = new_key_hash_table ();
2281 full_trust = new_key_hash_table ();
2283 kdb = keydb_new (0);
2284 reset_trust_records();
2286 /* Fixme: Instead of always building a UTK list, we could just build it
2287 * here when needed */
2288 if (!utk_list)
2290 if (!opt.quiet)
2291 log_info (_("no ultimately trusted keys found\n"));
2292 goto leave;
2295 /* mark all UTKs as used and fully_trusted and set validity to
2296 ultimate */
2297 for (k=utk_list; k; k = k->next)
2299 KBNODE keyblock;
2300 PKT_public_key *pk;
2302 keyblock = get_pubkeyblock (k->kid);
2303 if (!keyblock)
2305 log_error (_("public key of ultimately"
2306 " trusted key %s not found\n"), keystr(k->kid));
2307 continue;
2309 mark_keyblock_seen (used, keyblock);
2310 mark_keyblock_seen (stored, keyblock);
2311 mark_keyblock_seen (full_trust, keyblock);
2312 pk = keyblock->pkt->pkt.public_key;
2313 for (node=keyblock; node; node = node->next)
2315 if (node->pkt->pkttype == PKT_USER_ID)
2316 update_validity (pk, node->pkt->pkt.user_id, 0, TRUST_ULTIMATE);
2318 if ( pk->expiredate && pk->expiredate >= start_time
2319 && pk->expiredate < next_expire)
2320 next_expire = pk->expiredate;
2322 release_kbnode (keyblock);
2323 do_sync ();
2326 klist = utk_list;
2328 log_info(_("%d marginal(s) needed, %d complete(s) needed, %s trust model\n"),
2329 opt.marginals_needed,opt.completes_needed,trust_model_string());
2331 for (depth=0; depth < opt.max_cert_depth; depth++)
2333 int valids=0,key_count;
2334 /* See whether we should assign ownertrust values to the keys in
2335 klist. */
2336 ot_unknown = ot_undefined = ot_never = 0;
2337 ot_marginal = ot_full = ot_ultimate = 0;
2338 for (k=klist; k; k = k->next)
2340 int min=0;
2342 /* 120 and 60 are as per RFC2440 */
2343 if(k->trust_value>=120)
2344 min=TRUST_FULLY;
2345 else if(k->trust_value>=60)
2346 min=TRUST_MARGINAL;
2348 if(min!=k->min_ownertrust)
2349 update_min_ownertrust(k->kid,min);
2351 if (interactive && k->ownertrust == TRUST_UNKNOWN)
2353 k->ownertrust = ask_ownertrust (k->kid,min);
2355 if (k->ownertrust == -1)
2357 quit=1;
2358 goto leave;
2362 /* This can happen during transition from an old trustdb
2363 before trust sigs. It can also happen if a user uses two
2364 different versions of GnuPG or changes the --trust-model
2365 setting. */
2366 if(k->ownertrust<min)
2368 if(DBG_TRUST)
2369 log_debug("key %08lX%08lX:"
2370 " overriding ownertrust `%s' with `%s'\n",
2371 (ulong)k->kid[0],(ulong)k->kid[1],
2372 trust_value_to_string(k->ownertrust),
2373 trust_value_to_string(min));
2375 k->ownertrust=min;
2378 if (k->ownertrust == TRUST_UNKNOWN)
2379 ot_unknown++;
2380 else if (k->ownertrust == TRUST_UNDEFINED)
2381 ot_undefined++;
2382 else if (k->ownertrust == TRUST_NEVER)
2383 ot_never++;
2384 else if (k->ownertrust == TRUST_MARGINAL)
2385 ot_marginal++;
2386 else if (k->ownertrust == TRUST_FULLY)
2387 ot_full++;
2388 else if (k->ownertrust == TRUST_ULTIMATE)
2389 ot_ultimate++;
2391 valids++;
2394 /* Find all keys which are signed by a key in kdlist */
2395 keys = validate_key_list (kdb, full_trust, klist,
2396 start_time, &next_expire);
2397 if (!keys)
2399 log_error ("validate_key_list failed\n");
2400 rc = G10ERR_GENERAL;
2401 goto leave;
2404 for (key_count=0, kar=keys; kar->keyblock; kar++, key_count++)
2407 /* Store the calculated valididation status somewhere */
2408 if (opt.verbose > 1)
2409 dump_key_array (depth, keys);
2411 for (kar=keys; kar->keyblock; kar++)
2412 store_validation_status (depth, kar->keyblock, stored);
2414 log_info (_("depth: %d valid: %3d signed: %3d"
2415 " trust: %d-, %dq, %dn, %dm, %df, %du\n"),
2416 depth, valids, key_count, ot_unknown, ot_undefined,
2417 ot_never, ot_marginal, ot_full, ot_ultimate );
2419 /* Build a new kdlist from all fully valid keys in KEYS */
2420 if (klist != utk_list)
2421 release_key_items (klist);
2422 klist = NULL;
2423 for (kar=keys; kar->keyblock; kar++)
2425 for (node=kar->keyblock; node; node = node->next)
2427 if (node->pkt->pkttype == PKT_USER_ID && (node->flag & 4))
2429 u32 kid[2];
2431 /* have we used this key already? */
2432 keyid_from_pk (kar->keyblock->pkt->pkt.public_key, kid);
2433 if(test_key_hash_table(used,kid)==0)
2435 /* Normally we add both the primary and subkey
2436 ids to the hash via mark_keyblock_seen, but
2437 since we aren't using this hash as a skipfnc,
2438 that doesn't matter here. */
2439 add_key_hash_table (used,kid);
2440 k = new_key_item ();
2441 k->kid[0]=kid[0];
2442 k->kid[1]=kid[1];
2443 k->ownertrust =
2444 (get_ownertrust (kar->keyblock->pkt->pkt.public_key)
2445 & TRUST_MASK);
2446 k->min_ownertrust =
2447 get_min_ownertrust(kar->keyblock->pkt->pkt.public_key);
2448 k->trust_depth=
2449 kar->keyblock->pkt->pkt.public_key->trust_depth;
2450 k->trust_value=
2451 kar->keyblock->pkt->pkt.public_key->trust_value;
2452 if(kar->keyblock->pkt->pkt.public_key->trust_regexp)
2453 k->trust_regexp=
2454 xstrdup(kar->keyblock->pkt->
2455 pkt.public_key->trust_regexp);
2456 k->next = klist;
2457 klist = k;
2458 break;
2463 release_key_array (keys);
2464 keys = NULL;
2465 if (!klist)
2466 break; /* no need to dive in deeper */
2469 leave:
2470 keydb_release (kdb);
2471 release_key_array (keys);
2472 release_key_items (klist);
2473 release_key_hash_table (full_trust);
2474 release_key_hash_table (used);
2475 release_key_hash_table (stored);
2476 if (!rc && !quit) /* mark trustDB as checked */
2478 if (next_expire == 0xffffffff || next_expire < start_time )
2479 tdbio_write_nextcheck (0);
2480 else
2482 tdbio_write_nextcheck (next_expire);
2483 log_info (_("next trustdb check due at %s\n"),
2484 strtimestamp (next_expire));
2487 if(tdbio_update_version_record()!=0)
2489 log_error(_("unable to update trustdb version record: "
2490 "write failed: %s\n"), g10_errstr(rc));
2491 tdbio_invalid();
2494 do_sync ();
2495 pending_check_trustdb = 0;
2498 return rc;