summaryrefslogtreecommitdiffstats
path: root/g10/import.c
blob: 3748433636dc1aaaec328b91975e28fe59fd2003 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
/* import.c
 *	Copyright (C) 1998 Free Software Foundation, Inc.
 *
 * This file is part of GNUPG.
 *
 * GNUPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GNUPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include "options.h"
#include "packet.h"
#include "errors.h"
#include "keydb.h"
#include "memory.h"
#include "util.h"
#include "trustdb.h"
#include "main.h"
#include "i18n.h"


static int read_block( IOBUF a, compress_filter_context_t *cfx,
			     PACKET **pending_pkt, KBNODE *ret_root );
static int import_one( const char *fname, KBNODE keyblock );
static int import_secret_one( const char *fname, KBNODE keyblock );
static int import_revoke_cert( const char *fname, KBNODE node );
static int chk_self_sigs( const char *fname, KBNODE keyblock,
			  PKT_public_key *pk, u32 *keyid );
static int delete_inv_parts( const char *fname, KBNODE keyblock, u32 *keyid );
static int merge_blocks( const char *fname, KBNODE keyblock_orig,
			 KBNODE keyblock, u32 *keyid,
			 int *n_uids, int *n_sigs, int *n_subk );
static int append_uid( KBNODE keyblock, KBNODE node, int *n_sigs,
			     const char *fname, u32 *keyid );
static int merge_sigs( KBNODE dst, KBNODE src, int *n_sigs,
			     const char *fname, u32 *keyid );


/****************
 * Import the public keys from the given filename. Input may be armored.
 * This function rejects all keys which are not validly self signed on at
 * least one userid. Only user ids which are self signed will be imported.
 * Other signatures are not checked.
 *
 * Actually this functtion does a merge. It works like this:
 *
 *  - get the keyblock
 *  - check self-signatures and remove all userids and their signatures
 *    without/invalid self-signatures.
 *  - reject the keyblock, if we have no valid userid.
 *  - See whether we have this key already in one of our pubrings.
 *    If not, simply add it to the default keyring.
 *  - Compare the key and the self-signatures of the new and the one in
 *    our keyring.  If they are different something weird is going on;
 *    ask what to do.
 *  - See whether we have only non-self-signature on one user id; if not
 *    ask the user what to do.
 *  - compare the signatures: If we already have this signature, check
 *    that they compare okay; if not, issue a warning and ask the user.
 *    (consider looking at the timestamp and use the newest?)
 *  - Simply add the signature.  Can't verify here because we may not have
 *    the signature's public key yet; verification is done when putting it
 *    into the trustdb, which is done automagically as soon as this pubkey
 *    is used.
 *  - Proceed with next signature.
 *
 *  Key revocation certificates have special handling.
 *
 */
int
import_keys( const char *fname )
{
    armor_filter_context_t afx;
    compress_filter_context_t cfx;
    PACKET *pending_pkt = NULL;
    IOBUF inp = NULL;
    KBNODE keyblock;
    int rc = 0;

    memset( &afx, 0, sizeof afx);
    memset( &cfx, 0, sizeof cfx);
    afx.only_keyblocks = 1;

    /* open file */
    inp = iobuf_open(fname);
    if( !fname )
	fname = "[stdin]";
    if( !inp ) {
	log_error_f(fname, _("can't open file: %s\n"), strerror(errno) );
	return G10ERR_OPEN_FILE;
    }

    if( !opt.no_armor ) /* armored reading is not disabled */
	iobuf_push_filter( inp, armor_filter, &afx );

    while( !(rc = read_block( inp, &cfx, &pending_pkt, &keyblock) )) {
	if( keyblock->pkt->pkttype == PKT_PUBLIC_KEY )
	    rc = import_one( fname, keyblock );
	else if( keyblock->pkt->pkttype == PKT_SECRET_KEY )
	    rc = import_secret_one( fname, keyblock );
	else if( keyblock->pkt->pkttype == PKT_SIGNATURE
		 && keyblock->pkt->pkt.signature->sig_class == 0x20 )
	    rc = import_revoke_cert( fname, keyblock );
	else {
	    log_info_f(fname, _("skipping block of type %d\n"),
					    keyblock->pkt->pkttype );
	}
	release_kbnode(keyblock);
	if( rc )
	    break;
    }
    if( rc == -1 )
	rc = 0;
    else if( rc && rc != G10ERR_INV_KEYRING )
	log_error_f( fname, _("read error: %s\n"), g10_errstr(rc));

    iobuf_close(inp);
    return rc;
}


