gpg: Prepare some key cleaning function for use with secret key packets.
[gnupg.git] / g10 / trustdb.c
index 527a23d..c113b7e 100644 (file)
@@ -15,7 +15,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
  */
 
 #include <config.h>
@@ -36,6 +36,7 @@
 #include "options.h"
 #include "packet.h"
 #include "main.h"
+#include "mbox-util.h"
 #include "i18n.h"
 #include "tdbio.h"
 #include "trustdb.h"
@@ -323,6 +324,13 @@ tdb_keyid_is_utk (u32 *kid)
 
   return 0;
 }
+
+/* Return the list of ultimately trusted keys.  */
+struct key_item *
+tdb_utks (void)
+{
+  return utk_list;
+}
 \f
 /*********************************************
  *********** TrustDB stuff *******************
@@ -975,26 +983,39 @@ tdb_check_trustdb_stale (ctrl_t ctrl)
 }
 
 /*
- * Return the validity information for PK.  This is the core of
- * get_validity.  If SIG is not NULL, then the trust is being
- * evaluated in the context of the provided signature.  This is used
- * by the TOFU code to record statistics.
+ * Return the validity information for KB/PK (at least one of them
+ * must be non-NULL).  This is the core of get_validity.  If SIG is
+ * not NULL, then the trust is being evaluated in the context of the
+ * provided signature.  This is used by the TOFU code to record
+ * statistics.
  */
 unsigned int
 tdb_get_validity_core (ctrl_t ctrl,
+                       kbnode_t kb,
                        PKT_public_key *pk, PKT_user_id *uid,
                        PKT_public_key *main_pk,
                       PKT_signature *sig,
                       int may_ask)
 {
   TRUSTREC trec, vrec;
-  gpg_error_t err;
+  gpg_error_t err = 0;
   ulong recno;
 #ifdef USE_TOFU
   unsigned int tofu_validity = TRUST_UNKNOWN;
+  int free_kb = 0;
 #endif
   unsigned int validity = TRUST_UNKNOWN;
 
+  if (kb && pk)
+    log_assert (keyid_cmp (pk_main_keyid (pk),
+                           pk_main_keyid (kb->pkt->pkt.public_key)) == 0);
+
+  if (! pk)
+    {
+      log_assert (kb);
+      pk = kb->pkt->pkt.public_key;
+    }
+
 #ifndef USE_TOFU
   (void)sig;
   (void)may_ask;
@@ -1022,29 +1043,64 @@ tdb_get_validity_core (ctrl_t ctrl,
 #ifdef USE_TOFU
   if (opt.trust_model == TM_TOFU || opt.trust_model == TM_TOFU_PGP)
     {
-      kbnode_t user_id_node = NULL; /* Silence -Wmaybe-uninitialized.  */
-      int user_ids = 0;
-      int user_ids_expired = 0;
+      kbnode_t n = NULL;
+      strlist_t user_id_list = NULL;
+      int done = 0;
 
-      /* If the caller didn't supply a user id then iterate over all
-        uids.  */
+      /* If the caller didn't supply a user id then use all uids.  */
       if (! uid)
-       user_id_node = get_pubkeyblock (main_pk->keyid);
+        {
+          if (! kb)
+            {
+              kb = get_pubkeyblock (main_pk->keyid);
+              free_kb = 1;
+            }
+          n = kb;
+        }
+
+      if (DBG_TRUST && sig && sig->signers_uid)
+        log_debug ("TOFU: only considering user id: '%s'\n",
+                   sig->signers_uid);
 
-      while (uid
-            || (user_id_node = find_next_kbnode (user_id_node, PKT_USER_ID)))
+      while (!done && (uid || (n = find_next_kbnode (n, PKT_USER_ID))))
        {
-         unsigned int tl;
          PKT_user_id *user_id;
+          int expired = 0;
 
          if (uid)
-           user_id = uid;
+            {
+              user_id = uid;
+              /* If the caller specified a user id, then we only
+                 process the specified user id and are done after the
+                 first iteration.  */
+              done = 1;
+            }
          else
-           user_id = user_id_node->pkt->pkt.user_id;
+           user_id = n->pkt->pkt.user_id;
+
+          if (user_id->attrib_data)
+            /* Skip user attributes.  */
+            continue;
+
+          if (sig && sig->signers_uid)
+            /* Make sure the UID matches.  */
+            {
+              char *email = mailbox_from_userid (user_id->name);
+              if (!email || !*email || strcmp (sig->signers_uid, email) != 0)
+                {
+                  if (DBG_TRUST)
+                    log_debug ("TOFU: skipping user id '%s', which does"
+                               " not match the signer's email ('%s')\n",
+                               email, sig->signers_uid);
+                  xfree (email);
+                  continue;
+                }
+              xfree (email);
+            }
 
           /* If the user id is revoked or expired, then skip it.  */
-         if (user_id->is_revoked || user_id->is_expired)
-           {
+          if (user_id->is_revoked || user_id->is_expired)
+            {
               if (DBG_TRUST)
                 {
                   char *s;
@@ -1059,41 +1115,44 @@ tdb_get_validity_core (ctrl_t ctrl,
                              s, user_id->name);
                 }
 
-             continue;
-           }
+              if (user_id->is_revoked)
+                continue;
 
-         user_ids ++;
+              expired = 1;
+            }
 
-         if (sig)
-           tl = tofu_register (ctrl, main_pk, user_id->name,
-                               sig->digest, sig->digest_len,
-                               sig->timestamp, "unknown",
-                               may_ask);
-         else
-           tl = tofu_get_validity (ctrl, main_pk, user_id->name, may_ask);
-
-         if (tl == TRUST_EXPIRED)
-           user_ids_expired ++;
-         else if (tl == TRUST_UNDEFINED || tl == TRUST_UNKNOWN)
-           ;
-         else if (tl == TRUST_NEVER)
-           tofu_validity = TRUST_NEVER;
-         else
-           {
-             log_assert (tl == TRUST_MARGINAL
-                          || tl == TRUST_FULLY
-                          || tl == TRUST_ULTIMATE);
+          add_to_strlist (&user_id_list, user_id->name);
+          user_id_list->flags = expired;
+        }
 
-             if (tl > tofu_validity)
-               /* XXX: We we really want the max?  */
-               tofu_validity = tl;
-           }
+      /* Process the user ids in the order they appear in the key
+         block.  */
+      strlist_rev (&user_id_list);
 
-         if (uid)
-           /* If the caller specified a user id, then we stop
-              now.  */
-           break;
-       }
+      /* It only makes sense to observe any signature before getting
+         the validity.  This is because if the current signature
+         results in a conflict, then we damn well want to take that
+         into account.  */
+      if (sig)
+        {
+          err = tofu_register_signature (ctrl, main_pk, user_id_list,
+                                         sig->digest, sig->digest_len,
+                                         sig->timestamp, "unknown");
+          if (err)
+            {
+              log_error ("TOFU: error registering signature: %s\n",
+                         gpg_strerror (err));
+
+              tofu_validity = TRUST_UNKNOWN;
+            }
+        }
+      if (! err)
+        tofu_validity = tofu_get_validity (ctrl, main_pk, user_id_list,
+                                           may_ask);
+
+      free_strlist (user_id_list);
+      if (free_kb)
+        release_kbnode (kb);
     }
 #endif /*USE_TOFU*/