1 /* import.c - import a key into our key storage.
2 * Copyright (C) 1998-2007, 2010-2011 Free Software Foundation, Inc.
3 * Copyright (C) 2014 Werner Koch
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/>.
39 #include "keyserver-internal.h"
40 #include "call-agent.h"
41 #include "../common/membuf.h"
54 ulong secret_imported;
56 ulong skipped_new_keys;
60 ulong v3keys; /* Number of V3 keys seen. */
64 static int import (ctrl_t ctrl,
65 IOBUF inp, const char* fname, struct stats_s *stats,
66 unsigned char **fpr, size_t *fpr_len, unsigned int options,
67 import_screener_t screener, void *screener_arg);
68 static int read_block (IOBUF a, PACKET **pending_pkt, kbnode_t *ret_root,
70 static void revocation_present (ctrl_t ctrl, kbnode_t keyblock);
71 static int import_one (ctrl_t ctrl,
72 const char *fname, kbnode_t keyblock,struct stats_s *stats,
73 unsigned char **fpr, size_t *fpr_len,
74 unsigned int options, int from_sk, int silent,
75 import_screener_t screener, void *screener_arg);
76 static int import_secret_one (ctrl_t ctrl, const char *fname, kbnode_t keyblock,
77 struct stats_s *stats, int batch,
78 unsigned int options, int for_migration,
79 import_screener_t screener, void *screener_arg);
80 static int import_revoke_cert( const char *fname, kbnode_t node,
81 struct stats_s *stats);
82 static int chk_self_sigs (const char *fname, kbnode_t keyblock,
83 PKT_public_key *pk, u32 *keyid, int *non_self );
84 static int delete_inv_parts (const char *fname, kbnode_t keyblock,
85 u32 *keyid, unsigned int options );
86 static int merge_blocks (const char *fname, kbnode_t keyblock_orig,
87 kbnode_t keyblock, u32 *keyid,
88 int *n_uids, int *n_sigs, int *n_subk );
89 static int append_uid (kbnode_t keyblock, kbnode_t node, int *n_sigs,
90 const char *fname, u32 *keyid );
91 static int append_key (kbnode_t keyblock, kbnode_t node, int *n_sigs,
92 const char *fname, u32 *keyid );
93 static int merge_sigs (kbnode_t dst, kbnode_t src, int *n_sigs,
94 const char *fname, u32 *keyid );
95 static int merge_keysigs (kbnode_t dst, kbnode_t src, int *n_sigs,
96 const char *fname, u32 *keyid );
99 parse_import_options(char *str,unsigned int *options,int noisy)
101 struct parse_options import_opts[]=
103 {"import-local-sigs",IMPORT_LOCAL_SIGS,NULL,
104 N_("import signatures that are marked as local-only")},
106 {"repair-pks-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG,NULL,
107 N_("repair damage from the pks keyserver during import")},
109 {"keep-ownertrust", IMPORT_KEEP_OWNERTTRUST, NULL,
110 N_("do not clear the ownertrust values during import")},
112 {"fast-import",IMPORT_FAST,NULL,
113 N_("do not update the trustdb after import")},
115 {"merge-only",IMPORT_MERGE_ONLY,NULL,
116 N_("only accept updates to existing keys")},
118 {"import-clean",IMPORT_CLEAN,NULL,
119 N_("remove unusable parts from key after import")},
121 {"import-minimal",IMPORT_MINIMAL|IMPORT_CLEAN,NULL,
122 N_("remove as much as possible from key after import")},
124 /* Aliases for backward compatibility */
125 {"allow-local-sigs",IMPORT_LOCAL_SIGS,NULL,NULL},
126 {"repair-hkp-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG,NULL,NULL},
128 {"import-unusable-sigs",0,NULL,NULL},
129 {"import-clean-sigs",0,NULL,NULL},
130 {"import-clean-uids",0,NULL,NULL},
131 {"convert-sk-to-pk",0, NULL,NULL}, /* Not anymore needed due to
136 return parse_options(str,options,import_opts,noisy);
141 import_new_stats_handle (void)
143 return xmalloc_clear ( sizeof (struct stats_s) );
148 import_release_stats_handle (void *p)
155 * Import the public keys from the given filename. Input may be armored.
156 * This function rejects all keys which are not validly self signed on at
157 * least one userid. Only user ids which are self signed will be imported.
158 * Other signatures are not checked.
160 * Actually this function does a merge. It works like this:
163 * - check self-signatures and remove all userids and their signatures
164 * without/invalid self-signatures.
165 * - reject the keyblock, if we have no valid userid.
166 * - See whether we have this key already in one of our pubrings.
167 * If not, simply add it to the default keyring.
168 * - Compare the key and the self-signatures of the new and the one in
169 * our keyring. If they are different something weird is going on;
171 * - See whether we have only non-self-signature on one user id; if not
172 * ask the user what to do.
173 * - compare the signatures: If we already have this signature, check
174 * that they compare okay; if not, issue a warning and ask the user.
175 * (consider looking at the timestamp and use the newest?)
176 * - Simply add the signature. Can't verify here because we may not have
177 * the signature's public key yet; verification is done when putting it
178 * into the trustdb, which is done automagically as soon as this pubkey
180 * - Proceed with next signature.
182 * Key revocation certificates have special handling.
185 import_keys_internal (ctrl_t ctrl, iobuf_t inp, char **fnames, int nnames,
186 void *stats_handle, unsigned char **fpr, size_t *fpr_len,
187 unsigned int options,
188 import_screener_t screener, void *screener_arg)
192 struct stats_s *stats = stats_handle;
195 stats = import_new_stats_handle ();
199 rc = import (ctrl, inp, "[stream]", stats, fpr, fpr_len, options,
200 screener, screener_arg);
204 if (!fnames && !nnames)
205 nnames = 1; /* Ohh what a ugly hack to jump into the loop */
207 for (i=0; i < nnames; i++)
209 const char *fname = fnames? fnames[i] : NULL;
210 IOBUF inp2 = iobuf_open(fname);
214 if (inp2 && is_secured_file (iobuf_get_fd (inp2)))
218 gpg_err_set_errno (EPERM);
221 log_error (_("can't open '%s': %s\n"), fname, strerror (errno));
224 rc = import (ctrl, inp2, fname, stats, fpr, fpr_len, options,
225 screener, screener_arg);
227 /* Must invalidate that ugly cache to actually close it. */
228 iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)fname);
230 log_error ("import from '%s' failed: %s\n",
231 fname, gpg_strerror (rc) );
240 import_print_stats (stats);
241 import_release_stats_handle (stats);
244 /* If no fast import and the trustdb is dirty (i.e. we added a key
245 or userID that had something other than a selfsig, a signature
246 that was other than a selfsig, or any revocation), then
247 update/check the trustdb if the user specified by setting
248 interactive or by not setting no-auto-check-trustdb */
250 if (!(options & IMPORT_FAST))
251 check_or_update_trustdb ();
258 import_keys (ctrl_t ctrl, char **fnames, int nnames,
259 void *stats_handle, unsigned int options )
261 import_keys_internal (ctrl, NULL, fnames, nnames, stats_handle,
262 NULL, NULL, options, NULL, NULL);
266 import_keys_stream (ctrl_t ctrl, IOBUF inp, void *stats_handle,
267 unsigned char **fpr, size_t *fpr_len, unsigned int options)
269 return import_keys_internal (ctrl, inp, NULL, 0, stats_handle,
270 fpr, fpr_len, options, NULL, NULL);
274 /* Variant of import_keys_stream reading from an estream_t. */
276 import_keys_es_stream (ctrl_t ctrl, estream_t fp, void *stats_handle,
277 unsigned char **fpr, size_t *fpr_len,
278 unsigned int options,
279 import_screener_t screener, void *screener_arg)
284 inp = iobuf_esopen (fp, "r", 1);
287 rc = gpg_error_from_syserror ();
288 log_error ("iobuf_esopen failed: %s\n", gpg_strerror (rc));
292 rc = import_keys_internal (ctrl, inp, NULL, 0, stats_handle,
293 fpr, fpr_len, options,
294 screener, screener_arg);
302 import (ctrl_t ctrl, IOBUF inp, const char* fname,struct stats_s *stats,
303 unsigned char **fpr,size_t *fpr_len, unsigned int options,
304 import_screener_t screener, void *screener_arg)
306 PACKET *pending_pkt = NULL;
307 kbnode_t keyblock = NULL; /* Need to initialize because gcc can't
308 grasp the return semantics of
313 getkey_disable_caches ();
315 if (!opt.no_armor) /* Armored reading is not disabled. */
317 armor_filter_context_t *afx;
319 afx = new_armor_context ();
320 afx->only_keyblocks = 1;
321 push_armor_filter (afx, inp);
322 release_armor_context (afx);
325 while (!(rc = read_block (inp, &pending_pkt, &keyblock, &v3keys)))
327 stats->v3keys += v3keys;
328 if (keyblock->pkt->pkttype == PKT_PUBLIC_KEY)
329 rc = import_one (ctrl, fname, keyblock,
330 stats, fpr, fpr_len, options, 0, 0,
331 screener, screener_arg);
332 else if (keyblock->pkt->pkttype == PKT_SECRET_KEY)
333 rc = import_secret_one (ctrl, fname, keyblock, stats,
334 opt.batch, options, 0,
335 screener, screener_arg);
336 else if (keyblock->pkt->pkttype == PKT_SIGNATURE
337 && keyblock->pkt->pkt.signature->sig_class == 0x20 )
338 rc = import_revoke_cert( fname, keyblock, stats );
341 log_info (_("skipping block of type %d\n"), keyblock->pkt->pkttype);
343 release_kbnode (keyblock);
345 /* fixme: we should increment the not imported counter but
346 this does only make sense if we keep on going despite of
347 errors. For now we do this only if the imported key is too
349 if (gpg_err_code (rc) == GPG_ERR_TOO_LARGE
350 && gpg_err_source (rc) == GPG_ERR_SOURCE_KEYBOX)
352 stats->not_imported++;
358 if (!(++stats->count % 100) && !opt.quiet)
359 log_info (_("%lu keys processed so far\n"), stats->count );
361 stats->v3keys += v3keys;
364 else if (rc && gpg_err_code (rc) != GPG_ERR_INV_KEYRING)
365 log_error (_("error reading '%s': %s\n"), fname, gpg_strerror (rc));
371 /* Helper to migrate secring.gpg to GnuPG 2.1. */
373 import_old_secring (ctrl_t ctrl, const char *fname)
377 PACKET *pending_pkt = NULL;
378 kbnode_t keyblock = NULL; /* Need to initialize because gcc can't
379 grasp the return semantics of
381 struct stats_s *stats;
384 inp = iobuf_open (fname);
385 if (inp && is_secured_file (iobuf_get_fd (inp)))
389 gpg_err_set_errno (EPERM);
393 err = gpg_error_from_syserror ();
394 log_error (_("can't open '%s': %s\n"), fname, gpg_strerror (err));
398 getkey_disable_caches();
399 stats = import_new_stats_handle ();
400 while (!(err = read_block (inp, &pending_pkt, &keyblock, &v3keys)))
402 if (keyblock->pkt->pkttype == PKT_SECRET_KEY)
403 err = import_secret_one (ctrl, fname, keyblock, stats, 1, 0, 1,
405 release_kbnode (keyblock);
409 import_release_stats_handle (stats);
412 else if (err && gpg_err_code (err) != GPG_ERR_INV_KEYRING)
413 log_error (_("error reading '%s': %s\n"), fname, gpg_strerror (err));
415 log_error ("import from '%s' failed: %s\n", fname, gpg_strerror (err));
418 iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)fname);
425 import_print_stats (void *hd)
427 struct stats_s *stats = hd;
431 log_info(_("Total number processed: %lu\n"),
432 stats->count + stats->v3keys);
434 log_info(_(" skipped PGP-2 keys: %lu\n"), stats->v3keys);
435 if (stats->skipped_new_keys )
436 log_info(_(" skipped new keys: %lu\n"),
437 stats->skipped_new_keys );
438 if (stats->no_user_id )
439 log_info(_(" w/o user IDs: %lu\n"), stats->no_user_id );
442 log_info(_(" imported: %lu"), stats->imported );
445 if (stats->unchanged )
446 log_info(_(" unchanged: %lu\n"), stats->unchanged );
448 log_info(_(" new user IDs: %lu\n"), stats->n_uids );
450 log_info(_(" new subkeys: %lu\n"), stats->n_subk );
452 log_info(_(" new signatures: %lu\n"), stats->n_sigs );
454 log_info(_(" new key revocations: %lu\n"), stats->n_revoc );
455 if (stats->secret_read )
456 log_info(_(" secret keys read: %lu\n"), stats->secret_read );
457 if (stats->secret_imported )
458 log_info(_(" secret keys imported: %lu\n"), stats->secret_imported );
459 if (stats->secret_dups )
460 log_info(_(" secret keys unchanged: %lu\n"), stats->secret_dups );
461 if (stats->not_imported )
462 log_info(_(" not imported: %lu\n"), stats->not_imported );
463 if (stats->n_sigs_cleaned)
464 log_info(_(" signatures cleaned: %lu\n"),stats->n_sigs_cleaned);
465 if (stats->n_uids_cleaned)
466 log_info(_(" user IDs cleaned: %lu\n"),stats->n_uids_cleaned);
469 if (is_status_enabled ())
473 snprintf (buf, sizeof buf,
474 "%lu %lu %lu 0 %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
475 stats->count + stats->v3keys,
484 stats->secret_imported,
486 stats->skipped_new_keys,
489 write_status_text (STATUS_IMPORT_RES, buf);
494 /* Return true if PKTTYPE is valid in a keyblock. */
496 valid_keyblock_packet (int pkttype)
501 case PKT_PUBLIC_SUBKEY:
503 case PKT_SECRET_SUBKEY:
516 * Read the next keyblock from stream A.
517 * PENDING_PKT should be initialzed to NULL
518 * and not changed by the caller.
519 * Return: 0 = okay, -1 no more blocks or another errorcode.
520 * The int at at R_V3KEY counts the number of unsupported v3
524 read_block( IOBUF a, PACKET **pending_pkt, kbnode_t *ret_root, int *r_v3keys)
528 kbnode_t root = NULL;
529 int in_cert, in_v3key;
535 root = new_kbnode( *pending_pkt );
542 pkt = xmalloc (sizeof *pkt);
545 while ((rc=parse_packet(a, pkt)) != -1)
547 if (rc && (gpg_err_code (rc) == GPG_ERR_LEGACY_KEY
548 && (pkt->pkttype == PKT_PUBLIC_KEY
549 || pkt->pkttype == PKT_SECRET_KEY)))
557 else if (rc ) /* (ignore errors) */
559 if (gpg_err_code (rc) == GPG_ERR_UNKNOWN_PACKET)
560 ; /* Do not show a diagnostic. */
563 log_error("read_block: read error: %s\n", gpg_strerror (rc) );
564 rc = GPG_ERR_INV_KEYRING;
572 if (in_v3key && !(pkt->pkttype == PKT_PUBLIC_KEY
573 || pkt->pkttype == PKT_SECRET_KEY))
581 if (!root && pkt->pkttype == PKT_SIGNATURE
582 && pkt->pkt.signature->sig_class == 0x20 )
584 /* This is a revocation certificate which is handled in a
586 root = new_kbnode( pkt );
591 /* Make a linked list of all packets. */
592 switch (pkt->pkttype)
595 if (check_compress_algo (pkt->pkt.compressed->algorithm))
597 rc = GPG_ERR_COMPR_ALGO;
602 compress_filter_context_t *cfx = xmalloc_clear( sizeof *cfx );
603 pkt->pkt.compressed->buf = NULL;
604 push_compress_filter2(a,cfx,pkt->pkt.compressed->algorithm,1);
611 /* Skip those packets. */
618 if (in_cert ) /* Store this packet. */
626 if (in_cert && valid_keyblock_packet (pkt->pkttype))
629 root = new_kbnode (pkt);
631 add_kbnode (root, new_kbnode (pkt));
632 pkt = xmalloc (sizeof *pkt);
640 if (rc == -1 && root )
644 release_kbnode( root );
653 /* Walk through the subkeys on a pk to find if we have the PKS
654 disease: multiple subkeys with their binding sigs stripped, and the
655 sig for the first subkey placed after the last subkey. That is,
656 instead of "pk uid sig sub1 bind1 sub2 bind2 sub3 bind3" we have
657 "pk uid sig sub1 sub2 sub3 bind1". We can't do anything about sub2
658 and sub3, as they are already lost, but we can try and rescue sub1
659 by reordering the keyblock so that it reads "pk uid sig sub1 bind1
660 sub2 sub3". Returns TRUE if the keyblock was modified. */
662 fix_pks_corruption (kbnode_t keyblock)
667 kbnode_t last = NULL;
668 kbnode_t sknode=NULL;
670 /* First determine if we have the problem at all. Look for 2 or
671 more subkeys in a row, followed by a single binding sig. */
672 for (node=keyblock; node; last=node, node=node->next)
674 if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
680 else if (node->pkt->pkttype == PKT_SIGNATURE
681 && node->pkt->pkt.signature->sig_class == 0x18
685 /* We might have the problem, as this key has two subkeys in
686 a row without any intervening packets. */
692 /* Temporarily attach node to sknode. */
693 node->next = sknode->next;
697 /* Note we aren't checking whether this binding sig is a
698 selfsig. This is not necessary here as the subkey and
699 binding sig will be rejected later if that is the
701 if (check_key_signature (keyblock,node,NULL))
703 /* Not a match, so undo the changes. */
704 sknode->next = node->next;
711 sknode->flag |= 1; /* Mark it good so we don't need to
725 /* Versions of GnuPG before 1.4.11 and 2.0.16 allowed to import bogus
726 direct key signatures. A side effect of this was that a later
727 import of the same good direct key signatures was not possible
728 because the cmp_signature check in merge_blocks considered them
729 equal. Although direct key signatures are now checked during
730 import, there might still be bogus signatures sitting in a keyring.
731 We need to detect and delete them before doing a merge. This
732 function returns the number of removed sigs. */
734 fix_bad_direct_key_sigs (kbnode_t keyblock, u32 *keyid)
740 for (node = keyblock->next; node; node=node->next)
742 if (node->pkt->pkttype == PKT_USER_ID)
744 if (node->pkt->pkttype == PKT_SIGNATURE
745 && IS_KEY_SIG (node->pkt->pkt.signature))
747 err = check_key_signature (keyblock, node, NULL);
748 if (err && gpg_err_code (err) != GPG_ERR_PUBKEY_ALGO )
750 /* If we don't know the error, we can't decide; this is
751 not a problem because cmp_signature can't compare the
753 log_info ("key %s: invalid direct key signature removed\n",
755 delete_kbnode (node);
766 print_import_ok (PKT_public_key *pk, unsigned int reason)
768 byte array[MAX_FINGERPRINT_LEN], *s;
769 char buf[MAX_FINGERPRINT_LEN*2+30], *p;
772 snprintf (buf, sizeof buf, "%u ", reason);
773 p = buf + strlen (buf);
775 fingerprint_from_pk (pk, array, &n);
777 for (i=0; i < n ; i++, s++, p += 2)
778 sprintf (p, "%02X", *s);
780 write_status_text (STATUS_IMPORT_OK, buf);
785 print_import_check (PKT_public_key * pk, PKT_user_id * id)
793 buf = xmalloc (17+41+id->len+32);
794 keyid_from_pk (pk, keyid);
795 sprintf (buf, "%08X%08X ", keyid[0], keyid[1]);
797 fingerprint_from_pk (pk, fpr, &n);
798 for (i = 0; i < n; i++, pos += 2)
799 sprintf (buf+pos, "%02X", fpr[i]);
802 strcat (buf, id->name);
803 write_status_text (STATUS_IMPORT_CHECK, buf);
809 check_prefs_warning(PKT_public_key *pk)
811 log_info(_("WARNING: key %s contains preferences for unavailable\n"
812 "algorithms on these user IDs:\n"), keystr_from_pk(pk));
817 check_prefs (ctrl_t ctrl, kbnode_t keyblock)
823 merge_keys_and_selfsig(keyblock);
824 pk=keyblock->pkt->pkt.public_key;
826 for(node=keyblock;node;node=node->next)
828 if(node->pkt->pkttype==PKT_USER_ID
829 && node->pkt->pkt.user_id->created
830 && node->pkt->pkt.user_id->prefs)
832 PKT_user_id *uid = node->pkt->pkt.user_id;
833 prefitem_t *prefs = uid->prefs;
834 char *user = utf8_to_native(uid->name,strlen(uid->name),0);
836 for(;prefs->type;prefs++)
838 char num[10]; /* prefs->value is a byte, so we're over
841 sprintf(num,"%u",prefs->value);
843 if(prefs->type==PREFTYPE_SYM)
845 if (openpgp_cipher_test_algo (prefs->value))
848 (openpgp_cipher_test_algo (prefs->value)
850 : openpgp_cipher_algo_name (prefs->value));
852 check_prefs_warning(pk);
853 log_info(_(" \"%s\": preference for cipher"
854 " algorithm %s\n"), user, algo);
858 else if(prefs->type==PREFTYPE_HASH)
860 if(openpgp_md_test_algo(prefs->value))
863 (gcry_md_test_algo (prefs->value)
865 : gcry_md_algo_name (prefs->value));
867 check_prefs_warning(pk);
868 log_info(_(" \"%s\": preference for digest"
869 " algorithm %s\n"), user, algo);
873 else if(prefs->type==PREFTYPE_ZIP)
875 if(check_compress_algo (prefs->value))
877 const char *algo=compress_algo_to_string(prefs->value);
879 check_prefs_warning(pk);
880 log_info(_(" \"%s\": preference for compression"
881 " algorithm %s\n"),user,algo?algo:num);
893 log_info(_("it is strongly suggested that you update"
894 " your preferences and\n"));
895 log_info(_("re-distribute this key to avoid potential algorithm"
896 " mismatch problems\n"));
901 strlist_t locusr = NULL;
903 byte fpr[MAX_FINGERPRINT_LEN], *p;
904 char username[(MAX_FINGERPRINT_LEN*2)+1];
907 p = fingerprint_from_pk (pk,fpr,&fprlen);
908 for(i=0;i<fprlen;i++,p++)
909 sprintf(username+2*i,"%02X",*p);
910 add_to_strlist(&locusr,username);
912 append_to_strlist(&sl,"updpref");
913 append_to_strlist(&sl,"save");
915 keyedit_menu (ctrl, username, locusr, sl, 1, 1 );
917 free_strlist(locusr);
920 log_info(_("you can update your preferences with:"
921 " gpg --edit-key %s updpref save\n"),keystr_from_pk(pk));
927 * Try to import one keyblock. Return an error only in serious cases,
928 * but never for an invalid keyblock. It uses log_error to increase
929 * the internal errorcount, so that invalid input can be detected by
930 * programs which called gpg. If SILENT is no messages are printed -
931 * even most error messages are suppressed.
934 import_one (ctrl_t ctrl,
935 const char *fname, kbnode_t keyblock, struct stats_s *stats,
936 unsigned char **fpr, size_t *fpr_len, unsigned int options,
937 int from_sk, int silent,
938 import_screener_t screener, void *screener_arg)
941 PKT_public_key *pk_orig;
942 kbnode_t node, uidnode;
943 kbnode_t keyblock_orig = NULL;
944 byte fpr2[MAX_FINGERPRINT_LEN];
953 char pkstrbuf[PUBKEY_STRING_SIZE];
955 /* Get the key and print some info about it. */
956 node = find_kbnode( keyblock, PKT_PUBLIC_KEY );
960 pk = node->pkt->pkt.public_key;
962 fingerprint_from_pk (pk, fpr2, &fpr2len);
963 for (an = fpr2len; an < MAX_FINGERPRINT_LEN; an++)
965 keyid_from_pk( pk, keyid );
966 uidnode = find_next_kbnode( keyblock, PKT_USER_ID );
968 if (opt.verbose && !opt.interactive && !silent)
970 log_info( "pub %s/%s %s ",
971 pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
972 keystr_from_pk(pk), datestr_from_pk(pk) );
974 print_utf8_buffer (log_get_stream (),
975 uidnode->pkt->pkt.user_id->name,
976 uidnode->pkt->pkt.user_id->len );
984 log_error( _("key %s: no user ID\n"), keystr_from_pk(pk));
988 if (screener && screener (keyblock, screener_arg))
990 log_error (_("key %s: %s\n"), keystr_from_pk (pk),
991 _("rejected by import screener"));
995 if (opt.interactive && !silent)
997 if (is_status_enabled())
998 print_import_check (pk, uidnode->pkt->pkt.user_id);
999 merge_keys_and_selfsig (keyblock);
1001 show_basic_key_info (keyblock);
1003 if (!cpr_get_answer_is_yes ("import.okay",
1004 "Do you want to import this key? (y/N) "))
1008 collapse_uids(&keyblock);
1010 /* Clean the key that we're about to import, to cut down on things
1011 that we have to clean later. This has no practical impact on the
1012 end result, but does result in less logging which might confuse
1014 if (options&IMPORT_CLEAN)
1015 clean_key (keyblock,opt.verbose,options&IMPORT_MINIMAL,NULL,NULL);
1017 clear_kbnode_flags( keyblock );
1019 if ((options&IMPORT_REPAIR_PKS_SUBKEY_BUG) && fix_pks_corruption(keyblock)
1021 log_info (_("key %s: PKS subkey corruption repaired\n"),
1022 keystr_from_pk(pk));
1024 rc = chk_self_sigs( fname, keyblock , pk, keyid, &non_self );
1026 return rc== -1? 0:rc;
1028 /* If we allow such a thing, mark unsigned uids as valid */
1029 if (opt.allow_non_selfsigned_uid)
1031 for (node=keyblock; node; node = node->next )
1032 if (node->pkt->pkttype == PKT_USER_ID && !(node->flag & 1) )
1034 char *user=utf8_to_native(node->pkt->pkt.user_id->name,
1035 node->pkt->pkt.user_id->len,0);
1037 log_info( _("key %s: accepted non self-signed user ID \"%s\"\n"),
1038 keystr_from_pk(pk),user);
1043 if (!delete_inv_parts( fname, keyblock, keyid, options ) )
1047 log_error( _("key %s: no valid user IDs\n"), keystr_from_pk(pk));
1049 log_info(_("this may be caused by a missing self-signature\n"));
1051 stats->no_user_id++;
1055 /* Do we have this key already in one of our pubrings ? */
1056 pk_orig = xmalloc_clear( sizeof *pk_orig );
1057 rc = get_pubkey_byfprint_fast (pk_orig, fpr2, fpr2len);
1058 if (rc && gpg_err_code (rc) != GPG_ERR_NO_PUBKEY
1059 && gpg_err_code (rc) != GPG_ERR_UNUSABLE_PUBKEY )
1062 log_error (_("key %s: public key not found: %s\n"),
1063 keystr(keyid), gpg_strerror (rc));
1065 else if ( rc && (opt.import_options&IMPORT_MERGE_ONLY) )
1067 if (opt.verbose && !silent )
1068 log_info( _("key %s: new key - skipped\n"), keystr(keyid));
1070 stats->skipped_new_keys++;
1072 else if (rc ) /* Insert this key. */
1074 KEYDB_HANDLE hd = keydb_new ();
1076 rc = keydb_locate_writable (hd, NULL);
1079 log_error (_("no writable keyring found: %s\n"), gpg_strerror (rc));
1081 return GPG_ERR_GENERAL;
1083 if (opt.verbose > 1 )
1084 log_info (_("writing to '%s'\n"), keydb_get_resource_name (hd) );
1086 rc = keydb_insert_keyblock (hd, keyblock );
1088 log_error (_("error writing keyring '%s': %s\n"),
1089 keydb_get_resource_name (hd), gpg_strerror (rc));
1090 else if (!(opt.import_options & IMPORT_KEEP_OWNERTTRUST))
1092 /* This should not be possible since we delete the
1093 ownertrust when a key is deleted, but it can happen if
1094 the keyring and trustdb are out of sync. It can also
1095 be made to happen with the trusted-key command and by
1096 importing and locally exported key. */
1098 clear_ownertrusts (pk);
1100 revalidation_mark ();
1105 if (!opt.quiet && !silent)
1107 char *p = get_user_id_byfpr_native (fpr2);
1108 log_info (_("key %s: public key \"%s\" imported\n"),
1112 if (is_status_enabled())
1114 char *us = get_long_user_id_string( keyid );
1115 write_status_text( STATUS_IMPORTED, us );
1117 print_import_ok (pk, 1);
1125 int n_uids, n_sigs, n_subk, n_sigs_cleaned, n_uids_cleaned;
1127 /* Compare the original against the new key; just to be sure nothing
1128 * weird is going on */
1129 if (cmp_public_keys( pk_orig, pk ) )
1132 log_error( _("key %s: doesn't match our copy\n"),keystr(keyid));
1136 /* Now read the original keyblock again so that we can use
1137 that handle for updating the keyblock. */
1139 keydb_disable_caching (hd);
1140 rc = keydb_search_fpr (hd, fpr2);
1143 log_error (_("key %s: can't locate original keyblock: %s\n"),
1144 keystr(keyid), gpg_strerror (rc));
1148 rc = keydb_get_keyblock (hd, &keyblock_orig);
1151 log_error (_("key %s: can't read original keyblock: %s\n"),
1152 keystr(keyid), gpg_strerror (rc));
1157 /* Make sure the original direct key sigs are all sane. */
1158 n_sigs_cleaned = fix_bad_direct_key_sigs (keyblock_orig, keyid);
1160 commit_kbnode (&keyblock_orig);
1162 /* and try to merge the block */
1163 clear_kbnode_flags( keyblock_orig );
1164 clear_kbnode_flags( keyblock );
1165 n_uids = n_sigs = n_subk = n_uids_cleaned = 0;
1166 rc = merge_blocks( fname, keyblock_orig, keyblock,
1167 keyid, &n_uids, &n_sigs, &n_subk );
1174 if ((options & IMPORT_CLEAN))
1175 clean_key (keyblock_orig,opt.verbose,options&IMPORT_MINIMAL,
1176 &n_uids_cleaned,&n_sigs_cleaned);
1178 if (n_uids || n_sigs || n_subk || n_sigs_cleaned || n_uids_cleaned)
1181 /* KEYBLOCK_ORIG has been updated; write */
1182 rc = keydb_update_keyblock (hd, keyblock_orig);
1184 log_error (_("error writing keyring '%s': %s\n"),
1185 keydb_get_resource_name (hd), gpg_strerror (rc) );
1187 revalidation_mark ();
1190 if (!opt.quiet && !silent)
1192 char *p = get_user_id_byfpr_native (fpr2);
1194 log_info( _("key %s: \"%s\" 1 new user ID\n"),
1197 log_info( _("key %s: \"%s\" %d new user IDs\n"),
1198 keystr(keyid),p,n_uids);
1200 log_info( _("key %s: \"%s\" 1 new signature\n"),
1203 log_info( _("key %s: \"%s\" %d new signatures\n"),
1204 keystr(keyid), p, n_sigs );
1206 log_info( _("key %s: \"%s\" 1 new subkey\n"),
1209 log_info( _("key %s: \"%s\" %d new subkeys\n"),
1210 keystr(keyid), p, n_subk );
1211 if (n_sigs_cleaned==1)
1212 log_info(_("key %s: \"%s\" %d signature cleaned\n"),
1213 keystr(keyid),p,n_sigs_cleaned);
1214 else if (n_sigs_cleaned)
1215 log_info(_("key %s: \"%s\" %d signatures cleaned\n"),
1216 keystr(keyid),p,n_sigs_cleaned);
1217 if (n_uids_cleaned==1)
1218 log_info(_("key %s: \"%s\" %d user ID cleaned\n"),
1219 keystr(keyid),p,n_uids_cleaned);
1220 else if (n_uids_cleaned)
1221 log_info(_("key %s: \"%s\" %d user IDs cleaned\n"),
1222 keystr(keyid),p,n_uids_cleaned);
1226 stats->n_uids +=n_uids;
1227 stats->n_sigs +=n_sigs;
1228 stats->n_subk +=n_subk;
1229 stats->n_sigs_cleaned +=n_sigs_cleaned;
1230 stats->n_uids_cleaned +=n_uids_cleaned;
1232 if (is_status_enabled () && !silent)
1233 print_import_ok (pk, ((n_uids?2:0)|(n_sigs?4:0)|(n_subk?8:0)));
1238 if (is_status_enabled ())
1239 print_import_ok (pk, 0);
1241 if (!opt.quiet && !silent)
1243 char *p = get_user_id_byfpr_native (fpr2);
1244 log_info( _("key %s: \"%s\" not changed\n"),keystr(keyid),p);
1251 keydb_release (hd); hd = NULL;
1255 if (mod_key || new_key || same_key)
1257 /* A little explanation for this: we fill in the fingerprint
1258 when importing keys as it can be useful to know the
1259 fingerprint in certain keyserver-related cases (a keyserver
1260 asked for a particular name, but the key doesn't have that
1261 name). However, in cases where we're importing more than
1262 one key at a time, we cannot know which key to fingerprint.
1263 In these cases, rather than guessing, we do not
1264 fingerprinting at all, and we must hope the user ID on the
1265 keys are useful. Note that we need to do this for new
1266 keys, merged keys and even for unchanged keys. This is
1267 required because for example the --auto-key-locate feature
1268 may import an already imported key and needs to know the
1269 fingerprint of the key in all cases. */
1273 /* Note that we need to compare against 0 here because
1274 COUNT gets only incremented after returning form this
1277 *fpr = fingerprint_from_pk (pk, NULL, fpr_len);
1283 /* Now that the key is definitely incorporated into the keydb, we
1284 need to check if a designated revocation is present or if the
1285 prefs are not rational so we can warn the user. */
1289 revocation_present (ctrl, keyblock_orig);
1290 if (!from_sk && have_secret_key_with_kid (keyid))
1291 check_prefs (ctrl, keyblock_orig);
1295 revocation_present (ctrl, keyblock);
1296 if (!from_sk && have_secret_key_with_kid (keyid))
1297 check_prefs (ctrl, keyblock);
1300 release_kbnode( keyblock_orig );
1301 free_public_key( pk_orig );
1307 /* Transfer all the secret keys in SEC_KEYBLOCK to the gpg-agent. The
1308 function prints diagnostics and returns an error code. If BATCH is
1309 true the secret keys are stored by gpg-agent in the transfer format
1310 (i.e. no re-protection and aksing for passphrases). */
1312 transfer_secret_keys (ctrl_t ctrl, struct stats_s *stats, kbnode_t sec_keyblock,
1315 gpg_error_t err = 0;
1318 kbnode_t ctx = NULL;
1320 PKT_public_key *main_pk, *pk;
1321 struct seckey_info *ski;
1325 void *format_args[2*PUBKEY_MAX_NSKEY];
1326 gcry_sexp_t skey, prot, tmpsexp;
1327 gcry_sexp_t curve = NULL;
1328 unsigned char *transferkey = NULL;
1329 size_t transferkeylen;
1330 gcry_cipher_hd_t cipherhd = NULL;
1331 unsigned char *wrappedkey = NULL;
1332 size_t wrappedkeylen;
1333 char *cache_nonce = NULL;
1335 /* Get the current KEK. */
1336 err = agent_keywrap_key (ctrl, 0, &kek, &keklen);
1339 log_error ("error getting the KEK: %s\n", gpg_strerror (err));
1343 /* Prepare a cipher context. */
1344 err = gcry_cipher_open (&cipherhd, GCRY_CIPHER_AES128,
1345 GCRY_CIPHER_MODE_AESWRAP, 0);
1347 err = gcry_cipher_setkey (cipherhd, kek, keklen);
1354 while ((node = walk_kbnode (sec_keyblock, &ctx, 0)))
1356 if (node->pkt->pkttype != PKT_SECRET_KEY
1357 && node->pkt->pkttype != PKT_SECRET_SUBKEY)
1359 pk = node->pkt->pkt.public_key;
1363 /* Make sure the keyids are available. */
1364 keyid_from_pk (pk, NULL);
1365 if (node->pkt->pkttype == PKT_SECRET_KEY)
1367 pk->main_keyid[0] = pk->keyid[0];
1368 pk->main_keyid[1] = pk->keyid[1];
1372 pk->main_keyid[0] = main_pk->keyid[0];
1373 pk->main_keyid[1] = main_pk->keyid[1];
1377 ski = pk->seckey_info;
1382 stats->secret_read++;
1384 /* We ignore stub keys. The way we handle them in other parts
1385 of the code is by asking the agent whether any secret key is
1386 available for a given keyblock and then concluding that we
1387 have a secret key; all secret (sub)keys of the keyblock the
1388 agent does not know of are then stub keys. This works also
1389 for card stub keys. The learn command or the card-status
1390 command may be used to check with the agent whether a card
1391 has been inserted and a stub key is in turn generated by the
1393 if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002)
1396 /* Convert our internal secret key object into an S-expression. */
1397 nskey = pubkey_get_nskey (pk->pubkey_algo);
1398 if (!nskey || nskey > PUBKEY_MAX_NSKEY)
1400 err = gpg_error (GPG_ERR_BAD_SECKEY);
1401 log_error ("internal error: %s\n", gpg_strerror (err));
1405 init_membuf (&mbuf, 50);
1406 put_membuf_str (&mbuf, "(skey");
1407 if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA
1408 || pk->pubkey_algo == PUBKEY_ALGO_EDDSA
1409 || pk->pubkey_algo == PUBKEY_ALGO_ECDH)
1412 char *curvestr = openpgp_oid_to_str (pk->pkey[0]);
1414 err = gpg_error_from_syserror ();
1417 const char *curvename = openpgp_oid_to_curve (curvestr, 1);
1418 err = gcry_sexp_build (&curve, NULL, "(curve %s)",
1419 curvename?curvename:curvestr);
1424 /* Append the public key element Q. */
1425 put_membuf_str (&mbuf, " _ %m");
1426 format_args[j++] = pk->pkey + 1;
1428 /* Append the secret key element D. For ECDH we
1429 skip PKEY[2] because this holds the KEK which is
1430 not needed by gpg-agent. */
1431 i = pk->pubkey_algo == PUBKEY_ALGO_ECDH? 3 : 2;
1432 if (gcry_mpi_get_flag (pk->pkey[i], GCRYMPI_FLAG_USER1))
1433 put_membuf_str (&mbuf, " e %m");
1435 put_membuf_str (&mbuf, " _ %m");
1436 format_args[j++] = pk->pkey + i;
1442 /* Standard case for the old (non-ECC) algorithms. */
1443 for (i=j=0; i < nskey; i++)
1446 continue; /* Protected keys only have NPKEY+1 elements. */
1448 if (gcry_mpi_get_flag (pk->pkey[i], GCRYMPI_FLAG_USER1))
1449 put_membuf_str (&mbuf, " e %m");
1451 put_membuf_str (&mbuf, " _ %m");
1452 format_args[j++] = pk->pkey + i;
1455 put_membuf_str (&mbuf, ")");
1456 put_membuf (&mbuf, "", 1);
1458 xfree (get_membuf (&mbuf, NULL));
1461 char *format = get_membuf (&mbuf, NULL);
1463 err = gpg_error_from_syserror ();
1465 err = gcry_sexp_build_array (&skey, NULL, format, format_args);
1470 log_error ("error building skey array: %s\n", gpg_strerror (err));
1474 if (ski->is_protected)
1478 /* Note that the IVLEN may be zero if we are working on a
1479 dummy key. We can't express that in an S-expression and
1480 thus we send dummy data for the IV. */
1481 snprintf (countbuf, sizeof countbuf, "%lu",
1482 (unsigned long)ski->s2k.count);
1483 err = gcry_sexp_build
1485 " (protection %s %s %b %d %s %b %s)\n",
1486 ski->sha1chk? "sha1":"sum",
1487 openpgp_cipher_algo_name (ski->algo),
1488 ski->ivlen? (int)ski->ivlen:1,
1489 ski->ivlen? ski->iv: (const unsigned char*)"X",
1491 openpgp_md_algo_name (ski->s2k.hash_algo),
1492 (int)sizeof (ski->s2k.salt), ski->s2k.salt,
1496 err = gcry_sexp_build (&prot, NULL, " (protection none)\n");
1499 xfree (transferkey);
1502 err = gcry_sexp_build (&tmpsexp, NULL,
1503 "(openpgp-private-key\n"
1510 openpgp_pk_algo_name (pk->pubkey_algo),
1512 (int)(unsigned long)ski->csum, prot);
1513 gcry_sexp_release (skey);
1514 gcry_sexp_release (prot);
1516 err = make_canon_sexp_pad (tmpsexp, 1, &transferkey, &transferkeylen);
1517 gcry_sexp_release (tmpsexp);
1520 log_error ("error building transfer key: %s\n", gpg_strerror (err));
1525 wrappedkeylen = transferkeylen + 8;
1527 wrappedkey = xtrymalloc (wrappedkeylen);
1529 err = gpg_error_from_syserror ();
1531 err = gcry_cipher_encrypt (cipherhd, wrappedkey, wrappedkeylen,
1532 transferkey, transferkeylen);
1535 xfree (transferkey);
1538 /* Send the wrapped key to the agent. */
1540 char *desc = gpg_format_keydesc (pk, FORMAT_KEYDESC_IMPORT, 1);
1541 err = agent_import_key (ctrl, desc, &cache_nonce,
1542 wrappedkey, wrappedkeylen, batch);
1548 log_info (_("key %s: secret key imported\n"),
1549 keystr_from_pk_with_sub (main_pk, pk));
1550 stats->secret_imported++;
1552 else if ( gpg_err_code (err) == GPG_ERR_EEXIST )
1555 log_info (_("key %s: secret key already exists\n"),
1556 keystr_from_pk_with_sub (main_pk, pk));
1558 stats->secret_dups++;
1562 log_error (_("key %s: error sending to agent: %s\n"),
1563 keystr_from_pk_with_sub (main_pk, pk),
1564 gpg_strerror (err));
1565 if (gpg_err_code (err) == GPG_ERR_CANCELED
1566 || gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
1567 break; /* Don't try the other subkeys. */
1572 gcry_sexp_release (curve);
1573 xfree (cache_nonce);
1575 xfree (transferkey);
1576 gcry_cipher_close (cipherhd);
1582 /* Walk a secret keyblock and produce a public keyblock out of it.
1583 Returns a new node or NULL on error. */
1585 sec_to_pub_keyblock (kbnode_t sec_keyblock)
1587 kbnode_t pub_keyblock = NULL;
1588 kbnode_t ctx = NULL;
1589 kbnode_t secnode, pubnode;
1591 while ((secnode = walk_kbnode (sec_keyblock, &ctx, 0)))
1593 if (secnode->pkt->pkttype == PKT_SECRET_KEY
1594 || secnode->pkt->pkttype == PKT_SECRET_SUBKEY)
1596 /* Make a public key. */
1600 pkt = xtrycalloc (1, sizeof *pkt);
1601 pk = pkt? copy_public_key (NULL, secnode->pkt->pkt.public_key): NULL;
1605 release_kbnode (pub_keyblock);
1608 if (secnode->pkt->pkttype == PKT_SECRET_KEY)
1609 pkt->pkttype = PKT_PUBLIC_KEY;
1611 pkt->pkttype = PKT_PUBLIC_SUBKEY;
1612 pkt->pkt.public_key = pk;
1614 pubnode = new_kbnode (pkt);
1618 pubnode = clone_kbnode (secnode);
1622 pub_keyblock = pubnode;
1624 add_kbnode (pub_keyblock, pubnode);
1627 return pub_keyblock;
1631 * Ditto for secret keys. Handling is simpler than for public keys.
1632 * We allow secret key importing only when allow is true, this is so
1633 * that a secret key can not be imported accidently and thereby tampering
1634 * with the trust calculation.
1637 import_secret_one (ctrl_t ctrl, const char *fname, kbnode_t keyblock,
1638 struct stats_s *stats, int batch, unsigned int options,
1640 import_screener_t screener, void *screener_arg)
1643 struct seckey_info *ski;
1644 kbnode_t node, uidnode;
1648 kbnode_t pub_keyblock;
1649 char pkstrbuf[PUBKEY_STRING_SIZE];
1651 /* Get the key and print some info about it */
1652 node = find_kbnode (keyblock, PKT_SECRET_KEY);
1656 pk = node->pkt->pkt.public_key;
1658 keyid_from_pk (pk, keyid);
1659 uidnode = find_next_kbnode (keyblock, PKT_USER_ID);
1661 if (screener && screener (keyblock, screener_arg))
1663 log_error (_("secret key %s: %s\n"), keystr_from_pk (pk),
1664 _("rejected by import screener"));
1668 if (opt.verbose && !for_migration)
1670 log_info ("sec %s/%s %s ",
1671 pubkey_string (pk, pkstrbuf, sizeof pkstrbuf),
1672 keystr_from_pk (pk), datestr_from_pk (pk));
1674 print_utf8_buffer (log_get_stream (), uidnode->pkt->pkt.user_id->name,
1675 uidnode->pkt->pkt.user_id->len);
1678 stats->secret_read++;
1680 if ((options & IMPORT_NO_SECKEY))
1683 log_error (_("importing secret keys not allowed\n"));
1690 log_error( _("key %s: no user ID\n"), keystr_from_pk (pk));
1694 ski = pk->seckey_info;
1697 /* Actually an internal error. */
1698 log_error ("key %s: secret key info missing\n", keystr_from_pk (pk));
1702 /* A quick check to not import keys with an invalid protection
1703 cipher algorithm (only checks the primary key, though). */
1704 if (ski->algo > 110)
1707 log_error (_("key %s: secret key with invalid cipher %d"
1708 " - skipped\n"), keystr_from_pk (pk), ski->algo);
1712 #ifdef ENABLE_SELINUX_HACKS
1715 /* We don't allow to import secret keys because that may be used
1716 to put a secret key into the keyring and the user might later
1717 be tricked into signing stuff with that key. */
1718 log_error (_("importing secret keys not allowed\n"));
1723 clear_kbnode_flags (keyblock);
1725 nr_prev = stats->skipped_new_keys;
1727 /* Make a public key out of the key. */
1728 pub_keyblock = sec_to_pub_keyblock (keyblock);
1730 log_error ("key %s: failed to create public key from secret key\n",
1731 keystr_from_pk (pk));
1734 /* Note that this outputs an IMPORT_OK status message for the
1735 public key block, and below we will output another one for
1736 the secret keys. FIXME? */
1737 import_one (ctrl, fname, pub_keyblock, stats,
1738 NULL, NULL, options, 1, for_migration,
1739 screener, screener_arg);
1741 /* Fixme: We should check for an invalid keyblock and
1742 cancel the secret key import in this case. */
1743 release_kbnode (pub_keyblock);
1745 /* At least we cancel the secret key import when the public key
1746 import was skipped due to MERGE_ONLY option and a new
1748 if (stats->skipped_new_keys <= nr_prev)
1750 /* Read the keyblock again to get the effects of a merge. */
1751 /* Fixme: we should do this based on the fingerprint or
1752 even better let import_one return the merged
1754 node = get_pubkeyblock (keyid);
1756 log_error ("key %s: failed to re-lookup public key\n",
1757 keystr_from_pk (pk));
1760 nr_prev = stats->secret_imported;
1761 if (!transfer_secret_keys (ctrl, stats, keyblock, batch))
1765 log_info (_("key %s: secret key imported\n"),
1766 keystr_from_pk (pk));
1767 if (stats->secret_imported > nr_prev)
1769 if (is_status_enabled ())
1770 print_import_ok (pk, status);
1771 check_prefs (ctrl, node);
1773 release_kbnode (node);
1783 * Import a revocation certificate; this is a single signature packet.
1786 import_revoke_cert( const char *fname, kbnode_t node, struct stats_s *stats )
1788 PKT_public_key *pk = NULL;
1790 kbnode_t keyblock = NULL;
1791 KEYDB_HANDLE hd = NULL;
1797 assert( !node->next );
1798 assert( node->pkt->pkttype == PKT_SIGNATURE );
1799 assert( node->pkt->pkt.signature->sig_class == 0x20 );
1801 keyid[0] = node->pkt->pkt.signature->keyid[0];
1802 keyid[1] = node->pkt->pkt.signature->keyid[1];
1804 pk = xmalloc_clear( sizeof *pk );
1805 rc = get_pubkey( pk, keyid );
1806 if (gpg_err_code (rc) == GPG_ERR_NO_PUBKEY )
1808 log_error(_("key %s: no public key -"
1809 " can't apply revocation certificate\n"), keystr(keyid));
1815 log_error(_("key %s: public key not found: %s\n"),
1816 keystr(keyid), gpg_strerror (rc));
1820 /* Read the original keyblock. */
1823 byte afp[MAX_FINGERPRINT_LEN];
1826 fingerprint_from_pk (pk, afp, &an);
1827 while (an < MAX_FINGERPRINT_LEN)
1829 rc = keydb_search_fpr (hd, afp);
1833 log_error (_("key %s: can't locate original keyblock: %s\n"),
1834 keystr(keyid), gpg_strerror (rc));
1837 rc = keydb_get_keyblock (hd, &keyblock );
1840 log_error (_("key %s: can't read original keyblock: %s\n"),
1841 keystr(keyid), gpg_strerror (rc));
1845 /* it is okay, that node is not in keyblock because
1846 * check_key_signature works fine for sig_class 0x20 in this
1848 rc = check_key_signature( keyblock, node, NULL);
1851 log_error( _("key %s: invalid revocation certificate"
1852 ": %s - rejected\n"), keystr(keyid), gpg_strerror (rc));
1856 /* check whether we already have this */
1857 for(onode=keyblock->next; onode; onode=onode->next ) {
1858 if (onode->pkt->pkttype == PKT_USER_ID )
1860 else if (onode->pkt->pkttype == PKT_SIGNATURE
1861 && !cmp_signatures(node->pkt->pkt.signature,
1862 onode->pkt->pkt.signature))
1865 goto leave; /* yes, we already know about it */
1870 insert_kbnode( keyblock, clone_kbnode(node), 0 );
1872 /* and write the keyblock back */
1873 rc = keydb_update_keyblock (hd, keyblock );
1875 log_error (_("error writing keyring '%s': %s\n"),
1876 keydb_get_resource_name (hd), gpg_strerror (rc) );
1883 char *p=get_user_id_native (keyid);
1884 log_info( _("key %s: \"%s\" revocation certificate imported\n"),
1890 /* If the key we just revoked was ultimately trusted, remove its
1891 ultimate trust. This doesn't stop the user from putting the
1892 ultimate trust back, but is a reasonable solution for now. */
1893 if(get_ownertrust(pk)==TRUST_ULTIMATE)
1894 clear_ownertrusts(pk);
1896 revalidation_mark ();
1900 release_kbnode( keyblock );
1901 free_public_key( pk );
1907 * Loop over the keyblock and check all self signatures.
1908 * Mark all user-ids with a self-signature by setting flag bit 0.
1909 * Mark all user-ids with an invalid self-signature by setting bit 1.
1910 * This works also for subkeys, here the subkey is marked. Invalid or
1911 * extra subkey sigs (binding or revocation) are marked for deletion.
1912 * non_self is set to true if there are any sigs other than self-sigs
1916 chk_self_sigs (const char *fname, kbnode_t keyblock,
1917 PKT_public_key *pk, u32 *keyid, int *non_self )
1919 kbnode_t n, knode = NULL;
1922 u32 bsdate=0, rsdate=0;
1923 kbnode_t bsnode = NULL, rsnode = NULL;
1928 for (n=keyblock; (n = find_next_kbnode (n, 0)); )
1930 if (n->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1940 if ( n->pkt->pkttype != PKT_SIGNATURE )
1943 sig = n->pkt->pkt.signature;
1944 if ( keyid[0] != sig->keyid[0] || keyid[1] != sig->keyid[1] )
1950 /* This just caches the sigs for later use. That way we
1951 import a fully-cached key which speeds things up. */
1952 if (!opt.no_sig_cache)
1953 check_key_signature (keyblock, n, NULL);
1955 if ( IS_UID_SIG(sig) || IS_UID_REV(sig) )
1957 kbnode_t unode = find_prev_kbnode( keyblock, n, PKT_USER_ID );
1960 log_error( _("key %s: no user ID for signature\n"),
1962 return -1; /* The complete keyblock is invalid. */
1965 /* If it hasn't been marked valid yet, keep trying. */
1966 if (!(unode->flag&1))
1968 rc = check_key_signature (keyblock, n, NULL);
1973 char *p = utf8_to_native
1974 (unode->pkt->pkt.user_id->name,
1975 strlen (unode->pkt->pkt.user_id->name),0);
1976 log_info (gpg_err_code(rc) == GPG_ERR_PUBKEY_ALGO ?
1977 _("key %s: unsupported public key "
1978 "algorithm on user ID \"%s\"\n"):
1979 _("key %s: invalid self-signature "
1980 "on user ID \"%s\"\n"),
1986 unode->flag |= 1; /* Mark that signature checked. */
1989 else if (IS_KEY_SIG (sig))
1991 rc = check_key_signature (keyblock, n, NULL);
1995 log_info (gpg_err_code (rc) == GPG_ERR_PUBKEY_ALGO ?
1996 _("key %s: unsupported public key algorithm\n"):
1997 _("key %s: invalid direct key signature\n"),
2002 else if ( IS_SUBKEY_SIG (sig) )
2004 /* Note that this works based solely on the timestamps like
2005 the rest of gpg. If the standard gets revocation
2006 targets, this may need to be revised. */
2011 log_info (_("key %s: no subkey for key binding\n"),
2013 n->flag |= 4; /* delete this */
2017 rc = check_key_signature (keyblock, n, NULL);
2021 log_info (gpg_err_code (rc) == GPG_ERR_PUBKEY_ALGO ?
2022 _("key %s: unsupported public key"
2024 _("key %s: invalid subkey binding\n"),
2030 /* It's valid, so is it newer? */
2031 if (sig->timestamp >= bsdate)
2033 knode->flag |= 1; /* The subkey is valid. */
2036 /* Delete the last binding sig since this
2040 log_info (_("key %s: removed multiple subkey"
2041 " binding\n"),keystr(keyid));
2045 bsdate = sig->timestamp;
2048 n->flag |= 4; /* older */
2052 else if ( IS_SUBKEY_REV (sig) )
2054 /* We don't actually mark the subkey as revoked right now,
2055 so just check that the revocation sig is the most recent
2056 valid one. Note that we don't care if the binding sig is
2057 newer than the revocation sig. See the comment in
2058 getkey.c:merge_selfsigs_subkey for more. */
2062 log_info (_("key %s: no subkey for key revocation\n"),
2064 n->flag |= 4; /* delete this */
2068 rc = check_key_signature (keyblock, n, NULL);
2072 log_info (gpg_err_code (rc) == GPG_ERR_PUBKEY_ALGO ?
2073 _("key %s: unsupported public"
2074 " key algorithm\n"):
2075 _("key %s: invalid subkey revocation\n"),
2081 /* It's valid, so is it newer? */
2082 if (sig->timestamp >= rsdate)
2086 /* Delete the last revocation sig since
2087 this one is newer. */
2090 log_info (_("key %s: removed multiple subkey"
2091 " revocation\n"),keystr(keyid));
2095 rsdate = sig->timestamp;
2098 n->flag |= 4; /* older */
2109 * delete all parts which are invalid and those signatures whose
2110 * public key algorithm is not available in this implemenation;
2111 * but consider RSA as valid, because parse/build_packets knows
2113 * returns: true if at least one valid user-id is left over.
2116 delete_inv_parts( const char *fname, kbnode_t keyblock,
2117 u32 *keyid, unsigned int options)
2120 int nvalid=0, uid_seen=0, subkey_seen=0;
2124 for (node=keyblock->next; node; node = node->next )
2126 if (node->pkt->pkttype == PKT_USER_ID)
2129 if ((node->flag & 2) || !(node->flag & 1) )
2133 char *p=utf8_to_native(node->pkt->pkt.user_id->name,
2134 node->pkt->pkt.user_id->len,0);
2135 log_info( _("key %s: skipped user ID \"%s\"\n"),
2139 delete_kbnode( node ); /* the user-id */
2140 /* and all following packets up to the next user-id */
2142 && node->next->pkt->pkttype != PKT_USER_ID
2143 && node->next->pkt->pkttype != PKT_PUBLIC_SUBKEY
2144 && node->next->pkt->pkttype != PKT_SECRET_SUBKEY ){
2145 delete_kbnode( node->next );
2152 else if ( node->pkt->pkttype == PKT_PUBLIC_SUBKEY
2153 || node->pkt->pkttype == PKT_SECRET_SUBKEY )
2155 if ((node->flag & 2) || !(node->flag & 1) )
2158 log_info( _("key %s: skipped subkey\n"),keystr(keyid));
2160 delete_kbnode( node ); /* the subkey */
2161 /* and all following signature packets */
2163 && node->next->pkt->pkttype == PKT_SIGNATURE ) {
2164 delete_kbnode( node->next );
2171 else if (node->pkt->pkttype == PKT_SIGNATURE
2172 && openpgp_pk_test_algo (node->pkt->pkt.signature->pubkey_algo)
2173 && node->pkt->pkt.signature->pubkey_algo != PUBKEY_ALGO_RSA )
2175 delete_kbnode( node ); /* build_packet() can't handle this */
2177 else if (node->pkt->pkttype == PKT_SIGNATURE
2178 && !node->pkt->pkt.signature->flags.exportable
2179 && !(options&IMPORT_LOCAL_SIGS)
2180 && !have_secret_key_with_kid (node->pkt->pkt.signature->keyid))
2182 /* here we violate the rfc a bit by still allowing
2183 * to import non-exportable signature when we have the
2184 * the secret key used to create this signature - it
2185 * seems that this makes sense */
2187 log_info( _("key %s: non exportable signature"
2188 " (class 0x%02X) - skipped\n"),
2189 keystr(keyid), node->pkt->pkt.signature->sig_class );
2190 delete_kbnode( node );
2192 else if (node->pkt->pkttype == PKT_SIGNATURE
2193 && node->pkt->pkt.signature->sig_class == 0x20)
2198 log_info( _("key %s: revocation certificate"
2199 " at wrong place - skipped\n"),keystr(keyid));
2200 delete_kbnode( node );
2204 /* If the revocation cert is from a different key than
2205 the one we're working on don't check it - it's
2206 probably from a revocation key and won't be
2207 verifiable with this key anyway. */
2209 if(node->pkt->pkt.signature->keyid[0]==keyid[0]
2210 && node->pkt->pkt.signature->keyid[1]==keyid[1])
2212 int rc = check_key_signature( keyblock, node, NULL);
2216 log_info( _("key %s: invalid revocation"
2217 " certificate: %s - skipped\n"),
2218 keystr(keyid), gpg_strerror (rc));
2219 delete_kbnode( node );
2224 else if (node->pkt->pkttype == PKT_SIGNATURE
2225 && (node->pkt->pkt.signature->sig_class == 0x18
2226 || node->pkt->pkt.signature->sig_class == 0x28)
2230 log_info( _("key %s: subkey signature"
2231 " in wrong place - skipped\n"), keystr(keyid));
2232 delete_kbnode( node );
2234 else if (node->pkt->pkttype == PKT_SIGNATURE
2235 && !IS_CERT(node->pkt->pkt.signature))
2238 log_info(_("key %s: unexpected signature class (0x%02X) -"
2239 " skipped\n"),keystr(keyid),
2240 node->pkt->pkt.signature->sig_class);
2241 delete_kbnode(node);
2243 else if ((node->flag & 4) ) /* marked for deletion */
2244 delete_kbnode( node );
2247 /* note: because keyblock is the public key, it is never marked
2248 * for deletion and so keyblock cannot change */
2249 commit_kbnode( &keyblock );
2255 * It may happen that the imported keyblock has duplicated user IDs.
2256 * We check this here and collapse those user IDs together with their
2258 * Returns: True if the keyblock has changed.
2261 collapse_uids( kbnode_t *keyblock )
2266 for(uid1=*keyblock;uid1;uid1=uid1->next)
2270 if(is_deleted_kbnode(uid1))
2273 if(uid1->pkt->pkttype!=PKT_USER_ID)
2276 for(uid2=uid1->next;uid2;uid2=uid2->next)
2278 if(is_deleted_kbnode(uid2))
2281 if(uid2->pkt->pkttype!=PKT_USER_ID)
2284 if(cmp_user_ids(uid1->pkt->pkt.user_id,
2285 uid2->pkt->pkt.user_id)==0)
2287 /* We have a duplicated uid */
2292 /* Now take uid2's signatures, and attach them to
2294 for(last=uid2;last->next;last=last->next)
2296 if(is_deleted_kbnode(last))
2299 if(last->next->pkt->pkttype==PKT_USER_ID
2300 || last->next->pkt->pkttype==PKT_PUBLIC_SUBKEY
2301 || last->next->pkt->pkttype==PKT_SECRET_SUBKEY)
2306 (find_prev_kbnode(*keyblock,uid2,0))->next=last->next;
2308 /* Now put uid2 in place as part of uid1 */
2309 last->next=uid1->next;
2311 delete_kbnode(uid2);
2313 /* Now dedupe uid1 */
2314 for(sig1=uid1->next;sig1;sig1=sig1->next)
2318 if(is_deleted_kbnode(sig1))
2321 if(sig1->pkt->pkttype==PKT_USER_ID
2322 || sig1->pkt->pkttype==PKT_PUBLIC_SUBKEY
2323 || sig1->pkt->pkttype==PKT_SECRET_SUBKEY)
2326 if(sig1->pkt->pkttype!=PKT_SIGNATURE)
2329 for(sig2=sig1->next,last=sig1;sig2;last=sig2,sig2=sig2->next)
2331 if(is_deleted_kbnode(sig2))
2334 if(sig2->pkt->pkttype==PKT_USER_ID
2335 || sig2->pkt->pkttype==PKT_PUBLIC_SUBKEY
2336 || sig2->pkt->pkttype==PKT_SECRET_SUBKEY)
2339 if(sig2->pkt->pkttype!=PKT_SIGNATURE)
2342 if(cmp_signatures(sig1->pkt->pkt.signature,
2343 sig2->pkt->pkt.signature)==0)
2345 /* We have a match, so delete the second
2347 delete_kbnode(sig2);
2356 commit_kbnode(keyblock);
2358 if(any && !opt.quiet)
2360 const char *key="???";
2362 if ((uid1 = find_kbnode (*keyblock, PKT_PUBLIC_KEY)) )
2363 key = keystr_from_pk (uid1->pkt->pkt.public_key);
2364 else if ((uid1 = find_kbnode( *keyblock, PKT_SECRET_KEY)) )
2365 key = keystr_from_pk (uid1->pkt->pkt.public_key);
2367 log_info (_("key %s: duplicated user ID detected - merged\n"), key);
2374 /* Check for a 0x20 revocation from a revocation key that is not
2375 present. This may be called without the benefit of merge_xxxx so
2376 you can't rely on pk->revkey and friends. */
2378 revocation_present (ctrl_t ctrl, kbnode_t keyblock)
2380 kbnode_t onode, inode;
2381 PKT_public_key *pk = keyblock->pkt->pkt.public_key;
2383 for(onode=keyblock->next;onode;onode=onode->next)
2385 /* If we reach user IDs, we're done. */
2386 if(onode->pkt->pkttype==PKT_USER_ID)
2389 if(onode->pkt->pkttype==PKT_SIGNATURE &&
2390 onode->pkt->pkt.signature->sig_class==0x1F &&
2391 onode->pkt->pkt.signature->revkey)
2394 PKT_signature *sig=onode->pkt->pkt.signature;
2396 for(idx=0;idx<sig->numrevkeys;idx++)
2400 keyid_from_fingerprint(sig->revkey[idx]->fpr,
2401 MAX_FINGERPRINT_LEN,keyid);
2403 for(inode=keyblock->next;inode;inode=inode->next)
2405 /* If we reach user IDs, we're done. */
2406 if(inode->pkt->pkttype==PKT_USER_ID)
2409 if(inode->pkt->pkttype==PKT_SIGNATURE &&
2410 inode->pkt->pkt.signature->sig_class==0x20 &&
2411 inode->pkt->pkt.signature->keyid[0]==keyid[0] &&
2412 inode->pkt->pkt.signature->keyid[1]==keyid[1])
2414 /* Okay, we have a revocation key, and a
2415 revocation issued by it. Do we have the key
2419 rc=get_pubkey_byfprint_fast (NULL,sig->revkey[idx]->fpr,
2420 MAX_FINGERPRINT_LEN);
2421 if (gpg_err_code (rc) == GPG_ERR_NO_PUBKEY
2422 || gpg_err_code (rc) == GPG_ERR_UNUSABLE_PUBKEY)
2424 char *tempkeystr=xstrdup(keystr_from_pk(pk));
2426 /* No, so try and get it */
2428 && (opt.keyserver_options.options
2429 & KEYSERVER_AUTO_KEY_RETRIEVE))
2431 log_info(_("WARNING: key %s may be revoked:"
2432 " fetching revocation key %s\n"),
2433 tempkeystr,keystr(keyid));
2434 keyserver_import_fprint (ctrl,
2435 sig->revkey[idx]->fpr,
2436 MAX_FINGERPRINT_LEN,
2439 /* Do we have it now? */
2440 rc=get_pubkey_byfprint_fast (NULL,
2441 sig->revkey[idx]->fpr,
2442 MAX_FINGERPRINT_LEN);
2445 if (gpg_err_code (rc) == GPG_ERR_NO_PUBKEY
2446 || gpg_err_code (rc) == GPG_ERR_UNUSABLE_PUBKEY)
2447 log_info(_("WARNING: key %s may be revoked:"
2448 " revocation key %s not present.\n"),
2449 tempkeystr,keystr(keyid));
2462 * compare and merge the blocks
2464 * o compare the signatures: If we already have this signature, check
2465 * that they compare okay; if not, issue a warning and ask the user.
2466 * o Simply add the signature. Can't verify here because we may not have
2467 * the signature's public key yet; verification is done when putting it
2468 * into the trustdb, which is done automagically as soon as this pubkey
2470 * Note: We indicate newly inserted packets with flag bit 0
2473 merge_blocks (const char *fname, kbnode_t keyblock_orig, kbnode_t keyblock,
2474 u32 *keyid, int *n_uids, int *n_sigs, int *n_subk )
2476 kbnode_t onode, node;
2479 /* 1st: handle revocation certificates */
2480 for (node=keyblock->next; node; node=node->next )
2482 if (node->pkt->pkttype == PKT_USER_ID )
2484 else if (node->pkt->pkttype == PKT_SIGNATURE
2485 && node->pkt->pkt.signature->sig_class == 0x20)
2487 /* check whether we already have this */
2489 for (onode=keyblock_orig->next; onode; onode=onode->next)
2491 if (onode->pkt->pkttype == PKT_USER_ID )
2493 else if (onode->pkt->pkttype == PKT_SIGNATURE
2494 && onode->pkt->pkt.signature->sig_class == 0x20
2495 && !cmp_signatures(onode->pkt->pkt.signature,
2496 node->pkt->pkt.signature))
2504 kbnode_t n2 = clone_kbnode(node);
2505 insert_kbnode( keyblock_orig, n2, 0 );
2510 char *p=get_user_id_native (keyid);
2511 log_info(_("key %s: \"%s\" revocation"
2512 " certificate added\n"), keystr(keyid),p);
2519 /* 2nd: merge in any direct key (0x1F) sigs */
2520 for(node=keyblock->next; node; node=node->next)
2522 if (node->pkt->pkttype == PKT_USER_ID )
2524 else if (node->pkt->pkttype == PKT_SIGNATURE
2525 && node->pkt->pkt.signature->sig_class == 0x1F)
2527 /* check whether we already have this */
2529 for (onode=keyblock_orig->next; onode; onode=onode->next)
2531 if (onode->pkt->pkttype == PKT_USER_ID)
2533 else if (onode->pkt->pkttype == PKT_SIGNATURE
2534 && onode->pkt->pkt.signature->sig_class == 0x1F
2535 && !cmp_signatures(onode->pkt->pkt.signature,
2536 node->pkt->pkt.signature))
2544 kbnode_t n2 = clone_kbnode(node);
2545 insert_kbnode( keyblock_orig, n2, 0 );
2549 log_info( _("key %s: direct key signature added\n"),
2555 /* 3rd: try to merge new certificates in */
2556 for (onode=keyblock_orig->next; onode; onode=onode->next)
2558 if (!(onode->flag & 1) && onode->pkt->pkttype == PKT_USER_ID)
2560 /* find the user id in the imported keyblock */
2561 for (node=keyblock->next; node; node=node->next)
2562 if (node->pkt->pkttype == PKT_USER_ID
2563 && !cmp_user_ids( onode->pkt->pkt.user_id,
2564 node->pkt->pkt.user_id ) )
2566 if (node ) /* found: merge */
2568 rc = merge_sigs( onode, node, n_sigs, fname, keyid );
2575 /* 4th: add new user-ids */
2576 for (node=keyblock->next; node; node=node->next)
2578 if (node->pkt->pkttype == PKT_USER_ID)
2580 /* do we have this in the original keyblock */
2581 for (onode=keyblock_orig->next; onode; onode=onode->next )
2582 if (onode->pkt->pkttype == PKT_USER_ID
2583 && !cmp_user_ids( onode->pkt->pkt.user_id,
2584 node->pkt->pkt.user_id ) )
2586 if (!onode ) /* this is a new user id: append */
2588 rc = append_uid( keyblock_orig, node, n_sigs, fname, keyid);
2596 /* 5th: add new subkeys */
2597 for (node=keyblock->next; node; node=node->next)
2600 if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
2602 /* do we have this in the original keyblock? */
2603 for(onode=keyblock_orig->next; onode; onode=onode->next)
2604 if (onode->pkt->pkttype == PKT_PUBLIC_SUBKEY
2605 && !cmp_public_keys( onode->pkt->pkt.public_key,
2606 node->pkt->pkt.public_key))
2608 if (!onode ) /* This is a new subkey: append. */
2610 rc = append_key (keyblock_orig, node, n_sigs, fname, keyid);
2616 else if (node->pkt->pkttype == PKT_SECRET_SUBKEY)
2618 /* do we have this in the original keyblock? */
2619 for (onode=keyblock_orig->next; onode; onode=onode->next )
2620 if (onode->pkt->pkttype == PKT_SECRET_SUBKEY
2621 && !cmp_public_keys (onode->pkt->pkt.public_key,
2622 node->pkt->pkt.public_key) )
2624 if (!onode ) /* This is a new subkey: append. */
2626 rc = append_key (keyblock_orig, node, n_sigs, fname, keyid);
2634 /* 6th: merge subkey certificates */
2635 for (onode=keyblock_orig->next; onode; onode=onode->next)
2637 if (!(onode->flag & 1)
2638 && (onode->pkt->pkttype == PKT_PUBLIC_SUBKEY
2639 || onode->pkt->pkttype == PKT_SECRET_SUBKEY))
2641 /* find the subkey in the imported keyblock */
2642 for(node=keyblock->next; node; node=node->next)
2644 if ((node->pkt->pkttype == PKT_PUBLIC_SUBKEY
2645 || node->pkt->pkttype == PKT_SECRET_SUBKEY)
2646 && !cmp_public_keys( onode->pkt->pkt.public_key,
2647 node->pkt->pkt.public_key ) )
2650 if (node) /* Found: merge. */
2652 rc = merge_keysigs( onode, node, n_sigs, fname, keyid );
2664 * Append the userid starting with NODE and all signatures to KEYBLOCK.
2667 append_uid (kbnode_t keyblock, kbnode_t node, int *n_sigs,
2668 const char *fname, u32 *keyid )
2671 kbnode_t n_where = NULL;
2676 assert(node->pkt->pkttype == PKT_USER_ID );
2678 /* find the position */
2679 for (n = keyblock; n; n_where = n, n = n->next)
2681 if (n->pkt->pkttype == PKT_PUBLIC_SUBKEY
2682 || n->pkt->pkttype == PKT_SECRET_SUBKEY )
2688 /* and append/insert */
2691 /* we add a clone to the original keyblock, because this
2692 * one is released first */
2693 n = clone_kbnode(node);
2696 insert_kbnode( n_where, n, 0 );
2700 add_kbnode( keyblock, n );
2703 if (n->pkt->pkttype == PKT_SIGNATURE )
2707 if (node && node->pkt->pkttype != PKT_SIGNATURE )
2716 * Merge the sigs from SRC onto DST. SRC and DST are both a PKT_USER_ID.
2717 * (how should we handle comment packets here?)
2720 merge_sigs (kbnode_t dst, kbnode_t src, int *n_sigs,
2721 const char *fname, u32 *keyid)
2729 assert(dst->pkt->pkttype == PKT_USER_ID );
2730 assert(src->pkt->pkttype == PKT_USER_ID );
2732 for (n=src->next; n && n->pkt->pkttype != PKT_USER_ID; n = n->next)
2734 if (n->pkt->pkttype != PKT_SIGNATURE )
2736 if (n->pkt->pkt.signature->sig_class == 0x18
2737 || n->pkt->pkt.signature->sig_class == 0x28 )
2738 continue; /* skip signatures which are only valid on subkeys */
2741 for (n2=dst->next; n2 && n2->pkt->pkttype != PKT_USER_ID; n2 = n2->next)
2742 if (!cmp_signatures(n->pkt->pkt.signature,n2->pkt->pkt.signature))
2749 /* This signature is new or newer, append N to DST.
2750 * We add a clone to the original keyblock, because this
2751 * one is released first */
2752 n2 = clone_kbnode(n);
2753 insert_kbnode( dst, n2, PKT_SIGNATURE );
2765 * Merge the sigs from SRC onto DST. SRC and DST are both a PKT_xxx_SUBKEY.
2768 merge_keysigs (kbnode_t dst, kbnode_t src, int *n_sigs,
2769 const char *fname, u32 *keyid)
2777 assert (dst->pkt->pkttype == PKT_PUBLIC_SUBKEY
2778 || dst->pkt->pkttype == PKT_SECRET_SUBKEY);
2780 for (n=src->next; n ; n = n->next)
2782 if (n->pkt->pkttype == PKT_PUBLIC_SUBKEY
2783 || n->pkt->pkttype == PKT_PUBLIC_KEY )
2785 if (n->pkt->pkttype != PKT_SIGNATURE )
2789 for (n2=dst->next; n2; n2 = n2->next)
2791 if (n2->pkt->pkttype == PKT_PUBLIC_SUBKEY
2792 || n2->pkt->pkttype == PKT_PUBLIC_KEY )
2794 if (n2->pkt->pkttype == PKT_SIGNATURE
2795 && (n->pkt->pkt.signature->keyid[0]
2796 == n2->pkt->pkt.signature->keyid[0])
2797 && (n->pkt->pkt.signature->keyid[1]
2798 == n2->pkt->pkt.signature->keyid[1])
2799 && (n->pkt->pkt.signature->timestamp
2800 <= n2->pkt->pkt.signature->timestamp)
2801 && (n->pkt->pkt.signature->sig_class
2802 == n2->pkt->pkt.signature->sig_class))
2810 /* This signature is new or newer, append N to DST.
2811 * We add a clone to the original keyblock, because this
2812 * one is released first */
2813 n2 = clone_kbnode(n);
2814 insert_kbnode( dst, n2, PKT_SIGNATURE );
2826 * Append the subkey starting with NODE and all signatures to KEYBLOCK.
2827 * Mark all new and copied packets by setting flag bit 0.
2830 append_key (kbnode_t keyblock, kbnode_t node, int *n_sigs,
2831 const char *fname, u32 *keyid)
2838 assert( node->pkt->pkttype == PKT_PUBLIC_SUBKEY
2839 || node->pkt->pkttype == PKT_SECRET_SUBKEY );
2843 /* we add a clone to the original keyblock, because this
2844 * one is released first */
2845 n = clone_kbnode(node);
2846 add_kbnode( keyblock, n );
2849 if (n->pkt->pkttype == PKT_SIGNATURE )
2853 if (node && node->pkt->pkttype != PKT_SIGNATURE )