/****************
 * Read the next keyblock from stream A, CFX is used to handle
 * compressed keyblocks. PENDING_PKT should be initialzed to NULL
 * and not chnaged form the caller.
 * Retunr: 0 = okay, -1 no more blocks or another errorcode.
 */
static int
read_block( IOBUF a, compress_filter_context_t *cfx,
	    PACKET **pending_pkt, KBNODE *ret_root )
{
    int rc;
    PACKET *pkt;
    KBNODE root = NULL;
    int in_cert;

    if( *pending_pkt ) {
	root = new_kbnode( *pending_pkt );
	*pending_pkt = NULL;
	in_cert = 1;
    }
    else
	in_cert = 0;
    pkt = m_alloc( sizeof *pkt );
    init_packet(pkt);
    while( (rc=parse_packet(a, pkt)) != -1 ) {
	if( rc ) {  /* ignore errors */
	    if( rc != G10ERR_UNKNOWN_PACKET ) {
		log_error("read_block: read error: %s\n", g10_errstr(rc) );
		rc = G10ERR_INV_KEYRING;
		goto ready;
	    }
	    free_packet( pkt );
	    init_packet(pkt);
	    continue;
	}

	if( !root && pkt->pkttype == PKT_SIGNATURE
		  && pkt->pkt.signature->sig_class == 0x20 ) {
	    /* this is a revocation certificate which is handled
	     * in a special way */
	    root = new_kbnode( pkt );
	    pkt = NULL;
	    goto ready;
	}

	/* make a linked list of all packets */
	switch( pkt->pkttype ) {
	  case PKT_COMPRESSED:
	    if( pkt->pkt.compressed->algorithm < 1
		|| pkt->pkt.compressed->algorithm > 2 ) {
		rc = G10ERR_COMPR_ALGO;
		goto ready;
	    }
	    cfx->algo = pkt->pkt.compressed->algorithm;
	    pkt->pkt.compressed->buf = NULL;
	    iobuf_push_filter( a, compress_filter, cfx );
	    free_packet( pkt );
	    init_packet(pkt);
	    break;


	  case PKT_PUBLIC_KEY:
	  case PKT_SECRET_KEY:
	    if( in_cert ) { /* store this packet */
		*pending_pkt = pkt;
		pkt = NULL;
		goto ready;
	    }
	    in_cert = 1;
	  default:
	    if( in_cert ) {
		if( !root )
		    root = new_kbnode( pkt );
		else
		    add_kbnode( root, new_kbnode( pkt ) );
		pkt = m_alloc( sizeof *pkt );
	    }
	    init_packet(pkt);
	    break;
	}
    }
  ready:
    if( rc == -1 && root )
	rc = 0;

    if( rc )
	release_kbnode( root );
    else
	*ret_root = root;
    free_packet( pkt );
    m_free( pkt );
    return rc;
}


/****************
 * Try to import one keyblock.	Return an error only in serious cases, but
 * never for an invalid keyblock.  It uses log_error to increase the
 * internal errorcount, so that invalid input can be detected by programs
 * which called g10.
 */
