1 /* import.c - import a key into our key storage.
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 * 2006 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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 #include "keyserver-internal.h"
54 ulong secret_imported
;
56 ulong skipped_new_keys
;
63 static int import( IOBUF inp
, const char* fname
,struct stats_s
*stats
,
64 unsigned char **fpr
,size_t *fpr_len
,unsigned int options
);
65 static int read_block( IOBUF a
, PACKET
**pending_pkt
, KBNODE
*ret_root
);
66 static void revocation_present(KBNODE keyblock
);
67 static int import_one(const char *fname
, KBNODE keyblock
,struct stats_s
*stats
,
68 unsigned char **fpr
,size_t *fpr_len
,
69 unsigned int options
,int from_sk
);
70 static int import_secret_one( const char *fname
, KBNODE keyblock
,
71 struct stats_s
*stats
, unsigned int options
);
72 static int import_revoke_cert( const char *fname
, KBNODE node
,
73 struct stats_s
*stats
);
74 static int chk_self_sigs( const char *fname
, KBNODE keyblock
,
75 PKT_public_key
*pk
, u32
*keyid
, int *non_self
);
76 static int delete_inv_parts( const char *fname
, KBNODE keyblock
,
77 u32
*keyid
, unsigned int options
);
78 static int merge_blocks( const char *fname
, KBNODE keyblock_orig
,
79 KBNODE keyblock
, u32
*keyid
,
80 int *n_uids
, int *n_sigs
, int *n_subk
);
81 static int append_uid( KBNODE keyblock
, KBNODE node
, int *n_sigs
,
82 const char *fname
, u32
*keyid
);
83 static int append_key( KBNODE keyblock
, KBNODE node
, int *n_sigs
,
84 const char *fname
, u32
*keyid
);
85 static int merge_sigs( KBNODE dst
, KBNODE src
, int *n_sigs
,
86 const char *fname
, u32
*keyid
);
87 static int merge_keysigs( KBNODE dst
, KBNODE src
, int *n_sigs
,
88 const char *fname
, u32
*keyid
);
91 parse_import_options(char *str
,unsigned int *options
,int noisy
)
93 struct parse_options import_opts
[]=
95 {"import-local-sigs",IMPORT_LOCAL_SIGS
,NULL
,
96 N_("import signatures that are marked as local-only")},
97 {"repair-pks-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG
,NULL
,
98 N_("repair damage from the pks keyserver during import")},
99 {"fast-import",IMPORT_FAST
,NULL
,
100 N_("do not update the trustdb after import")},
101 {"convert-sk-to-pk",IMPORT_SK2PK
,NULL
,
102 N_("create a public key when importing a secret key")},
103 {"merge-only",IMPORT_MERGE_ONLY
,NULL
,
104 N_("only accept updates to existing keys")},
105 {"import-clean",IMPORT_CLEAN
,NULL
,
106 N_("remove unusable parts from key after import")},
107 {"import-minimal",IMPORT_MINIMAL
|IMPORT_CLEAN
,NULL
,
108 N_("remove as much as possible from key after import")},
109 /* Aliases for backward compatibility */
110 {"allow-local-sigs",IMPORT_LOCAL_SIGS
,NULL
,NULL
},
111 {"repair-hkp-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG
,NULL
,NULL
},
113 {"import-unusable-sigs",0,NULL
,NULL
},
114 {"import-clean-sigs",0,NULL
,NULL
},
115 {"import-clean-uids",0,NULL
,NULL
},
119 return parse_options(str
,options
,import_opts
,noisy
);
123 import_new_stats_handle (void)
125 return xmalloc_clear ( sizeof (struct stats_s
) );
129 import_release_stats_handle (void *p
)
135 * Import the public keys from the given filename. Input may be armored.
136 * This function rejects all keys which are not validly self signed on at
137 * least one userid. Only user ids which are self signed will be imported.
138 * Other signatures are not checked.
140 * Actually this function does a merge. It works like this:
143 * - check self-signatures and remove all userids and their signatures
144 * without/invalid self-signatures.
145 * - reject the keyblock, if we have no valid userid.
146 * - See whether we have this key already in one of our pubrings.
147 * If not, simply add it to the default keyring.
148 * - Compare the key and the self-signatures of the new and the one in
149 * our keyring. If they are different something weird is going on;
151 * - See whether we have only non-self-signature on one user id; if not
152 * ask the user what to do.
153 * - compare the signatures: If we already have this signature, check
154 * that they compare okay; if not, issue a warning and ask the user.
155 * (consider looking at the timestamp and use the newest?)
156 * - Simply add the signature. Can't verify here because we may not have
157 * the signature's public key yet; verification is done when putting it
158 * into the trustdb, which is done automagically as soon as this pubkey
160 * - Proceed with next signature.
162 * Key revocation certificates have special handling.
166 import_keys_internal( IOBUF inp
, char **fnames
, int nnames
,
167 void *stats_handle
, unsigned char **fpr
, size_t *fpr_len
,
168 unsigned int options
)
171 struct stats_s
*stats
= stats_handle
;
174 stats
= import_new_stats_handle ();
177 rc
= import( inp
, "[stream]", stats
, fpr
, fpr_len
, options
);
180 if( !fnames
&& !nnames
)
181 nnames
= 1; /* Ohh what a ugly hack to jump into the loop */
183 for(i
=0; i
< nnames
; i
++ ) {
184 const char *fname
= fnames
? fnames
[i
] : NULL
;
185 IOBUF inp2
= iobuf_open(fname
);
188 if (inp2
&& is_secured_file (iobuf_get_fd (inp2
)))
195 log_error(_("can't open `%s': %s\n"), fname
, strerror(errno
) );
198 rc
= import( inp2
, fname
, stats
, fpr
, fpr_len
, options
);
200 /* Must invalidate that ugly cache to actually close it. */
201 iobuf_ioctl (NULL
, 2, 0, (char*)fname
);
203 log_error("import from `%s' failed: %s\n", fname
,
211 import_print_stats (stats
);
212 import_release_stats_handle (stats
);
215 /* If no fast import and the trustdb is dirty (i.e. we added a key
216 or userID that had something other than a selfsig, a signature
217 that was other than a selfsig, or any revocation), then
218 update/check the trustdb if the user specified by setting
219 interactive or by not setting no-auto-check-trustdb */
221 if(!(options
&IMPORT_FAST
))
222 trustdb_check_or_update();
228 import_keys( char **fnames
, int nnames
,
229 void *stats_handle
, unsigned int options
)
231 import_keys_internal(NULL
,fnames
,nnames
,stats_handle
,NULL
,NULL
,options
);
235 import_keys_stream( IOBUF inp
, void *stats_handle
,
236 unsigned char **fpr
, size_t *fpr_len
,unsigned int options
)
238 return import_keys_internal(inp
,NULL
,0,stats_handle
,fpr
,fpr_len
,options
);
242 import( IOBUF inp
, const char* fname
,struct stats_s
*stats
,
243 unsigned char **fpr
,size_t *fpr_len
,unsigned int options
)
245 PACKET
*pending_pkt
= NULL
;
246 KBNODE keyblock
= NULL
; /* Need to initialize because gcc can't
247 grasp the return semantics of
251 getkey_disable_caches();
253 if( !opt
.no_armor
) { /* armored reading is not disabled */
254 armor_filter_context_t
*afx
= xmalloc_clear( sizeof *afx
);
255 afx
->only_keyblocks
= 1;
256 iobuf_push_filter2( inp
, armor_filter
, afx
, 1 );
259 while( !(rc
= read_block( inp
, &pending_pkt
, &keyblock
) )) {
260 if( keyblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
)
261 rc
= import_one( fname
, keyblock
, stats
, fpr
, fpr_len
, options
, 0);
262 else if( keyblock
->pkt
->pkttype
== PKT_SECRET_KEY
)
263 rc
= import_secret_one( fname
, keyblock
, stats
, options
);
264 else if( keyblock
->pkt
->pkttype
== PKT_SIGNATURE
265 && keyblock
->pkt
->pkt
.signature
->sig_class
== 0x20 )
266 rc
= import_revoke_cert( fname
, keyblock
, stats
);
268 log_info( _("skipping block of type %d\n"),
269 keyblock
->pkt
->pkttype
);
271 release_kbnode(keyblock
);
272 /* fixme: we should increment the not imported counter but this
273 does only make sense if we keep on going despite of errors. */
276 if( !(++stats
->count
% 100) && !opt
.quiet
)
277 log_info(_("%lu keys processed so far\n"), stats
->count
);
281 else if( rc
&& rc
!= G10ERR_INV_KEYRING
)
282 log_error( _("error reading `%s': %s\n"), fname
, g10_errstr(rc
));
289 import_print_stats (void *hd
)
291 struct stats_s
*stats
= hd
;
294 log_info(_("Total number processed: %lu\n"), stats
->count
);
295 if( stats
->skipped_new_keys
)
296 log_info(_(" skipped new keys: %lu\n"),
297 stats
->skipped_new_keys
);
298 if( stats
->no_user_id
)
299 log_info(_(" w/o user IDs: %lu\n"), stats
->no_user_id
);
300 if( stats
->imported
|| stats
->imported_rsa
) {
301 log_info(_(" imported: %lu"), stats
->imported
);
302 if( stats
->imported_rsa
)
303 fprintf(stderr
, " (RSA: %lu)", stats
->imported_rsa
);
306 if( stats
->unchanged
)
307 log_info(_(" unchanged: %lu\n"), stats
->unchanged
);
309 log_info(_(" new user IDs: %lu\n"), stats
->n_uids
);
311 log_info(_(" new subkeys: %lu\n"), stats
->n_subk
);
313 log_info(_(" new signatures: %lu\n"), stats
->n_sigs
);
315 log_info(_(" new key revocations: %lu\n"), stats
->n_revoc
);
316 if( stats
->secret_read
)
317 log_info(_(" secret keys read: %lu\n"), stats
->secret_read
);
318 if( stats
->secret_imported
)
319 log_info(_(" secret keys imported: %lu\n"), stats
->secret_imported
);
320 if( stats
->secret_dups
)
321 log_info(_(" secret keys unchanged: %lu\n"), stats
->secret_dups
);
322 if( stats
->not_imported
)
323 log_info(_(" not imported: %lu\n"), stats
->not_imported
);
324 if( stats
->n_sigs_cleaned
)
325 log_info(_(" signatures cleaned: %lu\n"),stats
->n_sigs_cleaned
);
326 if( stats
->n_uids_cleaned
)
327 log_info(_(" user IDs cleaned: %lu\n"),stats
->n_uids_cleaned
);
330 if( is_status_enabled() ) {
332 sprintf(buf
, "%lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
343 stats
->secret_imported
,
345 stats
->skipped_new_keys
,
346 stats
->not_imported
);
347 write_status_text( STATUS_IMPORT_RES
, buf
);
353 * Read the next keyblock from stream A.
354 * PENDING_PKT should be initialzed to NULL
355 * and not chnaged form the caller.
356 * Retunr: 0 = okay, -1 no more blocks or another errorcode.
359 read_block( IOBUF a
, PACKET
**pending_pkt
, KBNODE
*ret_root
)
367 root
= new_kbnode( *pending_pkt
);
373 pkt
= xmalloc( sizeof *pkt
);
375 while( (rc
=parse_packet(a
, pkt
)) != -1 ) {
376 if( rc
) { /* ignore errors */
377 if( rc
!= G10ERR_UNKNOWN_PACKET
) {
378 log_error("read_block: read error: %s\n", g10_errstr(rc
) );
379 rc
= G10ERR_INV_KEYRING
;
387 if( !root
&& pkt
->pkttype
== PKT_SIGNATURE
388 && pkt
->pkt
.signature
->sig_class
== 0x20 ) {
389 /* this is a revocation certificate which is handled
390 * in a special way */
391 root
= new_kbnode( pkt
);
396 /* make a linked list of all packets */
397 switch( pkt
->pkttype
) {
399 if(check_compress_algo(pkt
->pkt
.compressed
->algorithm
))
401 rc
= G10ERR_COMPR_ALGO
;
406 compress_filter_context_t
*cfx
= xmalloc_clear( sizeof *cfx
);
407 pkt
->pkt
.compressed
->buf
= NULL
;
408 push_compress_filter2(a
,cfx
,pkt
->pkt
.compressed
->algorithm
,1);
415 /* skip those packets */
422 if( in_cert
) { /* store this packet */
431 root
= new_kbnode( pkt
);
433 add_kbnode( root
, new_kbnode( pkt
) );
434 pkt
= xmalloc( sizeof *pkt
);
441 if( rc
== -1 && root
)
445 release_kbnode( root
);
453 /* Walk through the subkeys on a pk to find if we have the PKS
454 disease: multiple subkeys with their binding sigs stripped, and the
455 sig for the first subkey placed after the last subkey. That is,
456 instead of "pk uid sig sub1 bind1 sub2 bind2 sub3 bind3" we have
457 "pk uid sig sub1 sub2 sub3 bind1". We can't do anything about sub2
458 and sub3, as they are already lost, but we can try and rescue sub1
459 by reordering the keyblock so that it reads "pk uid sig sub1 bind1
460 sub2 sub3". Returns TRUE if the keyblock was modified. */
463 fix_pks_corruption(KBNODE keyblock
)
465 int changed
=0,keycount
=0;
466 KBNODE node
,last
=NULL
,sknode
=NULL
;
468 /* First determine if we have the problem at all. Look for 2 or
469 more subkeys in a row, followed by a single binding sig. */
470 for(node
=keyblock
;node
;last
=node
,node
=node
->next
)
472 if(node
->pkt
->pkttype
==PKT_PUBLIC_SUBKEY
)
478 else if(node
->pkt
->pkttype
==PKT_SIGNATURE
&&
479 node
->pkt
->pkt
.signature
->sig_class
==0x18 &&
480 keycount
>=2 && node
->next
==NULL
)
482 /* We might have the problem, as this key has two subkeys in
483 a row without any intervening packets. */
489 /* Temporarily attach node to sknode. */
490 node
->next
=sknode
->next
;
494 /* Note we aren't checking whether this binding sig is a
495 selfsig. This is not necessary here as the subkey and
496 binding sig will be rejected later if that is the
498 if(check_key_signature(keyblock
,node
,NULL
))
500 /* Not a match, so undo the changes. */
501 sknode
->next
=node
->next
;
508 sknode
->flag
|= 1; /* Mark it good so we don't need to
523 print_import_ok (PKT_public_key
*pk
, PKT_secret_key
*sk
, unsigned int reason
)
525 byte array
[MAX_FINGERPRINT_LEN
], *s
;
526 char buf
[MAX_FINGERPRINT_LEN
*2+30], *p
;
529 sprintf (buf
, "%u ", reason
);
530 p
= buf
+ strlen (buf
);
533 fingerprint_from_pk (pk
, array
, &n
);
535 fingerprint_from_sk (sk
, array
, &n
);
537 for (i
=0; i
< n
; i
++, s
++, p
+= 2)
538 sprintf (p
, "%02X", *s
);
540 write_status_text (STATUS_IMPORT_OK
, buf
);
544 print_import_check (PKT_public_key
* pk
, PKT_user_id
* id
)
549 size_t i
, pos
= 0, n
;
551 buf
= xmalloc (17+41+id
->len
+32);
552 keyid_from_pk (pk
, keyid
);
553 sprintf (buf
, "%08X%08X ", keyid
[0], keyid
[1]);
555 fingerprint_from_pk (pk
, fpr
, &n
);
556 for (i
= 0; i
< n
; i
++, pos
+= 2)
557 sprintf (buf
+pos
, "%02X", fpr
[i
]);
560 strcat (buf
, id
->name
);
561 write_status_text (STATUS_IMPORT_CHECK
, buf
);
566 check_prefs_warning(PKT_public_key
*pk
)
568 log_info(_("WARNING: key %s contains preferences for unavailable\n"
569 "algorithms on these user IDs:\n"), keystr_from_pk(pk
));
573 check_prefs(KBNODE keyblock
)
579 merge_keys_and_selfsig(keyblock
);
580 pk
=keyblock
->pkt
->pkt
.public_key
;
582 for(node
=keyblock
;node
;node
=node
->next
)
584 if(node
->pkt
->pkttype
==PKT_USER_ID
585 && node
->pkt
->pkt
.user_id
->created
586 && node
->pkt
->pkt
.user_id
->prefs
)
588 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
589 prefitem_t
*prefs
=uid
->prefs
;
590 char *user
=utf8_to_native(uid
->name
,strlen(uid
->name
),0);
592 for(;prefs
->type
;prefs
++)
594 char num
[10]; /* prefs->value is a byte, so we're over
597 sprintf(num
,"%u",prefs
->value
);
599 if(prefs
->type
==PREFTYPE_SYM
)
601 if (openpgp_cipher_test_algo (prefs
->value
))
603 const char *algo
= gcry_cipher_algo_name (prefs
->value
);
605 check_prefs_warning(pk
);
606 log_info(_(" \"%s\": preference for cipher"
607 " algorithm %s\n"),user
,algo
?algo
:num
);
611 else if(prefs
->type
==PREFTYPE_HASH
)
613 if(openpgp_md_test_algo(prefs
->value
))
615 const char *algo
= gcry_md_algo_name (prefs
->value
);
617 check_prefs_warning(pk
);
618 log_info(_(" \"%s\": preference for digest"
619 " algorithm %s\n"),user
,algo
?algo
:num
);
623 else if(prefs
->type
==PREFTYPE_ZIP
)
625 if(check_compress_algo (prefs
->value
))
627 const char *algo
=compress_algo_to_string(prefs
->value
);
629 check_prefs_warning(pk
);
630 log_info(_(" \"%s\": preference for compression"
631 " algorithm %s\n"),user
,algo
?algo
:num
);
643 log_info(_("it is strongly suggested that you update"
644 " your preferences and\n"));
645 log_info(_("re-distribute this key to avoid potential algorithm"
646 " mismatch problems\n"));
650 STRLIST sl
=NULL
,locusr
=NULL
;
652 byte fpr
[MAX_FINGERPRINT_LEN
],*p
;
653 char username
[(MAX_FINGERPRINT_LEN
*2)+1];
656 p
=fingerprint_from_pk(pk
,fpr
,&fprlen
);
657 for(i
=0;i
<fprlen
;i
++,p
++)
658 sprintf(username
+2*i
,"%02X",*p
);
659 add_to_strlist(&locusr
,username
);
661 append_to_strlist(&sl
,"updpref");
662 append_to_strlist(&sl
,"save");
664 keyedit_menu( username
, locusr
, sl
, 1, 1 );
666 free_strlist(locusr
);
669 log_info(_("you can update your preferences with:"
670 " gpg --edit-key %s updpref save\n"),keystr_from_pk(pk
));
675 * Try to import one keyblock. Return an error only in serious cases, but
676 * never for an invalid keyblock. It uses log_error to increase the
677 * internal errorcount, so that invalid input can be detected by programs
681 import_one( const char *fname
, KBNODE keyblock
, struct stats_s
*stats
,
682 unsigned char **fpr
,size_t *fpr_len
,unsigned int options
,
686 PKT_public_key
*pk_orig
;
687 KBNODE node
, uidnode
;
688 KBNODE keyblock_orig
= NULL
;
695 /* get the key and print some info about it */
696 node
= find_kbnode( keyblock
, PKT_PUBLIC_KEY
);
700 pk
= node
->pkt
->pkt
.public_key
;
702 keyid_from_pk( pk
, keyid
);
703 uidnode
= find_next_kbnode( keyblock
, PKT_USER_ID
);
705 if( opt
.verbose
&& !opt
.interactive
)
707 log_info( "pub %4u%c/%s %s ",
709 pubkey_letter( pk
->pubkey_algo
),
710 keystr_from_pk(pk
), datestr_from_pk(pk
) );
712 print_utf8_string( stderr
, uidnode
->pkt
->pkt
.user_id
->name
,
713 uidnode
->pkt
->pkt
.user_id
->len
);
719 log_error( _("key %s: no user ID\n"), keystr_from_pk(pk
));
723 if (opt
.interactive
) {
724 if(is_status_enabled())
725 print_import_check (pk
, uidnode
->pkt
->pkt
.user_id
);
726 merge_keys_and_selfsig (keyblock
);
728 show_basic_key_info (keyblock
);
730 if (!cpr_get_answer_is_yes ("import.okay",
731 "Do you want to import this key? (y/N) "))
735 collapse_uids(&keyblock
);
737 /* Clean the key that we're about to import, to cut down on things
738 that we have to clean later. This has no practical impact on
739 the end result, but does result in less logging which might
741 if(options
&IMPORT_CLEAN
)
742 clean_key(keyblock
,opt
.verbose
,options
&IMPORT_MINIMAL
,NULL
,NULL
);
744 clear_kbnode_flags( keyblock
);
746 if((options
&IMPORT_REPAIR_PKS_SUBKEY_BUG
) && fix_pks_corruption(keyblock
)
748 log_info(_("key %s: PKS subkey corruption repaired\n"),
751 rc
= chk_self_sigs( fname
, keyblock
, pk
, keyid
, &non_self
);
753 return rc
== -1? 0:rc
;
755 /* If we allow such a thing, mark unsigned uids as valid */
756 if( opt
.allow_non_selfsigned_uid
)
757 for( node
=keyblock
; node
; node
= node
->next
)
758 if( node
->pkt
->pkttype
== PKT_USER_ID
&& !(node
->flag
& 1) )
760 char *user
=utf8_to_native(node
->pkt
->pkt
.user_id
->name
,
761 node
->pkt
->pkt
.user_id
->len
,0);
763 log_info( _("key %s: accepted non self-signed user ID \"%s\"\n"),
764 keystr_from_pk(pk
),user
);
768 if( !delete_inv_parts( fname
, keyblock
, keyid
, options
) ) {
769 log_error( _("key %s: no valid user IDs\n"), keystr_from_pk(pk
));
771 log_info(_("this may be caused by a missing self-signature\n"));
776 /* do we have this key already in one of our pubrings ? */
777 pk_orig
= xmalloc_clear( sizeof *pk_orig
);
778 rc
= get_pubkey_fast ( pk_orig
, keyid
);
779 if( rc
&& rc
!= G10ERR_NO_PUBKEY
&& rc
!= G10ERR_UNU_PUBKEY
)
781 log_error( _("key %s: public key not found: %s\n"),
782 keystr(keyid
), g10_errstr(rc
));
784 else if ( rc
&& (opt
.import_options
&IMPORT_MERGE_ONLY
) )
787 log_info( _("key %s: new key - skipped\n"), keystr(keyid
));
789 stats
->skipped_new_keys
++;
791 else if( rc
) { /* insert this key */
792 KEYDB_HANDLE hd
= keydb_new (0);
794 rc
= keydb_locate_writable (hd
, NULL
);
796 log_error (_("no writable keyring found: %s\n"), g10_errstr (rc
));
798 return G10ERR_GENERAL
;
800 if( opt
.verbose
> 1 )
801 log_info (_("writing to `%s'\n"), keydb_get_resource_name (hd
) );
803 rc
= keydb_insert_keyblock (hd
, keyblock
);
805 log_error (_("error writing keyring `%s': %s\n"),
806 keydb_get_resource_name (hd
), g10_errstr(rc
));
809 /* This should not be possible since we delete the
810 ownertrust when a key is deleted, but it can happen if
811 the keyring and trustdb are out of sync. It can also
812 be made to happen with the trusted-key command. */
814 clear_ownertrusts (pk
);
816 revalidation_mark ();
823 char *p
=get_user_id_native (keyid
);
824 log_info( _("key %s: public key \"%s\" imported\n"),
828 if( is_status_enabled() )
830 char *us
= get_long_user_id_string( keyid
);
831 write_status_text( STATUS_IMPORTED
, us
);
833 print_import_ok (pk
,NULL
, 1);
836 if( is_RSA( pk
->pubkey_algo
) )
837 stats
->imported_rsa
++;
842 int n_uids
, n_sigs
, n_subk
, n_sigs_cleaned
, n_uids_cleaned
;
844 /* Compare the original against the new key; just to be sure nothing
845 * weird is going on */
846 if( cmp_public_keys( pk_orig
, pk
) )
848 log_error( _("key %s: doesn't match our copy\n"),keystr(keyid
));
852 /* now read the original keyblock */
855 byte afp
[MAX_FINGERPRINT_LEN
];
858 fingerprint_from_pk (pk_orig
, afp
, &an
);
859 while (an
< MAX_FINGERPRINT_LEN
)
861 rc
= keydb_search_fpr (hd
, afp
);
865 log_error (_("key %s: can't locate original keyblock: %s\n"),
866 keystr(keyid
), g10_errstr(rc
));
870 rc
= keydb_get_keyblock (hd
, &keyblock_orig
);
873 log_error (_("key %s: can't read original keyblock: %s\n"),
874 keystr(keyid
), g10_errstr(rc
));
879 /* and try to merge the block */
880 clear_kbnode_flags( keyblock_orig
);
881 clear_kbnode_flags( keyblock
);
882 n_uids
= n_sigs
= n_subk
= n_sigs_cleaned
= n_uids_cleaned
= 0;
883 rc
= merge_blocks( fname
, keyblock_orig
, keyblock
,
884 keyid
, &n_uids
, &n_sigs
, &n_subk
);
891 if(options
&IMPORT_CLEAN
)
892 clean_key(keyblock_orig
,opt
.verbose
,options
&IMPORT_MINIMAL
,
893 &n_uids_cleaned
,&n_sigs_cleaned
);
895 if( n_uids
|| n_sigs
|| n_subk
|| n_sigs_cleaned
|| n_uids_cleaned
) {
897 /* keyblock_orig has been updated; write */
898 rc
= keydb_update_keyblock (hd
, keyblock_orig
);
900 log_error (_("error writing keyring `%s': %s\n"),
901 keydb_get_resource_name (hd
), g10_errstr(rc
) );
903 revalidation_mark ();
908 char *p
=get_user_id_native(keyid
);
910 log_info( _("key %s: \"%s\" 1 new user ID\n"),
913 log_info( _("key %s: \"%s\" %d new user IDs\n"),
914 keystr(keyid
),p
,n_uids
);
916 log_info( _("key %s: \"%s\" 1 new signature\n"),
919 log_info( _("key %s: \"%s\" %d new signatures\n"),
920 keystr(keyid
), p
, n_sigs
);
922 log_info( _("key %s: \"%s\" 1 new subkey\n"),
925 log_info( _("key %s: \"%s\" %d new subkeys\n"),
926 keystr(keyid
), p
, n_subk
);
927 if(n_sigs_cleaned
==1)
928 log_info(_("key %s: \"%s\" %d signature cleaned\n"),
929 keystr(keyid
),p
,n_sigs_cleaned
);
930 else if(n_sigs_cleaned
)
931 log_info(_("key %s: \"%s\" %d signatures cleaned\n"),
932 keystr(keyid
),p
,n_sigs_cleaned
);
933 if(n_uids_cleaned
==1)
934 log_info(_("key %s: \"%s\" %d user ID cleaned\n"),
935 keystr(keyid
),p
,n_uids_cleaned
);
936 else if(n_uids_cleaned
)
937 log_info(_("key %s: \"%s\" %d user IDs cleaned\n"),
938 keystr(keyid
),p
,n_uids_cleaned
);
942 stats
->n_uids
+=n_uids
;
943 stats
->n_sigs
+=n_sigs
;
944 stats
->n_subk
+=n_subk
;
945 stats
->n_sigs_cleaned
+=n_sigs_cleaned
;
946 stats
->n_uids_cleaned
+=n_uids_cleaned
;
948 if (is_status_enabled ())
949 print_import_ok (pk
, NULL
,
950 ((n_uids
?2:0)|(n_sigs
?4:0)|(n_subk
?8:0)));
954 if (is_status_enabled ())
955 print_import_ok (pk
, NULL
, 0);
959 char *p
=get_user_id_native(keyid
);
960 log_info( _("key %s: \"%s\" not changed\n"),keystr(keyid
),p
);
967 keydb_release (hd
); hd
= NULL
;
972 /* Now that the key is definitely incorporated into the keydb, we
973 need to check if a designated revocation is present or if the
974 prefs are not rational so we can warn the user. */
978 revocation_present(keyblock_orig
);
979 if(!from_sk
&& seckey_available(keyid
)==0)
980 check_prefs(keyblock_orig
);
984 /* A little explanation for this: we fill in the fingerprint
985 when importing keys as it can be useful to know the
986 fingerprint in certain keyserver-related cases (a keyserver
987 asked for a particular name, but the key doesn't have that
988 name). However, in cases where we're importing more than
989 one key at a time, we cannot know which key to fingerprint.
990 In these cases, rather than guessing, we do not fingerpring
991 at all, and we must hope the user ID on the keys are
996 if(stats
->imported
==1)
997 *fpr
=fingerprint_from_pk(pk
,NULL
,fpr_len
);
1002 revocation_present(keyblock
);
1003 if(!from_sk
&& seckey_available(keyid
)==0)
1004 check_prefs(keyblock
);
1007 release_kbnode( keyblock_orig
);
1008 free_public_key( pk_orig
);
1013 /* Walk a secret keyblock and produce a public keyblock out of it. */
1015 sec_to_pub_keyblock(KBNODE sec_keyblock
)
1017 KBNODE secnode
,pub_keyblock
=NULL
,ctx
=NULL
;
1019 while((secnode
=walk_kbnode(sec_keyblock
,&ctx
,0)))
1023 if(secnode
->pkt
->pkttype
==PKT_SECRET_KEY
||
1024 secnode
->pkt
->pkttype
==PKT_SECRET_SUBKEY
)
1026 /* Make a public key. We only need to convert enough to
1027 write the keyblock out. */
1029 PKT_secret_key
*sk
=secnode
->pkt
->pkt
.secret_key
;
1030 PACKET
*pkt
=xmalloc_clear(sizeof(PACKET
));
1031 PKT_public_key
*pk
=xmalloc_clear(sizeof(PKT_public_key
));
1034 if(secnode
->pkt
->pkttype
==PKT_SECRET_KEY
)
1035 pkt
->pkttype
=PKT_PUBLIC_KEY
;
1037 pkt
->pkttype
=PKT_PUBLIC_SUBKEY
;
1039 pkt
->pkt
.public_key
=pk
;
1041 pk
->version
=sk
->version
;
1042 pk
->timestamp
=sk
->timestamp
;
1043 pk
->expiredate
=sk
->expiredate
;
1044 pk
->pubkey_algo
=sk
->pubkey_algo
;
1046 n
=pubkey_get_npkey(pk
->pubkey_algo
);
1049 /* we can't properly extract the pubkey without knowing
1050 the number of MPIs */
1051 release_kbnode(pub_keyblock
);
1059 pk
->pkey
[i
]=mpi_copy(sk
->skey
[i
]);
1062 pubnode
=new_kbnode(pkt
);
1066 pubnode
=clone_kbnode(secnode
);
1069 if(pub_keyblock
==NULL
)
1070 pub_keyblock
=pubnode
;
1072 add_kbnode(pub_keyblock
,pubnode
);
1075 return pub_keyblock
;
1079 * Ditto for secret keys. Handling is simpler than for public keys.
1080 * We allow secret key importing only when allow is true, this is so
1081 * that a secret key can not be imported accidently and thereby tampering
1082 * with the trust calculation.
1085 import_secret_one( const char *fname
, KBNODE keyblock
,
1086 struct stats_s
*stats
, unsigned int options
)
1089 KBNODE node
, uidnode
;
1093 /* get the key and print some info about it */
1094 node
= find_kbnode( keyblock
, PKT_SECRET_KEY
);
1098 sk
= node
->pkt
->pkt
.secret_key
;
1099 keyid_from_sk( sk
, keyid
);
1100 uidnode
= find_next_kbnode( keyblock
, PKT_USER_ID
);
1104 log_info( "sec %4u%c/%s %s ",
1105 nbits_from_sk( sk
),
1106 pubkey_letter( sk
->pubkey_algo
),
1107 keystr_from_sk(sk
), datestr_from_sk(sk
) );
1109 print_utf8_string( stderr
, uidnode
->pkt
->pkt
.user_id
->name
,
1110 uidnode
->pkt
->pkt
.user_id
->len
);
1113 stats
->secret_read
++;
1117 log_error( _("key %s: no user ID\n"), keystr_from_sk(sk
));
1121 if(sk
->protect
.algo
>110)
1123 log_error(_("key %s: secret key with invalid cipher %d"
1124 " - skipped\n"),keystr_from_sk(sk
),sk
->protect
.algo
);
1128 #ifdef ENABLE_SELINUX_HACKS
1131 /* We don't allow to import secret keys because that may be used
1132 to put a secret key into the keyring and the user might later
1133 be tricked into signing stuff with that key. */
1134 log_error (_("importing secret keys not allowed\n"));
1139 clear_kbnode_flags( keyblock
);
1141 /* do we have this key already in one of our secrings ? */
1142 rc
= seckey_available( keyid
);
1143 if( rc
== G10ERR_NO_SECKEY
&& !(opt
.import_options
&IMPORT_MERGE_ONLY
) )
1145 /* simply insert this key */
1146 KEYDB_HANDLE hd
= keydb_new (1);
1148 /* get default resource */
1149 rc
= keydb_locate_writable (hd
, NULL
);
1151 log_error (_("no default secret keyring: %s\n"), g10_errstr (rc
));
1153 return G10ERR_GENERAL
;
1155 rc
= keydb_insert_keyblock (hd
, keyblock
);
1157 log_error (_("error writing keyring `%s': %s\n"),
1158 keydb_get_resource_name (hd
), g10_errstr(rc
) );
1162 log_info( _("key %s: secret key imported\n"), keystr_from_sk(sk
));
1163 stats
->secret_imported
++;
1164 if (is_status_enabled ())
1165 print_import_ok (NULL
, sk
, 1|16);
1167 if(options
&IMPORT_SK2PK
)
1169 /* Try and make a public key out of this. */
1171 KBNODE pub_keyblock
=sec_to_pub_keyblock(keyblock
);
1174 import_one(fname
,pub_keyblock
,stats
,
1175 NULL
,NULL
,opt
.import_options
,1);
1176 release_kbnode(pub_keyblock
);
1180 /* Now that the key is definitely incorporated into the keydb,
1181 if we have the public part of this key, we need to check if
1182 the prefs are rational. */
1183 node
=get_pubkeyblock(keyid
);
1187 release_kbnode(node
);
1191 { /* we can't merge secret keys */
1192 log_error( _("key %s: already in secret keyring\n"),
1193 keystr_from_sk(sk
));
1194 stats
->secret_dups
++;
1195 if (is_status_enabled ())
1196 print_import_ok (NULL
, sk
, 16);
1198 /* TODO: if we ever do merge secret keys, make sure to handle
1199 the sec_to_pub_keyblock feature as well. */
1202 log_error( _("key %s: secret key not found: %s\n"),
1203 keystr_from_sk(sk
), g10_errstr(rc
));
1210 * Import a revocation certificate; this is a single signature packet.
1213 import_revoke_cert( const char *fname
, KBNODE node
, struct stats_s
*stats
)
1215 PKT_public_key
*pk
=NULL
;
1216 KBNODE onode
, keyblock
= NULL
;
1217 KEYDB_HANDLE hd
= NULL
;
1221 assert( !node
->next
);
1222 assert( node
->pkt
->pkttype
== PKT_SIGNATURE
);
1223 assert( node
->pkt
->pkt
.signature
->sig_class
== 0x20 );
1225 keyid
[0] = node
->pkt
->pkt
.signature
->keyid
[0];
1226 keyid
[1] = node
->pkt
->pkt
.signature
->keyid
[1];
1228 pk
= xmalloc_clear( sizeof *pk
);
1229 rc
= get_pubkey( pk
, keyid
);
1230 if( rc
== G10ERR_NO_PUBKEY
)
1232 log_error(_("key %s: no public key -"
1233 " can't apply revocation certificate\n"), keystr(keyid
));
1239 log_error(_("key %s: public key not found: %s\n"),
1240 keystr(keyid
), g10_errstr(rc
));
1244 /* read the original keyblock */
1247 byte afp
[MAX_FINGERPRINT_LEN
];
1250 fingerprint_from_pk (pk
, afp
, &an
);
1251 while (an
< MAX_FINGERPRINT_LEN
)
1253 rc
= keydb_search_fpr (hd
, afp
);
1257 log_error (_("key %s: can't locate original keyblock: %s\n"),
1258 keystr(keyid
), g10_errstr(rc
));
1261 rc
= keydb_get_keyblock (hd
, &keyblock
);
1264 log_error (_("key %s: can't read original keyblock: %s\n"),
1265 keystr(keyid
), g10_errstr(rc
));
1269 /* it is okay, that node is not in keyblock because
1270 * check_key_signature works fine for sig_class 0x20 in this
1272 rc
= check_key_signature( keyblock
, node
, NULL
);
1275 log_error( _("key %s: invalid revocation certificate"
1276 ": %s - rejected\n"), keystr(keyid
), g10_errstr(rc
));
1280 /* check whether we already have this */
1281 for(onode
=keyblock
->next
; onode
; onode
=onode
->next
) {
1282 if( onode
->pkt
->pkttype
== PKT_USER_ID
)
1284 else if( onode
->pkt
->pkttype
== PKT_SIGNATURE
1285 && !cmp_signatures(node
->pkt
->pkt
.signature
,
1286 onode
->pkt
->pkt
.signature
))
1289 goto leave
; /* yes, we already know about it */
1295 insert_kbnode( keyblock
, clone_kbnode(node
), 0 );
1297 /* and write the keyblock back */
1298 rc
= keydb_update_keyblock (hd
, keyblock
);
1300 log_error (_("error writing keyring `%s': %s\n"),
1301 keydb_get_resource_name (hd
), g10_errstr(rc
) );
1302 keydb_release (hd
); hd
= NULL
;
1306 char *p
=get_user_id_native (keyid
);
1307 log_info( _("key %s: \"%s\" revocation certificate imported\n"),
1313 /* If the key we just revoked was ultimately trusted, remove its
1314 ultimate trust. This doesn't stop the user from putting the
1315 ultimate trust back, but is a reasonable solution for now. */
1316 if(get_ownertrust(pk
)==TRUST_ULTIMATE
)
1317 clear_ownertrusts(pk
);
1319 revalidation_mark ();
1323 release_kbnode( keyblock
);
1324 free_public_key( pk
);
1330 * loop over the keyblock and check all self signatures.
1331 * Mark all user-ids with a self-signature by setting flag bit 0.
1332 * Mark all user-ids with an invalid self-signature by setting bit 1.
1333 * This works also for subkeys, here the subkey is marked. Invalid or
1334 * extra subkey sigs (binding or revocation) are marked for deletion.
1335 * non_self is set to true if there are any sigs other than self-sigs
1339 chk_self_sigs( const char *fname
, KBNODE keyblock
,
1340 PKT_public_key
*pk
, u32
*keyid
, int *non_self
)
1342 KBNODE n
,knode
=NULL
;
1345 u32 bsdate
=0,rsdate
=0;
1346 KBNODE bsnode
=NULL
,rsnode
=NULL
;
1348 for( n
=keyblock
; (n
= find_next_kbnode(n
, 0)); ) {
1349 if(n
->pkt
->pkttype
==PKT_PUBLIC_SUBKEY
)
1358 else if( n
->pkt
->pkttype
!= PKT_SIGNATURE
)
1360 sig
= n
->pkt
->pkt
.signature
;
1361 if( keyid
[0] == sig
->keyid
[0] && keyid
[1] == sig
->keyid
[1] ) {
1363 /* This just caches the sigs for later use. That way we
1364 import a fully-cached key which speeds things up. */
1365 if(!opt
.no_sig_cache
)
1366 check_key_signature(keyblock
,n
,NULL
);
1368 if( IS_UID_SIG(sig
) || IS_UID_REV(sig
) )
1370 KBNODE unode
= find_prev_kbnode( keyblock
, n
, PKT_USER_ID
);
1373 log_error( _("key %s: no user ID for signature\n"),
1375 return -1; /* the complete keyblock is invalid */
1378 /* If it hasn't been marked valid yet, keep trying */
1379 if(!(unode
->flag
&1)) {
1380 rc
= check_key_signature( keyblock
, n
, NULL
);
1385 char *p
=utf8_to_native(unode
->pkt
->pkt
.user_id
->name
,
1386 strlen(unode
->pkt
->pkt
.user_id
->name
),0);
1387 log_info( rc
== G10ERR_PUBKEY_ALGO
?
1388 _("key %s: unsupported public key "
1389 "algorithm on user ID \"%s\"\n"):
1390 _("key %s: invalid self-signature "
1391 "on user ID \"%s\"\n"),
1397 unode
->flag
|= 1; /* mark that signature checked */
1400 else if( sig
->sig_class
== 0x18 ) {
1401 /* Note that this works based solely on the timestamps
1402 like the rest of gpg. If the standard gets
1403 revocation targets, this may need to be revised. */
1408 log_info( _("key %s: no subkey for key binding\n"),
1410 n
->flag
|= 4; /* delete this */
1414 rc
= check_key_signature( keyblock
, n
, NULL
);
1418 log_info(rc
== G10ERR_PUBKEY_ALGO
?
1419 _("key %s: unsupported public key"
1421 _("key %s: invalid subkey binding\n"),
1427 /* It's valid, so is it newer? */
1428 if(sig
->timestamp
>=bsdate
) {
1429 knode
->flag
|= 1; /* the subkey is valid */
1432 bsnode
->flag
|=4; /* Delete the last binding
1433 sig since this one is
1436 log_info(_("key %s: removed multiple subkey"
1437 " binding\n"),keystr(keyid
));
1441 bsdate
=sig
->timestamp
;
1444 n
->flag
|=4; /* older */
1448 else if( sig
->sig_class
== 0x28 ) {
1449 /* We don't actually mark the subkey as revoked right
1450 now, so just check that the revocation sig is the
1451 most recent valid one. Note that we don't care if
1452 the binding sig is newer than the revocation sig.
1453 See the comment in getkey.c:merge_selfsigs_subkey for
1458 log_info( _("key %s: no subkey for key revocation\n"),
1460 n
->flag
|= 4; /* delete this */
1464 rc
= check_key_signature( keyblock
, n
, NULL
);
1468 log_info(rc
== G10ERR_PUBKEY_ALGO
?
1469 _("key %s: unsupported public"
1470 " key algorithm\n"):
1471 _("key %s: invalid subkey revocation\n"),
1477 /* It's valid, so is it newer? */
1478 if(sig
->timestamp
>=rsdate
)
1482 rsnode
->flag
|=4; /* Delete the last revocation
1483 sig since this one is
1486 log_info(_("key %s: removed multiple subkey"
1487 " revocation\n"),keystr(keyid
));
1491 rsdate
=sig
->timestamp
;
1494 n
->flag
|=4; /* older */
1507 * delete all parts which are invalid and those signatures whose
1508 * public key algorithm is not available in this implemenation;
1509 * but consider RSA as valid, because parse/build_packets knows
1511 * returns: true if at least one valid user-id is left over.
1514 delete_inv_parts( const char *fname
, KBNODE keyblock
,
1515 u32
*keyid
, unsigned int options
)
1518 int nvalid
=0, uid_seen
=0, subkey_seen
=0;
1520 for(node
=keyblock
->next
; node
; node
= node
->next
) {
1521 if( node
->pkt
->pkttype
== PKT_USER_ID
) {
1523 if( (node
->flag
& 2) || !(node
->flag
& 1) ) {
1526 char *p
=utf8_to_native(node
->pkt
->pkt
.user_id
->name
,
1527 node
->pkt
->pkt
.user_id
->len
,0);
1528 log_info( _("key %s: skipped user ID \"%s\"\n"),
1532 delete_kbnode( node
); /* the user-id */
1533 /* and all following packets up to the next user-id */
1535 && node
->next
->pkt
->pkttype
!= PKT_USER_ID
1536 && node
->next
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
1537 && node
->next
->pkt
->pkttype
!= PKT_SECRET_SUBKEY
){
1538 delete_kbnode( node
->next
);
1545 else if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1546 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1547 if( (node
->flag
& 2) || !(node
->flag
& 1) ) {
1549 log_info( _("key %s: skipped subkey\n"),keystr(keyid
));
1551 delete_kbnode( node
); /* the subkey */
1552 /* and all following signature packets */
1554 && node
->next
->pkt
->pkttype
== PKT_SIGNATURE
) {
1555 delete_kbnode( node
->next
);
1562 else if (node
->pkt
->pkttype
== PKT_SIGNATURE
1563 && openpgp_pk_test_algo (node
->pkt
->pkt
.signature
->pubkey_algo
)
1564 && node
->pkt
->pkt
.signature
->pubkey_algo
!= PUBKEY_ALGO_RSA
)
1565 delete_kbnode( node
); /* build_packet() can't handle this */
1566 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
&&
1567 !node
->pkt
->pkt
.signature
->flags
.exportable
&&
1568 !(options
&IMPORT_LOCAL_SIGS
) &&
1569 seckey_available( node
->pkt
->pkt
.signature
->keyid
) )
1571 /* here we violate the rfc a bit by still allowing
1572 * to import non-exportable signature when we have the
1573 * the secret key used to create this signature - it
1574 * seems that this makes sense */
1576 log_info( _("key %s: non exportable signature"
1577 " (class 0x%02X) - skipped\n"),
1578 keystr(keyid
), node
->pkt
->pkt
.signature
->sig_class
);
1579 delete_kbnode( node
);
1581 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
1582 && node
->pkt
->pkt
.signature
->sig_class
== 0x20 ) {
1586 log_info( _("key %s: revocation certificate"
1587 " at wrong place - skipped\n"),keystr(keyid
));
1588 delete_kbnode( node
);
1591 /* If the revocation cert is from a different key than
1592 the one we're working on don't check it - it's
1593 probably from a revocation key and won't be
1594 verifiable with this key anyway. */
1596 if(node
->pkt
->pkt
.signature
->keyid
[0]==keyid
[0] &&
1597 node
->pkt
->pkt
.signature
->keyid
[1]==keyid
[1])
1599 int rc
= check_key_signature( keyblock
, node
, NULL
);
1603 log_info( _("key %s: invalid revocation"
1604 " certificate: %s - skipped\n"),
1605 keystr(keyid
), g10_errstr(rc
));
1606 delete_kbnode( node
);
1611 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
&&
1612 (node
->pkt
->pkt
.signature
->sig_class
== 0x18 ||
1613 node
->pkt
->pkt
.signature
->sig_class
== 0x28) &&
1617 log_info( _("key %s: subkey signature"
1618 " in wrong place - skipped\n"), keystr(keyid
));
1619 delete_kbnode( node
);
1621 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
1622 && !IS_CERT(node
->pkt
->pkt
.signature
))
1625 log_info(_("key %s: unexpected signature class (0x%02X) -"
1626 " skipped\n"),keystr(keyid
),
1627 node
->pkt
->pkt
.signature
->sig_class
);
1628 delete_kbnode(node
);
1630 else if( (node
->flag
& 4) ) /* marked for deletion */
1631 delete_kbnode( node
);
1634 /* note: because keyblock is the public key, it is never marked
1635 * for deletion and so keyblock cannot change */
1636 commit_kbnode( &keyblock
);
1642 * It may happen that the imported keyblock has duplicated user IDs.
1643 * We check this here and collapse those user IDs together with their
1645 * Returns: True if the keyblock hash changed.
1648 collapse_uids( KBNODE
*keyblock
)
1655 for( n
= *keyblock
; n
; n
= n
->next
) {
1656 if( n
->pkt
->pkttype
!= PKT_USER_ID
)
1658 for( n2
= n
->next
; n2
; n2
= n2
->next
) {
1659 if( n2
->pkt
->pkttype
== PKT_USER_ID
1660 && !cmp_user_ids( n
->pkt
->pkt
.user_id
,
1661 n2
->pkt
->pkt
.user_id
) ) {
1662 /* found a duplicate */
1665 || n2
->next
->pkt
->pkttype
== PKT_USER_ID
1666 || n2
->next
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1667 || n2
->next
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1668 /* no more signatures: delete the user ID
1670 remove_kbnode( keyblock
, n2
);
1673 /* The simple approach: Move one signature and
1674 * then start over to delete the next one :-( */
1675 move_kbnode( keyblock
, n2
->next
, n
->next
);
1685 /* now we may have duplicate signatures on one user ID: fix this */
1686 for( in_uid
= 0, n
= *keyblock
; n
; n
= n
->next
) {
1687 if( n
->pkt
->pkttype
== PKT_USER_ID
)
1689 else if( n
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1690 || n
->pkt
->pkttype
== PKT_SECRET_SUBKEY
)
1696 for( ; n2
; n2
= n2
->next
) {
1697 if( n2
->pkt
->pkttype
== PKT_USER_ID
1698 || n2
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1699 || n2
->pkt
->pkttype
== PKT_SECRET_SUBKEY
)
1701 if( n2
->pkt
->pkttype
!= PKT_SIGNATURE
)
1705 else if( !cmp_signatures( ncmp
->pkt
->pkt
.signature
,
1706 n2
->pkt
->pkt
.signature
)) {
1707 remove_kbnode( keyblock
, n2
);
1711 n2
= ncmp
? ncmp
->next
: NULL
;
1718 const char *key
="???";
1720 if( (n
= find_kbnode( *keyblock
, PKT_PUBLIC_KEY
)) )
1721 key
=keystr_from_pk(n
->pkt
->pkt
.public_key
);
1722 else if( (n
= find_kbnode( *keyblock
, PKT_SECRET_KEY
)) )
1723 key
=keystr_from_sk(n
->pkt
->pkt
.secret_key
);
1725 log_info(_("key %s: duplicated user ID detected - merged\n"),key
);
1731 /* Check for a 0x20 revocation from a revocation key that is not
1732 present. This may be called without the benefit of merge_xxxx so
1733 you can't rely on pk->revkey and friends. */
1735 revocation_present(KBNODE keyblock
)
1738 PKT_public_key
*pk
=keyblock
->pkt
->pkt
.public_key
;
1740 for(onode
=keyblock
->next
;onode
;onode
=onode
->next
)
1742 /* If we reach user IDs, we're done. */
1743 if(onode
->pkt
->pkttype
==PKT_USER_ID
)
1746 if(onode
->pkt
->pkttype
==PKT_SIGNATURE
&&
1747 onode
->pkt
->pkt
.signature
->sig_class
==0x1F &&
1748 onode
->pkt
->pkt
.signature
->revkey
)
1751 PKT_signature
*sig
=onode
->pkt
->pkt
.signature
;
1753 for(idx
=0;idx
<sig
->numrevkeys
;idx
++)
1757 keyid_from_fingerprint(sig
->revkey
[idx
]->fpr
,
1758 MAX_FINGERPRINT_LEN
,keyid
);
1760 for(inode
=keyblock
->next
;inode
;inode
=inode
->next
)
1762 /* If we reach user IDs, we're done. */
1763 if(inode
->pkt
->pkttype
==PKT_USER_ID
)
1766 if(inode
->pkt
->pkttype
==PKT_SIGNATURE
&&
1767 inode
->pkt
->pkt
.signature
->sig_class
==0x20 &&
1768 inode
->pkt
->pkt
.signature
->keyid
[0]==keyid
[0] &&
1769 inode
->pkt
->pkt
.signature
->keyid
[1]==keyid
[1])
1771 /* Okay, we have a revocation key, and a
1772 revocation issued by it. Do we have the key
1776 rc
=get_pubkey_byfprint_fast (NULL
,sig
->revkey
[idx
]->fpr
,
1777 MAX_FINGERPRINT_LEN
);
1778 if(rc
==G10ERR_NO_PUBKEY
|| rc
==G10ERR_UNU_PUBKEY
)
1780 char *tempkeystr
=xstrdup(keystr_from_pk(pk
));
1782 /* No, so try and get it */
1784 && (opt
.keyserver_options
.options
1785 & KEYSERVER_AUTO_KEY_RETRIEVE
))
1787 log_info(_("WARNING: key %s may be revoked:"
1788 " fetching revocation key %s\n"),
1789 tempkeystr
,keystr(keyid
));
1790 keyserver_import_fprint(sig
->revkey
[idx
]->fpr
,
1791 MAX_FINGERPRINT_LEN
,
1794 /* Do we have it now? */
1795 rc
=get_pubkey_byfprint_fast (NULL
,
1796 sig
->revkey
[idx
]->fpr
,
1797 MAX_FINGERPRINT_LEN
);
1800 if(rc
==G10ERR_NO_PUBKEY
|| rc
==G10ERR_UNU_PUBKEY
)
1801 log_info(_("WARNING: key %s may be revoked:"
1802 " revocation key %s not present.\n"),
1803 tempkeystr
,keystr(keyid
));
1815 * compare and merge the blocks
1817 * o compare the signatures: If we already have this signature, check
1818 * that they compare okay; if not, issue a warning and ask the user.
1819 * o Simply add the signature. Can't verify here because we may not have
1820 * the signature's public key yet; verification is done when putting it
1821 * into the trustdb, which is done automagically as soon as this pubkey
1823 * Note: We indicate newly inserted packets with flag bit 0
1826 merge_blocks( const char *fname
, KBNODE keyblock_orig
, KBNODE keyblock
,
1827 u32
*keyid
, int *n_uids
, int *n_sigs
, int *n_subk
)
1832 /* 1st: handle revocation certificates */
1833 for(node
=keyblock
->next
; node
; node
=node
->next
) {
1834 if( node
->pkt
->pkttype
== PKT_USER_ID
)
1836 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
1837 && node
->pkt
->pkt
.signature
->sig_class
== 0x20 ) {
1838 /* check whether we already have this */
1840 for(onode
=keyblock_orig
->next
; onode
; onode
=onode
->next
) {
1841 if( onode
->pkt
->pkttype
== PKT_USER_ID
)
1843 else if( onode
->pkt
->pkttype
== PKT_SIGNATURE
1844 && onode
->pkt
->pkt
.signature
->sig_class
== 0x20
1845 && !cmp_signatures(onode
->pkt
->pkt
.signature
,
1846 node
->pkt
->pkt
.signature
))
1853 KBNODE n2
= clone_kbnode(node
);
1854 insert_kbnode( keyblock_orig
, n2
, 0 );
1859 char *p
=get_user_id_native (keyid
);
1860 log_info(_("key %s: \"%s\" revocation"
1861 " certificate added\n"), keystr(keyid
),p
);
1868 /* 2nd: merge in any direct key (0x1F) sigs */
1869 for(node
=keyblock
->next
; node
; node
=node
->next
) {
1870 if( node
->pkt
->pkttype
== PKT_USER_ID
)
1872 else if( node
->pkt
->pkttype
== PKT_SIGNATURE
1873 && node
->pkt
->pkt
.signature
->sig_class
== 0x1F ) {
1874 /* check whether we already have this */
1876 for(onode
=keyblock_orig
->next
; onode
; onode
=onode
->next
) {
1877 if( onode
->pkt
->pkttype
== PKT_USER_ID
)
1879 else if( onode
->pkt
->pkttype
== PKT_SIGNATURE
1880 && onode
->pkt
->pkt
.signature
->sig_class
== 0x1F
1881 && !cmp_signatures(onode
->pkt
->pkt
.signature
,
1882 node
->pkt
->pkt
.signature
)) {
1889 KBNODE n2
= clone_kbnode(node
);
1890 insert_kbnode( keyblock_orig
, n2
, 0 );
1894 log_info( _("key %s: direct key signature added\n"),
1900 /* 3rd: try to merge new certificates in */
1901 for(onode
=keyblock_orig
->next
; onode
; onode
=onode
->next
) {
1902 if( !(onode
->flag
& 1) && onode
->pkt
->pkttype
== PKT_USER_ID
) {
1903 /* find the user id in the imported keyblock */
1904 for(node
=keyblock
->next
; node
; node
=node
->next
)
1905 if( node
->pkt
->pkttype
== PKT_USER_ID
1906 && !cmp_user_ids( onode
->pkt
->pkt
.user_id
,
1907 node
->pkt
->pkt
.user_id
) )
1909 if( node
) { /* found: merge */
1910 rc
= merge_sigs( onode
, node
, n_sigs
, fname
, keyid
);
1917 /* 4th: add new user-ids */
1918 for(node
=keyblock
->next
; node
; node
=node
->next
) {
1919 if( node
->pkt
->pkttype
== PKT_USER_ID
) {
1920 /* do we have this in the original keyblock */
1921 for(onode
=keyblock_orig
->next
; onode
; onode
=onode
->next
)
1922 if( onode
->pkt
->pkttype
== PKT_USER_ID
1923 && !cmp_user_ids( onode
->pkt
->pkt
.user_id
,
1924 node
->pkt
->pkt
.user_id
) )
1926 if( !onode
) { /* this is a new user id: append */
1927 rc
= append_uid( keyblock_orig
, node
, n_sigs
, fname
, keyid
);
1935 /* 5th: add new subkeys */
1936 for(node
=keyblock
->next
; node
; node
=node
->next
) {
1938 if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
1939 /* do we have this in the original keyblock? */
1940 for(onode
=keyblock_orig
->next
; onode
; onode
=onode
->next
)
1941 if( onode
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1942 && !cmp_public_keys( onode
->pkt
->pkt
.public_key
,
1943 node
->pkt
->pkt
.public_key
) )
1945 if( !onode
) { /* this is a new subkey: append */
1946 rc
= append_key( keyblock_orig
, node
, n_sigs
, fname
, keyid
);
1952 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1953 /* do we have this in the original keyblock? */
1954 for(onode
=keyblock_orig
->next
; onode
; onode
=onode
->next
)
1955 if( onode
->pkt
->pkttype
== PKT_SECRET_SUBKEY
1956 && !cmp_secret_keys( onode
->pkt
->pkt
.secret_key
,
1957 node
->pkt
->pkt
.secret_key
) )
1959 if( !onode
) { /* this is a new subkey: append */
1960 rc
= append_key( keyblock_orig
, node
, n_sigs
, fname
, keyid
);
1968 /* 6th: merge subkey certificates */
1969 for(onode
=keyblock_orig
->next
; onode
; onode
=onode
->next
) {
1970 if( !(onode
->flag
& 1)
1971 && ( onode
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1972 || onode
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) ) {
1973 /* find the subkey in the imported keyblock */
1974 for(node
=keyblock
->next
; node
; node
=node
->next
) {
1975 if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
1976 && !cmp_public_keys( onode
->pkt
->pkt
.public_key
,
1977 node
->pkt
->pkt
.public_key
) )
1979 else if( node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
1980 && !cmp_secret_keys( onode
->pkt
->pkt
.secret_key
,
1981 node
->pkt
->pkt
.secret_key
) )
1984 if( node
) { /* found: merge */
1985 rc
= merge_keysigs( onode
, node
, n_sigs
, fname
, keyid
);
1998 * append the userid starting with NODE and all signatures to KEYBLOCK.
2001 append_uid( KBNODE keyblock
, KBNODE node
, int *n_sigs
,
2002 const char *fname
, u32
*keyid
)
2004 KBNODE n
, n_where
=NULL
;
2006 assert(node
->pkt
->pkttype
== PKT_USER_ID
);
2008 /* find the position */
2009 for( n
= keyblock
; n
; n_where
= n
, n
= n
->next
) {
2010 if( n
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
2011 || n
->pkt
->pkttype
== PKT_SECRET_SUBKEY
)
2017 /* and append/insert */
2019 /* we add a clone to the original keyblock, because this
2020 * one is released first */
2021 n
= clone_kbnode(node
);
2023 insert_kbnode( n_where
, n
, 0 );
2027 add_kbnode( keyblock
, n
);
2030 if( n
->pkt
->pkttype
== PKT_SIGNATURE
)
2034 if( node
&& node
->pkt
->pkttype
!= PKT_SIGNATURE
)
2043 * Merge the sigs from SRC onto DST. SRC and DST are both a PKT_USER_ID.
2044 * (how should we handle comment packets here?)
2047 merge_sigs( KBNODE dst
, KBNODE src
, int *n_sigs
,
2048 const char *fname
, u32
*keyid
)
2053 assert(dst
->pkt
->pkttype
== PKT_USER_ID
);
2054 assert(src
->pkt
->pkttype
== PKT_USER_ID
);
2056 for(n
=src
->next
; n
&& n
->pkt
->pkttype
!= PKT_USER_ID
; n
= n
->next
) {
2057 if( n
->pkt
->pkttype
!= PKT_SIGNATURE
)
2059 if( n
->pkt
->pkt
.signature
->sig_class
== 0x18
2060 || n
->pkt
->pkt
.signature
->sig_class
== 0x28 )
2061 continue; /* skip signatures which are only valid on subkeys */
2063 for(n2
=dst
->next
; n2
&& n2
->pkt
->pkttype
!= PKT_USER_ID
; n2
= n2
->next
)
2064 if(!cmp_signatures(n
->pkt
->pkt
.signature
,n2
->pkt
->pkt
.signature
))
2070 /* This signature is new or newer, append N to DST.
2071 * We add a clone to the original keyblock, because this
2072 * one is released first */
2073 n2
= clone_kbnode(n
);
2074 insert_kbnode( dst
, n2
, PKT_SIGNATURE
);
2085 * Merge the sigs from SRC onto DST. SRC and DST are both a PKT_xxx_SUBKEY.
2088 merge_keysigs( KBNODE dst
, KBNODE src
, int *n_sigs
,
2089 const char *fname
, u32
*keyid
)
2094 assert( dst
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
2095 || dst
->pkt
->pkttype
== PKT_SECRET_SUBKEY
);
2097 for(n
=src
->next
; n
; n
= n
->next
) {
2098 if( n
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
2099 || n
->pkt
->pkttype
== PKT_PUBLIC_KEY
)
2101 if( n
->pkt
->pkttype
!= PKT_SIGNATURE
)
2104 for(n2
=dst
->next
; n2
; n2
= n2
->next
){
2105 if( n2
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
2106 || n2
->pkt
->pkttype
== PKT_PUBLIC_KEY
)
2108 if( n2
->pkt
->pkttype
== PKT_SIGNATURE
2109 && n
->pkt
->pkt
.signature
->keyid
[0]
2110 == n2
->pkt
->pkt
.signature
->keyid
[0]
2111 && n
->pkt
->pkt
.signature
->keyid
[1]
2112 == n2
->pkt
->pkt
.signature
->keyid
[1]
2113 && n
->pkt
->pkt
.signature
->timestamp
2114 <= n2
->pkt
->pkt
.signature
->timestamp
2115 && n
->pkt
->pkt
.signature
->sig_class
2116 == n2
->pkt
->pkt
.signature
->sig_class
) {
2122 /* This signature is new or newer, append N to DST.
2123 * We add a clone to the original keyblock, because this
2124 * one is released first */
2125 n2
= clone_kbnode(n
);
2126 insert_kbnode( dst
, n2
, PKT_SIGNATURE
);
2137 * append the subkey starting with NODE and all signatures to KEYBLOCK.
2138 * Mark all new and copied packets by setting flag bit 0.
2141 append_key( KBNODE keyblock
, KBNODE node
, int *n_sigs
,
2142 const char *fname
, u32
*keyid
)
2146 assert( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
2147 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
);
2150 /* we add a clone to the original keyblock, because this
2151 * one is released first */
2152 n
= clone_kbnode(node
);
2153 add_kbnode( keyblock
, n
);
2156 if( n
->pkt
->pkttype
== PKT_SIGNATURE
)
2160 if( node
&& node
->pkt
->pkttype
!= PKT_SIGNATURE
)
2169 /* Walk a public keyblock and produce a secret keyblock out of it.
2170 Instead of inserting the secret key parameters (which we don't
2171 have), we insert a stub. */
2173 pub_to_sec_keyblock (KBNODE pub_keyblock
)
2175 KBNODE pubnode
, secnode
;
2176 KBNODE sec_keyblock
= NULL
;
2177 KBNODE walkctx
= NULL
;
2179 while((pubnode
= walk_kbnode (pub_keyblock
,&walkctx
,0)))
2181 if (pubnode
->pkt
->pkttype
== PKT_PUBLIC_KEY
2182 || pubnode
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
)
2184 /* Make a secret key. We only need to convert enough to
2185 write the keyblock out. */
2186 PKT_public_key
*pk
= pubnode
->pkt
->pkt
.public_key
;
2187 PACKET
*pkt
= xmalloc_clear (sizeof *pkt
);
2188 PKT_secret_key
*sk
= xmalloc_clear (sizeof *sk
);
2191 if (pubnode
->pkt
->pkttype
== PKT_PUBLIC_KEY
)
2192 pkt
->pkttype
= PKT_SECRET_KEY
;
2194 pkt
->pkttype
= PKT_SECRET_SUBKEY
;
2196 pkt
->pkt
.secret_key
= sk
;
2198 copy_public_parts_to_secret_key ( pk
, sk
);
2199 sk
->version
= pk
->version
;
2200 sk
->timestamp
= pk
->timestamp
;
2202 n
= pubkey_get_npkey (pk
->pubkey_algo
);
2204 n
= 1; /* Unknown number of parameters, however the data
2205 is stored in the first mpi. */
2206 for (i
=0; i
< n
; i
++ )
2207 sk
->skey
[i
] = mpi_copy (pk
->pkey
[i
]);
2209 sk
->is_protected
= 1;
2210 sk
->protect
.s2k
.mode
= 1001;
2212 secnode
= new_kbnode (pkt
);
2216 secnode
= clone_kbnode (pubnode
);
2220 sec_keyblock
= secnode
;
2222 add_kbnode (sec_keyblock
, secnode
);
2225 return sec_keyblock
;
2229 /* Walk over the secret keyring SEC_KEYBLOCK and update any simple
2230 stub keys with the serial number SNNUM of the card if one of the
2231 fingerprints FPR1, FPR2 or FPR3 match. Print a note if the key is
2232 a duplicate (may happen in case of backed uped keys).
2234 Returns: True if anything changed.
2237 update_sec_keyblock_with_cardinfo (KBNODE sec_keyblock
,
2238 const unsigned char *fpr1
,
2239 const unsigned char *fpr2
,
2240 const unsigned char *fpr3
,
2241 const char *serialnostr
)
2244 KBNODE walkctx
= NULL
;
2246 byte array
[MAX_FINGERPRINT_LEN
];
2251 while((node
= walk_kbnode (sec_keyblock
, &walkctx
, 0)))
2253 if (node
->pkt
->pkttype
!= PKT_SECRET_KEY
2254 && node
->pkt
->pkttype
!= PKT_SECRET_SUBKEY
)
2256 sk
= node
->pkt
->pkt
.secret_key
;
2258 fingerprint_from_sk (sk
, array
, &n
);
2260 continue; /* Can't be a card key. */
2261 if ( !((fpr1
&& !memcmp (array
, fpr1
, 20))
2262 || (fpr2
&& !memcmp (array
, fpr2
, 20))
2263 || (fpr3
&& !memcmp (array
, fpr3
, 20))) )
2264 continue; /* No match. */
2266 if (sk
->is_protected
== 1 && sk
->protect
.s2k
.mode
== 1001)
2268 /* Standard case: migrate that stub to a key stub. */
2269 sk
->protect
.s2k
.mode
= 1002;
2271 for (sk
->protect
.ivlen
=0; sk
->protect
.ivlen
< 16 && *s
&& s
[1];
2272 sk
->protect
.ivlen
++, s
+= 2)
2273 sk
->protect
.iv
[sk
->protect
.ivlen
] = xtoi_2 (s
);
2276 else if (sk
->is_protected
== 1 && sk
->protect
.s2k
.mode
== 1002)
2279 for (sk
->protect
.ivlen
=0; sk
->protect
.ivlen
< 16 && *s
&& s
[1];
2280 sk
->protect
.ivlen
++, s
+= 2)
2281 if (sk
->protect
.iv
[sk
->protect
.ivlen
] != xtoi_2 (s
))
2283 log_info (_("NOTE: a key's S/N does not "
2284 "match the card's one\n"));
2290 if (node
->pkt
->pkttype
!= PKT_SECRET_KEY
)
2291 log_info (_("NOTE: primary key is online and stored on card\n"));
2293 log_info (_("NOTE: secondary key is online and stored on card\n"));
2302 /* Check whether a secret key stub exists for the public key PK. If
2303 not create such a stub key and store it into the secring. If it
2304 exists, add appropriate subkey stubs and update the secring.
2305 Return 0 if the key could be created. */
2307 auto_create_card_key_stub ( const char *serialnostr
,
2308 const unsigned char *fpr1
,
2309 const unsigned char *fpr2
,
2310 const unsigned char *fpr3
)
2312 KBNODE pub_keyblock
;
2313 KBNODE sec_keyblock
;
2317 /* We only want to do this for an OpenPGP card. */
2318 if (!serialnostr
|| strncmp (serialnostr
, "D27600012401", 12)
2319 || strlen (serialnostr
) != 32 )
2320 return G10ERR_GENERAL
;
2322 /* First get the public keyring from any of the provided fingerprints. */
2323 if ( (fpr1
&& !get_keyblock_byfprint (&pub_keyblock
, fpr1
, 20))
2324 || (fpr2
&& !get_keyblock_byfprint (&pub_keyblock
, fpr2
, 20))
2325 || (fpr3
&& !get_keyblock_byfprint (&pub_keyblock
, fpr3
, 20)))
2328 return G10ERR_GENERAL
;
2332 /* Now check whether there is a secret keyring. */
2334 PKT_public_key
*pk
= pub_keyblock
->pkt
->pkt
.public_key
;
2335 byte afp
[MAX_FINGERPRINT_LEN
];
2338 fingerprint_from_pk (pk
, afp
, &an
);
2339 memset (afp
, 0, MAX_FINGERPRINT_LEN
);
2340 rc
= keydb_search_fpr (hd
, afp
);
2345 rc
= keydb_get_keyblock (hd
, &sec_keyblock
);
2348 log_error (_("error reading keyblock: %s\n"), g10_errstr(rc
) );
2349 rc
= G10ERR_GENERAL
;
2353 merge_keys_and_selfsig (sec_keyblock
);
2355 /* FIXME: We need to add new subkeys first. */
2356 if (update_sec_keyblock_with_cardinfo (sec_keyblock
,
2360 rc
= keydb_update_keyblock (hd
, sec_keyblock
);
2362 log_error (_("error writing keyring `%s': %s\n"),
2363 keydb_get_resource_name (hd
), g10_errstr(rc
) );
2367 else /* A secret key does not exists - create it. */
2369 sec_keyblock
= pub_to_sec_keyblock (pub_keyblock
);
2370 update_sec_keyblock_with_cardinfo (sec_keyblock
,
2374 rc
= keydb_locate_writable (hd
, NULL
);
2377 log_error (_("no default secret keyring: %s\n"), g10_errstr (rc
));
2378 rc
= G10ERR_GENERAL
;
2382 rc
= keydb_insert_keyblock (hd
, sec_keyblock
);
2384 log_error (_("error writing keyring `%s': %s\n"),
2385 keydb_get_resource_name (hd
), g10_errstr(rc
) );
2389 release_kbnode (sec_keyblock
);
2390 release_kbnode (pub_keyblock
);