static int
import_one( const char *fname, KBNODE keyblock )
{
    PKT_public_key *pk;
    PKT_public_key *pk_orig;
    KBNODE node, uidnode;
    KBNODE keyblock_orig = NULL;
    KBPOS kbpos;
    u32 keyid[2];
    int rc = 0;
    int new_key = 0;
    int mod_key = 0;

    /* get the key and print some info about it */
    node = find_kbnode( keyblock, PKT_PUBLIC_KEY );
    if( !node )
	BUG();

    pk = node->pkt->pkt.public_key;
    keyid_from_pk( pk, keyid );
    uidnode = find_next_kbnode( keyblock, PKT_USER_ID );

    if( opt.verbose ) {
	log_info_f( fname, "pub  %4u%c/%08lX %s   ",
		  nbits_from_pk( pk ),
		  pubkey_letter( pk->pubkey_algo ),
		  (ulong)keyid[1], datestr_from_pk(pk) );
	if( uidnode )
	    print_string( stderr, uidnode->pkt->pkt.user_id->name,
				  uidnode->pkt->pkt.user_id->len, 0 );
	putc('\n', stderr);
    }
    if( !uidnode ) {
	log_error_f(fname, _("key %08lX: no user id\n"), (ulong)keyid[1]);
	return 0;
    }

    clear_kbnode_flags( keyblock );
    rc = chk_self_sigs( fname, keyblock , pk, keyid );
    if( rc )
	return rc== -1? 0:rc;

    if( !delete_inv_parts( fname, keyblock, keyid ) ) {
	log_info_f( fname, _("key %08lX: no valid user ids\n"),
						    (ulong)keyid[1]);
	log_info(_("this may be caused by a missing self-signature\n"));
	return 0;
    }


    /* do we have this key already in one of our pubrings ? */
    pk_orig = m_alloc_clear( sizeof *pk_orig );
    rc = get_pubkey( pk_orig, keyid );
    if( rc && rc != G10ERR_NO_PUBKEY ) {
	log_error_f( fname, _("key %08lX: public key not found: %s\n"),
				(ulong)keyid[1], g10_errstr(rc));
    }
    else if( rc ) { /* insert this key */
	/* get default resource */
	if( get_keyblock_handle( NULL, 0, &kbpos ) ) {
	    log_error(_("no default public keyring\n"));
	    return G10ERR_GENERAL;
	}
	if( opt.verbose > 1 )
	    log_info_f( fname, _("writing to '%s'\n"),
				keyblock_resource_name(&kbpos) );
	if( (rc=lock_keyblock( &kbpos )) )
	    log_error_f( keyblock_resource_name(&kbpos),
			_("can't lock public keyring: %s\n"), g10_errstr(rc) );
	else if( (rc=insert_keyblock( &kbpos, keyblock )) )
	    log_error_f( keyblock_resource_name(&kbpos),
			_("can't write to keyring: %s\n"), g10_errstr(rc) );
	unlock_keyblock( &kbpos );
	/* we are ready */
	log_info_f( fname, _("key %08lX: public key imported\n"), (ulong)keyid[1]);
	new_key = 1;
    }
    else { /* merge */
	int n_uids, n_sigs, n_subk;

	/* Compare the original against the new key; just to be sure nothing
	 * weird is going on */
	if( cmp_public_keys( pk_orig, pk ) ) {
	    log_error_f( fname, _("key %08lX: doesn't match our copy\n"),
							  (ulong)keyid[1]);
	    rc = G10ERR_GENERAL;
	    goto leave;
	}

	/* See whether we have only non-self-signature on one user id; if not
	 * ask the user what to do. <--- fixme */

	/* now read the original keyblock */
	rc = find_keyblock_bypk( &kbpos, pk_orig );
	if( rc ) {
	    log_error_f(fname,
			_("key %08lX: can't locate original keyblock: %s\n"),
				     (ulong)keyid[1], g10_errstr(rc));
	    goto leave;
	}
	rc = read_keyblock( &kbpos, &keyblock_orig );
	if( rc ) {
	    log_error_f(fname,
			_("key %08lX: can't read original keyblock: %s\n"),
					    (ulong)keyid[1], g10_errstr(rc));
	    goto leave;
	}
	/* and try to merge the block */
	clear_kbnode_flags( keyblock_orig );
	clear_kbnode_flags( keyblock );
	n_uids = n_sigs = n_subk = 0;
	rc = merge_blocks( fname, keyblock_orig, keyblock,
				keyid, &n_uids, &n_sigs, &n_subk );
	if( rc )
	    goto leave;
	if( n_uids || n_sigs || n_subk ) {
	    mod_key = 1;
	    /* keyblock_orig has been updated; write */
	    if( opt.verbose > 1 )
		log_info_f(keyblock_resource_name(&kbpos),
				      _("writing keyblock\n"));
	    if( (rc=lock_keyblock( &kbpos )) )
		log_error_f(keyblock_resource_name(&kbpos),
			 _("can't lock public keyring: %s\n"), g10_errstr(rc) );
	    else if( (rc=update_keyblock( &kbpos, keyblock_orig )) )
		log_error_f( keyblock_resource_name(&kbpos),
			    _("can't write keyblock: %s\n"), g10_errstr(rc) );
	    unlock_keyblock( &kbpos );
	    /* we are ready */
	    if( n_uids == 1 )
		log_info_f(fname, _("key %08lX: 1 new user-id\n"),
					 (ulong)keyid[1]);
	    else if( n_uids )
		log_info_f(fname, _("key %08lX: %d new user-ids\n"),
					 (ulong)keyid[1], n_uids );
	    if( n_sigs == 1 )
		log_info_f(fname, _("key %08lX: 1 new signature\n"),
					 (ulong)keyid[1]);
	    else if( n_sigs )
		log_info_f(fname, _("key %08lX: %d new signatures\n"),
					 (ulong)keyid[1], n_sigs );
	    if( n_subk == 1 )
		log_info_f(fname, _("key %08lX: 1 new subkey\n"),
					 (ulong)keyid[1]);
	    else if( n_subk )
		log_info_f(fname, _("key %08lX: %d new subkeys\n"),
					 (ulong)keyid[1], n_subk );
	}
	else
	    log_info_f(fname, _("key %08lX: not changed\n"), (ulong)keyid[1] );
    }
    if( !rc ) {
	rc = query_trust_record( new_key? pk : pk_orig );
	if( rc && rc != -1 )
	    log_error("trustdb error: %s\n", g10_errstr(rc) );
	else if( rc == -1 ) { /* not found trustdb */
	    rc = insert_trust_record( new_key? pk : pk_orig );
	    if( rc )
		log_error("key %08lX: trustdb insert failed: %s\n",
					(ulong)keyid[1], g10_errstr(rc) );
	}
	else if( mod_key )
	    rc = update_trust_record( keyblock_orig );
	else
	    rc = clear_trust_checked_flag( new_key? pk : pk_orig );
    }

  leave:
    release_kbnode( keyblock_orig );
    free_public_key( pk_orig );
    return rc;
}


/****************
 * Ditto for secret keys.  Handling is simpler than for public keys.
 */
static int
import_secret_one( const char *fname, KBNODE keyblock )
{
    PKT_secret_key *sk;
    KBNODE node, uidnode;
    KBPOS kbpos;
    u32 keyid[2];
    int rc = 0;

    /* get the key and print some info about it */
    node = find_kbnode( keyblock, PKT_SECRET_KEY );
    if( !node )
	BUG();

    sk = node->pkt->pkt.secret_key;
    keyid_from_sk( sk, keyid );
    uidnode = find_next_kbnode( keyblock, PKT_USER_ID );

    if( opt.verbose ) {
	log_info_f(fname, "sec  %4u%c/%08lX %s   ",
		  nbits_from_sk( sk ),
		  pubkey_letter( sk->pubkey_algo ),
		  (ulong)keyid[1], datestr_from_sk(sk) );
	if( uidnode )
	    print_string( stderr, uidnode->pkt->pkt.user_id->name,
				  uidnode->pkt->pkt.user_id->len, 0 );
	putc('\n', stderr);
    }
    if( !uidnode ) {
	log_error_f(fname, _("key %08lX: no user id\n"), (ulong)keyid[1]);
	return 0;
    }

    clear_kbnode_flags( keyblock );

    /* do we have this key already in one of our secrings ? */
    rc = seckey_available( keyid );
    if( rc == G10ERR_NO_SECKEY ) { /* simply insert this key */
	/* get default resource */
	if( get_keyblock_handle( NULL, 1, &kbpos ) ) {
	    log_error("no default secret keyring\n");
	    return G10ERR_GENERAL;
	}
	if( opt.verbose > 1 )
	    log_info_f(keyblock_resource_name(&kbpos), _("writing keyblock\n"));
	if( (rc=lock_keyblock( &kbpos )) )
	    log_error_f( keyblock_resource_name(&kbpos),
		      _("can't lock secret keyring: %s\n"), g10_errstr(rc) );
	else if( (rc=insert_keyblock( &kbpos, keyblock )) )
	    log_error_f(keyblock_resource_name(&kbpos),
		      _("can't write keyring: %s\n"), g10_errstr(rc) );
	unlock_keyblock( &kbpos );
	/* we are ready */
	log_info_f(fname, _("key %08lX: secret key imported\n"), (ulong)keyid[1]);
    }
    else if( !rc ) { /* we can't merge secret keys */
	log_error_f(fname, _("key %08lX: already in secret keyring\n"),
						(ulong)keyid[1]);
    }
    else
	log_error_f(fname, _("key %08lX: secret key not found: %s\n"),
				(ulong)keyid[1], g10_errstr(rc));

    return rc;
}


/****************
 * Import a revocation certificate; this is a single signature packet.
 */
static int
import_revoke_cert( const char *fname, KBNODE node )
{
    PKT_public_key *pk=NULL;
    KBNODE onode, keyblock = NULL;
    KBPOS kbpos;
    u32 keyid[2];
    int rc = 0;

    assert( !node->next );
    assert( node->pkt->pkttype == PKT_SIGNATURE );
    assert( node->pkt->pkt.signature->sig_class == 0x20 );

    keyid[0] = node->pkt->pkt.signature->keyid[0];
    keyid[1] = node->pkt->pkt.signature->keyid[1];

    pk = m_alloc_clear( sizeof *pk );
    rc = get_pubkey( pk, keyid );
    if( rc == G10ERR_NO_PUBKEY ) {
	log_info_f(fname, _("key %08lX: no public key - "
		 "can't apply revocation certificate\n"), (ulong)keyid[1]);
	rc = 0;
	goto leave;
    }
    else if( rc ) {
	log_error_f(fname, _("key %08lX: public key not found: %s\n"),
				       (ulong)keyid[1], g10_errstr(rc));
	goto leave;
    }

    /* read the original keyblock */
    rc = find_keyblock_bypk( &kbpos, pk );
    if( rc ) {
	log_error_f(fname,
		_("key %08lX: can't locate original keyblock: %s\n"),
					(ulong)keyid[1], g10_errstr(rc));
	goto leave;
    }
    rc = read_keyblock( &kbpos, &keyblock );
    if( rc ) {
	log_error_f(fname,
		_("key %08lX: can't read original keyblock: %s\n"),
					(ulong)keyid[1], g10_errstr(rc));
	goto leave;
    }


    /* it is okay, that node is not in keyblock because
     * check_key_signature works fine for sig_class 0x20 in this
     * special case. */
    rc = check_key_signature( keyblock, node, NULL);
    if( rc ) {
	log_error_f(fname, _("key %08lX: invalid revocation certificate"
		  ": %s - rejected\n"), (ulong)keyid[1], g10_errstr(rc));
    }


    /* check whether we already have this */
    for(onode=keyblock->next; onode; onode=onode->next ) {
	if( onode->pkt->pkttype == PKT_USER_ID )
	    break;
	else if( onode->pkt->pkttype == PKT_SIGNATURE
		 && onode->pkt->pkt.signature->sig_class == 0x20
		 && keyid[0] == onode->pkt->pkt.signature->keyid[0]
		 && keyid[1] == onode->pkt->pkt.signature->keyid[1] ) {
	    rc = 0;
	    goto leave; /* yes, we already know about it */
	}
    }


    /* insert it */
    insert_kbnode( keyblock, clone_kbnode(node), 0 );

    /* and write the keyblock back */
    if( opt.verbose > 1 )
	log_info_f( keyblock_resource_name(&kbpos), _("writing keyblock\n"));
    if( (rc=lock_keyblock( &kbpos )) )
	log_error_f( keyblock_resource_name(&kbpos),
		    _("can't lock public keyring: %s\n"), g10_errstr(rc) );
    else if( (rc=update_keyblock( &kbpos, keyblock )) )
	log_error_f(keyblock_resource_name(&kbpos),
		    _("can't write keyblock: %s\n"), g10_errstr(rc) );
    unlock_keyblock( &kbpos );
    /* we are ready */
    log_info_f(fname, _("key %08lX: revocation certificate imported\n"),
					(ulong)keyid[1]);

  leave:
    release_kbnode( keyblock );
    free_public_key( pk );
    return rc;
}


/****************
 * loop over the keyblock and check all self signatures.
 * Mark all user-ids with a self-signature by setting flag bit 0.
 * Mark all user-ids with an invalid self-signature by setting bit 1.
 */
static int
chk_self_sigs( const char *fname, KBNODE keyblock,
	       PKT_public_key *pk, u32 *keyid )
{
    KBNODE n, unode;
    PKT_signature *sig;
    int rc;

    for( n=keyblock; (n = find_next_kbnode(n, 0)); ) {
	if( n->pkt->pkttype != PKT_SIGNATURE )
	    continue;
	sig = n->pkt->pkt.signature;
	if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] ) {
	    unode = find_prev_kbnode( keyblock, n, PKT_USER_ID );
	    if( !unode )  {
		log_error_f(fname, _("key %08lX: no user-id for signature\n"),
					(ulong)keyid[1]);
		return -1;  /* the complete keyblock is invalid */
	    }
	    rc = check_key_signature( keyblock, n, NULL);
	    if( rc ) {
		log_error_f( fname,  rc == G10ERR_PUBKEY_ALGO ?
			  _("key %08lX: unsupported public key algorithm\n"):
			  _("key %08lX: invalid self-signature\n"),
				 (ulong)keyid[1]);

		unode->flag |= 2; /* mark as invalid */
	    }
	    unode->flag |= 1; /* mark that user-id checked */
	}
    }
    return 0;
}

/****************
 * delete all parts which are invalid and those signatures whose
 * public key algorithm is not available in this implemenation;
 * but consider RSA as valid, because parse/build_packets knows
 * about it.
 * returns: true if at least one valid user-id is left over.
 */
static int
delete_inv_parts( const char *fname, KBNODE keyblock, u32 *keyid )
{
    KBNODE node;
    int nvalid=0, uid_seen=0;

    for(node=keyblock->next; node; node = node->next ) {
	if( node->pkt->pkttype == PKT_USER_ID ) {
	    uid_seen = 1;
	    if( (node->flag & 2) || !(node->flag & 1) ) {
		if( opt.verbose ) {
		    log_info_f(fname, _("key %08lX: skipped userid '"),
							 (ulong)keyid[1]);
		    print_string( stderr, node->pkt->pkt.user_id->name,
				      node->pkt->pkt.user_id->len, 0 );
		    fputs("'\n", stderr );
		}
		delete_kbnode( node ); /* the user-id */
		/* and all following packets up to the next user-id */
		while( node->next && node->next->pkt->pkttype != PKT_USER_ID ){
		    delete_kbnode( node->next );
		    node = node->next;
		}
	    }
	    else
		nvalid++;
	}
	else if( node->pkt->pkttype == PKT_SIGNATURE
		 && check_pubkey_algo( node->pkt->pkt.signature->pubkey_algo)
		 && node->pkt->pkt.signature->pubkey_algo != PUBKEY_ALGO_RSA )
	    delete_kbnode( node ); /* build_packet() can't handle this */
	else if( node->pkt->pkttype == PKT_SIGNATURE
		 && node->pkt->pkt.signature->sig_class == 0x20 )  {
	    if( uid_seen ) {
		log_error_f(fname, _("key %08lX: revocation certificate "
				     "at wrong place - skipped\n"),
				    (ulong)keyid[1]);
		delete_kbnode( node );
	    }
	    else {
		int rc = check_key_signature( keyblock, node, NULL);
		if( rc ) {
		    log_error_f(fname, _("key %08lX: invalid revocation "
			      "certificate: %s - skipped\n"),
			      (ulong)keyid[1], g10_errstr(rc));
		    delete_kbnode( node );
		}
	    }
	}
    }

    /* note: because keyblock is the public key, it is never marked
     * for deletion and so keyblock cannot change */
    commit_kbnode( &keyblock );
    return nvalid;
}


/****************
 * compare and merge the blocks
 *
 * o compare the signatures: If we already have this signature, check
 *   that they compare okay; if not, issue a warning and ask the user.
 *   FIXME: add the check that we don't have duplicate signatures and the
 *   warning in cases where the old/new signatures don't match.
 * o Simply add the signature.	Can't verify here because we may not have
 *   the signature's public key yet; verification is done when putting it
 *   into the trustdb, which is done automagically as soon as this pubkey
 *   is used.
 * Note: We indicate newly inserted packets with flag bit 0
 */
static int
merge_blocks( const char *fname, KBNODE keyblock_orig, KBNODE keyblock,
	      u32 *keyid, int *n_uids, int *n_sigs, int *n_subk )
{
    KBNODE onode, node;
    int rc, found;

    /* 1st: handle revocation certificates */
    for(node=keyblock->next; node; node=node->next ) {
	if( node->pkt->pkttype == PKT_USER_ID )
	    break;
	else if( node->pkt->pkttype == PKT_SIGNATURE
		 && node->pkt->pkt.signature->sig_class == 0x20 )  {
	    /* check whether we already have this */
	    found = 0;
	    for(onode=keyblock_orig->next; onode; onode=onode->next ) {
		if( onode->pkt->pkttype == PKT_USER_ID )
		    break;
		else if( onode->pkt->pkttype == PKT_SIGNATURE
			 && onode->pkt->pkt.signature->sig_class == 0x20
			 && node->pkt->pkt.signature->keyid[0]
			    == onode->pkt->pkt.signature->keyid[0]
			 && node->pkt->pkt.signature->keyid[1]
			    == onode->pkt->pkt.signature->keyid[1] ) {
		    found = 1;
		    break;
		}
	    }
	    if( !found ) {
		KBNODE n2 = clone_kbnode(node);
		insert_kbnode( keyblock_orig, n2, 0 );
		n2->flag |= 1;
		node->flag |= 1;
		log_info_f(fname, _("key %08lX: revocation certificate added\n"),
					 (ulong)keyid[1]);
	    }
	}
    }

    /* 2nd: try to merge new ones in */
    for(onode=keyblock_orig->next; onode; onode=onode->next ) {
	if( !(onode->flag & 1) && onode->pkt->pkttype == PKT_USER_ID) {
	    /* find the user id in the imported keyblock */
	    for(node=keyblock->next; node; node=node->next )
		if( !(node->flag & 1)
		    && node->pkt->pkttype == PKT_USER_ID
		    && !cmp_user_ids( onode->pkt->pkt.user_id,
					  node->pkt->pkt.user_id ) )
		    break;
	    if( node ) { /* found: merge */
		rc = merge_sigs( onode, node, n_sigs, fname, keyid );
		if( rc )
		    return rc;
	    }
	}
    }

    /* 3rd: add new user-ids */
    for(node=keyblock->next; node; node=node->next ) {
	if( !(node->flag & 1) && node->pkt->pkttype == PKT_USER_ID) {
	    /* do we have this in the original keyblock */
	    for(onode=keyblock_orig->next; onode; onode=onode->next )
		if( !(onode->flag & 1)
		    && onode->pkt->pkttype == PKT_USER_ID
		    && cmp_user_ids( onode->pkt->pkt.user_id,
				     node->pkt->pkt.user_id ) )
		    break;
	    if( !node ) { /* this is a new user id: append */
		rc = append_uid( keyblock_orig, node, n_sigs, fname, keyid);
		if( rc )
		    return rc;
		++*n_uids;
	    }
	}
    }

    /* 4th: add new subkeys */
    /* FIXME */

    return 0;
}


/****************
 * append the userid starting with NODE and all signatures to KEYBLOCK.
 * Mark all new and copied packets by setting flag bit 0.
 */
static int
append_uid( KBNODE keyblock, KBNODE node, int *n_sigs,
					  const char *fname, u32 *keyid )
{
    KBNODE n;

    assert(node->pkt->pkttype == PKT_USER_ID );
    /* at lease a self signature comes next to the user-id */
    if( node->next->pkt->pkttype == PKT_USER_ID ) {
	log_error_f(fname, _("key %08lX: our copy has no self-signature\n"),
						  (ulong)keyid[1]);
	return G10ERR_GENERAL;
    }

    for( ;node && node->pkt->pkttype != PKT_USER_ID; node = node->next ) {
	/* we add a clone to the original keyblock, because this
	 * one is released first */
	n = clone_kbnode(node);
	add_kbnode( keyblock, n );
	node->flag |= 1;
	n->flag |= 1;
	if( n->pkt->pkttype == PKT_SIGNATURE )
	    ++*n_sigs;
    }

    return 0;
}


/****************
 * Merge the sigs from SRC onto DST. SRC and DST are both a PKT_USER_ID.
 * (how should we handle comment packets here?)
 */
static int
merge_sigs( KBNODE dst, KBNODE src, int *n_sigs,
				    const char *fname, u32 *keyid )
{
    KBNODE n, n2;
    int found=0;

    assert(dst->pkt->pkttype == PKT_USER_ID );
    assert(src->pkt->pkttype == PKT_USER_ID );
    /* at least a self signature comes next to the user-ids */
    assert(src->next->pkt->pkttype != PKT_USER_ID );
    if( dst->next->pkt->pkttype == PKT_USER_ID ) {
	log_error_f(fname, _("key %08lX: our copy has no self-signature\n"),
						  (ulong)keyid[1]);
	return 0;
    }


    for(n=src->next; n && n->pkt->pkttype != PKT_USER_ID; n = n->next ) {
	if( n->pkt->pkttype != PKT_SIGNATURE )
	    continue;
	found = 0;
	for(n2=dst->next; n2 && n2->pkt->pkttype != PKT_USER_ID; n2 = n2->next)
	    if( n2->pkt->pkttype == PKT_SIGNATURE
		&& n->pkt->pkt.signature->keyid[0]
		   == n2->pkt->pkt.signature->keyid[0]
		&& n->pkt->pkt.signature->keyid[1]
		   == n2->pkt->pkt.signature->keyid[1] ) {
	    found++;
	    break;
	}

	if( found ) { /* we already have this signature */
	    /* Hmmm: should we compare the timestamp etc?
	     * but then we have first to see whether this signature is valid
	     * - or simply add it in such a case and let trustdb logic
	     * decide whether to remove the old one
	     */
	    continue;
	}

	/* This signature is new, append N to DST it.
	 * We add a clone to the original keyblock, because this
	 * one is released first */
	n2 = clone_kbnode(n);
	insert_kbnode( dst, n2, PKT_SIGNATURE );
	n2->flag |= 1;
	n->flag |= 1;
	++*n_sigs;
    }

    return 0;
}