~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to g10/import.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* import.c - Import OpenPGP key material
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3
 
 *               2003 Free Software Foundation, Inc.
 
1
/* import.c - import a key into our key storage.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
 
3
 *               2007 Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of GnuPG.
6
6
 *
7
7
 * GnuPG is free software; you can redistribute it and/or modify
8
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
 
9
 * the Free Software Foundation; either version 3 of the License, or
10
10
 * (at your option) any later version.
11
11
 *
12
12
 * GnuPG is distributed in the hope that it will be useful,
15
15
 * GNU General Public License for more details.
16
16
 *
17
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20
19
 */
21
20
 
22
21
#include <config.h>
26
25
#include <errno.h>
27
26
#include <assert.h>
28
27
 
 
28
#include "gpg.h"
29
29
#include "options.h"
30
30
#include "packet.h"
31
 
#include "errors.h"
 
31
#include "status.h"
32
32
#include "keydb.h"
33
 
#include "memory.h"
34
33
#include "util.h"
35
34
#include "trustdb.h"
36
35
#include "main.h"
54
53
    ulong secret_dups;
55
54
    ulong skipped_new_keys;
56
55
    ulong not_imported;
 
56
    ulong n_sigs_cleaned;
 
57
    ulong n_uids_cleaned;
57
58
};
58
59
 
59
60
 
60
 
static int import( iobuf_t inp, const char* fname,
61
 
                   struct stats_s *stats, unsigned int options );
62
 
static int read_block( iobuf_t a, PACKET **pending_pkt, KBNODE *ret_root );
 
61
static int import( IOBUF inp, const char* fname,struct stats_s *stats,
 
62
                   unsigned char **fpr,size_t *fpr_len,unsigned int options );
 
63
static int read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root );
63
64
static void revocation_present(KBNODE keyblock);
64
 
static int import_one( const char *fname, KBNODE keyblock,
65
 
                       struct stats_s *stats, unsigned int options);
 
65
static int import_one(const char *fname, KBNODE keyblock,struct stats_s *stats,
 
66
                      unsigned char **fpr,size_t *fpr_len,
 
67
                      unsigned int options,int from_sk);
66
68
static int import_secret_one( const char *fname, KBNODE keyblock,
67
69
                              struct stats_s *stats, unsigned int options);
68
70
static int import_revoke_cert( const char *fname, KBNODE node,
84
86
                             const char *fname, u32 *keyid );
85
87
 
86
88
int
87
 
parse_import_options(char *str,unsigned int *options)
 
89
parse_import_options(char *str,unsigned int *options,int noisy)
88
90
{
89
91
  struct parse_options import_opts[]=
90
92
    {
91
 
      {"allow-local-sigs",IMPORT_ALLOW_LOCAL_SIGS},
92
 
      {"repair-hkp-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG},
93
 
      {"repair-pks-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG},
94
 
      {"fast-import",IMPORT_FAST_IMPORT},
95
 
      {"convert-sk-to-pk",IMPORT_SK2PK},
96
 
      {NULL,0}
 
93
      {"import-local-sigs",IMPORT_LOCAL_SIGS,NULL,
 
94
       N_("import signatures that are marked as local-only")},
 
95
      {"repair-pks-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG,NULL,
 
96
       N_("repair damage from the pks keyserver during import")},
 
97
      {"fast-import",IMPORT_FAST,NULL,
 
98
       N_("do not update the trustdb after import")},
 
99
      {"convert-sk-to-pk",IMPORT_SK2PK,NULL,
 
100
       N_("create a public key when importing a secret key")},
 
101
      {"merge-only",IMPORT_MERGE_ONLY,NULL,
 
102
       N_("only accept updates to existing keys")},
 
103
      {"import-clean",IMPORT_CLEAN,NULL,
 
104
       N_("remove unusable parts from key after import")},
 
105
      {"import-minimal",IMPORT_MINIMAL|IMPORT_CLEAN,NULL,
 
106
       N_("remove as much as possible from key after import")},
 
107
      /* Aliases for backward compatibility */
 
108
      {"allow-local-sigs",IMPORT_LOCAL_SIGS,NULL,NULL},
 
109
      {"repair-hkp-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG,NULL,NULL},
 
110
      /* dummy */
 
111
      {"import-unusable-sigs",0,NULL,NULL},
 
112
      {"import-clean-sigs",0,NULL,NULL},
 
113
      {"import-clean-uids",0,NULL,NULL},
 
114
      {NULL,0,NULL,NULL}
97
115
    };
98
116
 
99
 
  return parse_options(str,options,import_opts);
 
117
  return parse_options(str,options,import_opts,noisy);
100
118
}
101
119
 
102
120
void *
103
121
import_new_stats_handle (void)
104
122
{
105
 
    return xcalloc (1, sizeof (struct stats_s) );
 
123
    return xmalloc_clear ( sizeof (struct stats_s) );
106
124
}
107
125
 
108
126
void
143
161
 *
144
162
 */
145
163
static int
146
 
import_keys_internal( iobuf_t inp, char **fnames, int nnames,
147
 
                      void *stats_handle, unsigned int options )
 
164
import_keys_internal( IOBUF inp, char **fnames, int nnames,
 
165
                      void *stats_handle, unsigned char **fpr, size_t *fpr_len,
 
166
                      unsigned int options )
148
167
{
149
168
    int i, rc = 0;
150
169
    struct stats_s *stats = stats_handle;
153
172
        stats = import_new_stats_handle ();
154
173
 
155
174
    if (inp) {
156
 
        rc = import( inp, "[stream]", stats, options);
 
175
        rc = import( inp, "[stream]", stats, fpr, fpr_len, options);
157
176
    }
158
177
    else {
159
178
        if( !fnames && !nnames )
161
180
 
162
181
        for(i=0; i < nnames; i++ ) {
163
182
            const char *fname = fnames? fnames[i] : NULL;
164
 
            iobuf_t inp2 = iobuf_open(fname);
 
183
            IOBUF inp2 = iobuf_open(fname);
165
184
            if( !fname )
166
185
                fname = "[stdin]";
 
186
            if (inp2 && is_secured_file (iobuf_get_fd (inp2)))
 
187
              {
 
188
                iobuf_close (inp2);
 
189
                inp2 = NULL;
 
190
                errno = EPERM;
 
191
              }
167
192
            if( !inp2 )
168
193
                log_error(_("can't open `%s': %s\n"), fname, strerror(errno) );
169
 
            else {
170
 
                rc = import( inp2, fname, stats, options );
 
194
            else
 
195
              {
 
196
                rc = import( inp2, fname, stats, fpr, fpr_len, options );
171
197
                iobuf_close(inp2);
172
198
                /* Must invalidate that ugly cache to actually close it. */
173
199
                iobuf_ioctl (NULL, 2, 0, (char*)fname);
174
200
                if( rc )
175
 
                    log_error("import from `%s' failed: %s\n", fname,
176
 
                              gpg_strerror (rc) );
177
 
            }
 
201
                  log_error("import from `%s' failed: %s\n", fname,
 
202
                            g10_errstr(rc) );
 
203
              }
178
204
            if( !fname )
179
205
                break;
180
206
        }
183
209
        import_print_stats (stats);
184
210
        import_release_stats_handle (stats);
185
211
    }
 
212
 
186
213
    /* If no fast import and the trustdb is dirty (i.e. we added a key
187
214
       or userID that had something other than a selfsig, a signature
188
215
       that was other than a selfsig, or any revocation), then
189
216
       update/check the trustdb if the user specified by setting
190
217
       interactive or by not setting no-auto-check-trustdb */
191
 
    if (!(options&IMPORT_FAST_IMPORT) && trustdb_pending_check())
192
 
      {
193
 
        if (opt.interactive)
194
 
          update_trustdb();
195
 
        else if (!opt.no_auto_check_trustdb)
196
 
          check_trustdb();
197
 
      }
 
218
 
 
219
    if(!(options&IMPORT_FAST))
 
220
      trustdb_check_or_update();
198
221
 
199
222
    return rc;
200
223
}
203
226
import_keys( char **fnames, int nnames,
204
227
             void *stats_handle, unsigned int options )
205
228
{
206
 
    import_keys_internal( NULL, fnames, nnames, stats_handle, options);
 
229
  import_keys_internal(NULL,fnames,nnames,stats_handle,NULL,NULL,options);
207
230
}
208
231
 
209
232
int
210
 
import_keys_stream( iobuf_t inp, void *stats_handle, unsigned int options )
 
233
import_keys_stream( IOBUF inp, void *stats_handle,
 
234
                    unsigned char **fpr, size_t *fpr_len,unsigned int options )
211
235
{
212
 
    return import_keys_internal( inp, NULL, 0, stats_handle, options);
 
236
  return import_keys_internal(inp,NULL,0,stats_handle,fpr,fpr_len,options);
213
237
}
214
238
 
215
239
static int
216
 
import( iobuf_t inp, const char* fname,
217
 
        struct stats_s *stats, unsigned int options )
 
240
import( IOBUF inp, const char* fname,struct stats_s *stats,
 
241
        unsigned char **fpr,size_t *fpr_len,unsigned int options )
218
242
{
219
243
    PACKET *pending_pkt = NULL;
220
 
    KBNODE keyblock;
 
244
    KBNODE keyblock = NULL;  /* Need to initialize because gcc can't
 
245
                                grasp the return semantics of
 
246
                                read_block. */
221
247
    int rc = 0;
222
248
 
223
249
    getkey_disable_caches();
224
250
 
225
251
    if( !opt.no_armor ) { /* armored reading is not disabled */
226
 
        armor_filter_context_t *afx = xcalloc (1, sizeof *afx );
 
252
        armor_filter_context_t *afx;
 
253
 
 
254
        afx = new_armor_context ();
227
255
        afx->only_keyblocks = 1;
228
 
        iobuf_push_filter2( inp, armor_filter, afx, 1 );
 
256
        push_armor_filter (afx, inp);
 
257
        release_armor_context (afx);
229
258
    }
230
259
 
231
260
    while( !(rc = read_block( inp, &pending_pkt, &keyblock) )) {
232
261
        if( keyblock->pkt->pkttype == PKT_PUBLIC_KEY )
233
 
            rc = import_one( fname, keyblock, stats, options );
 
262
            rc = import_one( fname, keyblock, stats, fpr, fpr_len, options, 0);
234
263
        else if( keyblock->pkt->pkttype == PKT_SECRET_KEY ) 
235
264
                rc = import_secret_one( fname, keyblock, stats, options );
236
265
        else if( keyblock->pkt->pkttype == PKT_SIGNATURE
250
279
    }
251
280
    if( rc == -1 )
252
281
        rc = 0;
253
 
    else if( rc && rc != GPG_ERR_INV_KEYRING )
254
 
        log_error( _("error reading `%s': %s\n"), fname, gpg_strerror (rc));
 
282
    else if( rc && rc != G10ERR_INV_KEYRING )
 
283
        log_error( _("error reading `%s': %s\n"), fname, g10_errstr(rc));
255
284
 
256
285
    return rc;
257
286
}
271
300
            log_info(_("          w/o user IDs: %lu\n"), stats->no_user_id );
272
301
        if( stats->imported || stats->imported_rsa ) {
273
302
            log_info(_("              imported: %lu"), stats->imported );
274
 
            if( stats->imported_rsa )
275
 
                fprintf(stderr, "  (RSA: %lu)", stats->imported_rsa );
276
 
            putc('\n', stderr);
 
303
            if (stats->imported_rsa)
 
304
              log_printf ("  (RSA: %lu)", stats->imported_rsa );
 
305
            log_printf ("\n");
277
306
        }
278
307
        if( stats->unchanged )
279
308
            log_info(_("             unchanged: %lu\n"), stats->unchanged );
293
322
            log_info(_(" secret keys unchanged: %lu\n"), stats->secret_dups );
294
323
        if( stats->not_imported )
295
324
            log_info(_("          not imported: %lu\n"), stats->not_imported );
 
325
        if( stats->n_sigs_cleaned)
 
326
            log_info(_("    signatures cleaned: %lu\n"),stats->n_sigs_cleaned);
 
327
        if( stats->n_uids_cleaned)
 
328
            log_info(_("      user IDs cleaned: %lu\n"),stats->n_uids_cleaned);
296
329
    }
297
330
 
298
331
    if( is_status_enabled() ) {
324
357
 * Retunr: 0 = okay, -1 no more blocks or another errorcode.
325
358
 */
326
359
static int
327
 
read_block( iobuf_t a, PACKET **pending_pkt, KBNODE *ret_root )
 
360
read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root )
328
361
{
329
362
    int rc;
330
363
    PACKET *pkt;
338
371
    }
339
372
    else
340
373
        in_cert = 0;
341
 
    pkt = xmalloc ( sizeof *pkt );
 
374
    pkt = xmalloc( sizeof *pkt );
342
375
    init_packet(pkt);
343
376
    while( (rc=parse_packet(a, pkt)) != -1 ) {
344
377
        if( rc ) {  /* ignore errors */
345
 
            if( rc != GPG_ERR_UNKNOWN_PACKET ) {
346
 
                log_error("read_block: read error: %s\n", gpg_strerror (rc) );
347
 
                rc = GPG_ERR_INV_KEYRING;
 
378
            if( rc != G10ERR_UNKNOWN_PACKET ) {
 
379
                log_error("read_block: read error: %s\n", g10_errstr(rc) );
 
380
                rc = G10ERR_INV_KEYRING;
348
381
                goto ready;
349
382
            }
350
383
            free_packet( pkt );
364
397
        /* make a linked list of all packets */
365
398
        switch( pkt->pkttype ) {
366
399
          case PKT_COMPRESSED:
367
 
            if( pkt->pkt.compressed->algorithm < 1
368
 
                || pkt->pkt.compressed->algorithm > 2 ) {
369
 
                rc = GPG_ERR_COMPR_ALGO;
 
400
            if(check_compress_algo(pkt->pkt.compressed->algorithm))
 
401
              {
 
402
                rc = G10ERR_COMPR_ALGO;
370
403
                goto ready;
371
 
            }
372
 
            {
373
 
                compress_filter_context_t *cfx = xcalloc (1, sizeof *cfx );
374
 
                cfx->algo = pkt->pkt.compressed->algorithm;
 
404
              }
 
405
            else
 
406
              {
 
407
                compress_filter_context_t *cfx = xmalloc_clear( sizeof *cfx );
375
408
                pkt->pkt.compressed->buf = NULL;
376
 
                iobuf_push_filter2( a, compress_filter, cfx, 1 );
377
 
            }
 
409
                push_compress_filter2(a,cfx,pkt->pkt.compressed->algorithm,1);
 
410
              }
378
411
            free_packet( pkt );
379
412
            init_packet(pkt);
380
413
            break;
399
432
                    root = new_kbnode( pkt );
400
433
                else
401
434
                    add_kbnode( root, new_kbnode( pkt ) );
402
 
                pkt = xmalloc ( sizeof *pkt );
 
435
                pkt = xmalloc( sizeof *pkt );
403
436
            }
404
437
            init_packet(pkt);
405
438
            break;
414
447
    else
415
448
        *ret_root = root;
416
449
    free_packet( pkt );
417
 
    xfree ( pkt );
 
450
    xfree( pkt );
418
451
    return rc;
419
452
}
420
453
 
508
541
  write_status_text (STATUS_IMPORT_OK, buf);
509
542
}
510
543
 
511
 
void
 
544
static void
512
545
print_import_check (PKT_public_key * pk, PKT_user_id * id)
513
546
{
514
547
    char * buf;
530
563
    xfree (buf);
531
564
}
532
565
 
 
566
static void
 
567
check_prefs_warning(PKT_public_key *pk)
 
568
{
 
569
  log_info(_("WARNING: key %s contains preferences for unavailable\n"
 
570
             "algorithms on these user IDs:\n"), keystr_from_pk(pk));
 
571
}
 
572
 
 
573
static void
 
574
check_prefs(KBNODE keyblock)
 
575
{
 
576
  KBNODE node;
 
577
  PKT_public_key *pk;
 
578
  int problem=0;
 
579
  
 
580
  merge_keys_and_selfsig(keyblock);
 
581
  pk=keyblock->pkt->pkt.public_key;
 
582
 
 
583
  for(node=keyblock;node;node=node->next)
 
584
    {
 
585
      if(node->pkt->pkttype==PKT_USER_ID
 
586
         && node->pkt->pkt.user_id->created
 
587
         && node->pkt->pkt.user_id->prefs)
 
588
        {
 
589
          PKT_user_id *uid=node->pkt->pkt.user_id;
 
590
          prefitem_t *prefs=uid->prefs;
 
591
          char *user=utf8_to_native(uid->name,strlen(uid->name),0);
 
592
 
 
593
          for(;prefs->type;prefs++)
 
594
            {
 
595
              char num[10]; /* prefs->value is a byte, so we're over
 
596
                               safe here */
 
597
 
 
598
              sprintf(num,"%u",prefs->value);
 
599
 
 
600
              if(prefs->type==PREFTYPE_SYM)
 
601
                {
 
602
                  if (openpgp_cipher_test_algo (prefs->value))
 
603
                    {
 
604
                      const char *algo = 
 
605
                        (openpgp_cipher_test_algo (prefs->value)
 
606
                         ? num 
 
607
                         : openpgp_cipher_algo_name (prefs->value));
 
608
                      if(!problem)
 
609
                        check_prefs_warning(pk);
 
610
                      log_info(_("         \"%s\": preference for cipher"
 
611
                                 " algorithm %s\n"), user, algo);
 
612
                      problem=1;
 
613
                    }
 
614
                }
 
615
              else if(prefs->type==PREFTYPE_HASH)
 
616
                {
 
617
                  if(openpgp_md_test_algo(prefs->value))
 
618
                    {
 
619
                      const char *algo =
 
620
                        (gcry_md_test_algo (prefs->value)
 
621
                         ? num 
 
622
                         : gcry_md_algo_name (prefs->value));
 
623
                      if(!problem)
 
624
                        check_prefs_warning(pk);
 
625
                      log_info(_("         \"%s\": preference for digest"
 
626
                                 " algorithm %s\n"), user, algo);
 
627
                      problem=1;
 
628
                    }
 
629
                }
 
630
              else if(prefs->type==PREFTYPE_ZIP)
 
631
                {
 
632
                  if(check_compress_algo (prefs->value))
 
633
                    {
 
634
                      const char *algo=compress_algo_to_string(prefs->value);
 
635
                      if(!problem)
 
636
                        check_prefs_warning(pk);
 
637
                      log_info(_("         \"%s\": preference for compression"
 
638
                                 " algorithm %s\n"),user,algo?algo:num);
 
639
                      problem=1;
 
640
                    }
 
641
                }
 
642
            }
 
643
 
 
644
          xfree(user);
 
645
        }
 
646
    }
 
647
 
 
648
  if(problem)
 
649
    {
 
650
      log_info(_("it is strongly suggested that you update"
 
651
                 " your preferences and\n"));
 
652
      log_info(_("re-distribute this key to avoid potential algorithm"
 
653
                 " mismatch problems\n"));
 
654
 
 
655
      if(!opt.batch)
 
656
        {
 
657
          strlist_t sl=NULL,locusr=NULL;
 
658
          size_t fprlen=0;
 
659
          byte fpr[MAX_FINGERPRINT_LEN],*p;
 
660
          char username[(MAX_FINGERPRINT_LEN*2)+1];
 
661
          unsigned int i;
 
662
 
 
663
          p=fingerprint_from_pk(pk,fpr,&fprlen);
 
664
          for(i=0;i<fprlen;i++,p++)
 
665
            sprintf(username+2*i,"%02X",*p);
 
666
          add_to_strlist(&locusr,username);
 
667
 
 
668
          append_to_strlist(&sl,"updpref");
 
669
          append_to_strlist(&sl,"save");
 
670
 
 
671
          keyedit_menu( username, locusr, sl, 1, 1 );
 
672
          free_strlist(sl);
 
673
          free_strlist(locusr);
 
674
        }
 
675
      else if(!opt.quiet)
 
676
        log_info(_("you can update your preferences with:"
 
677
                   " gpg --edit-key %s updpref save\n"),keystr_from_pk(pk));
 
678
    }
 
679
}
 
680
 
533
681
/****************
534
682
 * Try to import one keyblock.  Return an error only in serious cases, but
535
683
 * never for an invalid keyblock.  It uses log_error to increase the
537
685
 * which called g10.
538
686
 */
539
687
static int
540
 
import_one( const char *fname, KBNODE keyblock,
541
 
            struct stats_s *stats, unsigned int options )
 
688
import_one( const char *fname, KBNODE keyblock, struct stats_s *stats,
 
689
            unsigned char **fpr,size_t *fpr_len,unsigned int options,
 
690
            int from_sk )
542
691
{
543
692
    PKT_public_key *pk;
544
693
    PKT_public_key *pk_orig;
556
705
        BUG();
557
706
 
558
707
    pk = node->pkt->pkt.public_key;
 
708
 
559
709
    keyid_from_pk( pk, keyid );
560
710
    uidnode = find_next_kbnode( keyblock, PKT_USER_ID );
561
711
 
562
 
    if(pk->pubkey_algo==PUBKEY_ALGO_ELGAMAL)
563
 
      log_info(_("NOTE: Elgamal primary key detected - "
564
 
                 "this may take some time to import\n"));
565
 
 
566
 
    if( opt.verbose && !opt.interactive ) {
567
 
        log_info( "pub  %4u%c/%08lX %s   ",
 
712
    if( opt.verbose && !opt.interactive )
 
713
      {
 
714
        log_info( "pub  %4u%c/%s %s  ",
568
715
                  nbits_from_pk( pk ),
569
716
                  pubkey_letter( pk->pubkey_algo ),
570
 
                  (ulong)keyid[1], datestr_from_pk(pk) );
 
717
                  keystr_from_pk(pk), datestr_from_pk(pk) );
571
718
        if( uidnode )
572
 
            print_utf8_string( stderr, uidnode->pkt->pkt.user_id->name,
573
 
                                       uidnode->pkt->pkt.user_id->len );
574
 
        putc('\n', stderr);
575
 
    }
576
 
    if( !uidnode ) {
577
 
        log_error( _("key %08lX: no user ID\n"), (ulong)keyid[1]);
 
719
          print_utf8_string( stderr, uidnode->pkt->pkt.user_id->name,
 
720
                             uidnode->pkt->pkt.user_id->len );
 
721
        log_printf ("\n");
 
722
      }
 
723
 
 
724
    if( !uidnode )
 
725
      {
 
726
        log_error( _("key %s: no user ID\n"), keystr_from_pk(pk));
578
727
        return 0;
579
 
    }
 
728
      }
580
729
    
581
730
    if (opt.interactive) {
582
731
        if(is_status_enabled())
590
739
            return 0;
591
740
    }
592
741
 
 
742
    collapse_uids(&keyblock);
 
743
 
 
744
    /* Clean the key that we're about to import, to cut down on things
 
745
       that we have to clean later.  This has no practical impact on
 
746
       the end result, but does result in less logging which might
 
747
       confuse the user. */
 
748
    if(options&IMPORT_CLEAN)
 
749
      clean_key(keyblock,opt.verbose,options&IMPORT_MINIMAL,NULL,NULL);
 
750
 
593
751
    clear_kbnode_flags( keyblock );
594
752
 
595
753
    if((options&IMPORT_REPAIR_PKS_SUBKEY_BUG) && fix_pks_corruption(keyblock)
596
754
       && opt.verbose)
597
 
      log_info(_("key %08lX: PKS subkey corruption repaired\n"),
598
 
               (ulong)keyid[1]);
 
755
      log_info(_("key %s: PKS subkey corruption repaired\n"),
 
756
               keystr_from_pk(pk));
599
757
 
600
758
    rc = chk_self_sigs( fname, keyblock , pk, keyid, &non_self );
601
759
    if( rc )
609
767
            char *user=utf8_to_native(node->pkt->pkt.user_id->name,
610
768
                                      node->pkt->pkt.user_id->len,0);
611
769
            node->flag |= 1;
612
 
            log_info( _("key %08lX: accepted non self-signed user ID '%s'\n"),
613
 
                      (ulong)keyid[1],user);
614
 
            xfree (user);
 
770
            log_info( _("key %s: accepted non self-signed user ID \"%s\"\n"),
 
771
                      keystr_from_pk(pk),user);
 
772
            xfree(user);
615
773
          }
616
774
 
617
775
    if( !delete_inv_parts( fname, keyblock, keyid, options ) ) {
618
 
        log_error ( _("key %08lX: no valid user IDs\n"), (ulong)keyid[1]);
 
776
        log_error( _("key %s: no valid user IDs\n"), keystr_from_pk(pk));
619
777
        if( !opt.quiet )
620
 
            log_info(_("this may be caused by a missing self-signature\n"));
 
778
          log_info(_("this may be caused by a missing self-signature\n"));
621
779
        stats->no_user_id++;
622
780
        return 0;
623
781
    }
624
782
 
625
783
    /* do we have this key already in one of our pubrings ? */
626
 
    pk_orig = xcalloc (1, sizeof *pk_orig );
 
784
    pk_orig = xmalloc_clear( sizeof *pk_orig );
627
785
    rc = get_pubkey_fast ( pk_orig, keyid );
628
 
    if( rc && gpg_err_code (rc) != GPG_ERR_NO_PUBKEY
629
 
        && gpg_err_code (rc) != GPG_ERR_UNUSABLE_PUBKEY ) {
630
 
        log_error( _("key %08lX: public key not found: %s\n"),
631
 
                                (ulong)keyid[1], gpg_strerror (rc));
632
 
    }
633
 
    else if ( rc && opt.merge_only ) {
 
786
    if( rc && rc != G10ERR_NO_PUBKEY && rc != G10ERR_UNU_PUBKEY )
 
787
      {
 
788
        log_error( _("key %s: public key not found: %s\n"),
 
789
                   keystr(keyid), g10_errstr(rc));
 
790
      }
 
791
    else if ( rc && (opt.import_options&IMPORT_MERGE_ONLY) )
 
792
      {
634
793
        if( opt.verbose )
635
 
            log_info( _("key %08lX: new key - skipped\n"), (ulong)keyid[1] );
 
794
          log_info( _("key %s: new key - skipped\n"), keystr(keyid));
636
795
        rc = 0;
637
796
        stats->skipped_new_keys++;
638
 
    }
 
797
      }
639
798
    else if( rc ) { /* insert this key */
640
799
        KEYDB_HANDLE hd = keydb_new (0);
641
800
 
642
801
        rc = keydb_locate_writable (hd, NULL);
643
802
        if (rc) {
644
 
            log_error (_("no writable keyring found: %s\n"), gpg_strerror (rc));
 
803
            log_error (_("no writable keyring found: %s\n"), g10_errstr (rc));
645
804
            keydb_release (hd);
646
 
            return GPG_ERR_GENERAL;
 
805
            return G10ERR_GENERAL;
647
806
        }
648
807
        if( opt.verbose > 1 )
649
808
            log_info (_("writing to `%s'\n"), keydb_get_resource_name (hd) );
 
809
 
650
810
        rc = keydb_insert_keyblock (hd, keyblock );
651
811
        if (rc)
652
812
           log_error (_("error writing keyring `%s': %s\n"),
653
 
                       keydb_get_resource_name (hd), gpg_strerror (rc));
 
813
                       keydb_get_resource_name (hd), g10_errstr(rc));
654
814
        else
655
815
          {
656
816
            /* This should not be possible since we delete the
665
825
        keydb_release (hd);
666
826
 
667
827
        /* we are ready */
668
 
        if( !opt.quiet ) {
669
 
            char *p=get_user_id_printable (keyid);
670
 
            log_info( _("key %08lX: public key \"%s\" imported\n"),
671
 
                      (ulong)keyid[1],p);
672
 
            xfree (p);
673
 
        }
674
 
        if( is_status_enabled() ) {
 
828
        if( !opt.quiet )
 
829
          {
 
830
            char *p=get_user_id_native (keyid);
 
831
            log_info( _("key %s: public key \"%s\" imported\n"),
 
832
                      keystr(keyid),p);
 
833
            xfree(p);
 
834
          }
 
835
        if( is_status_enabled() )
 
836
          {
675
837
            char *us = get_long_user_id_string( keyid );
676
838
            write_status_text( STATUS_IMPORTED, us );
677
 
            xfree (us);
 
839
            xfree(us);
678
840
            print_import_ok (pk,NULL, 1);
679
 
        }
 
841
          }
680
842
        stats->imported++;
681
843
        if( is_RSA( pk->pubkey_algo ) )
682
844
            stats->imported_rsa++;
684
846
    }
685
847
    else { /* merge */
686
848
        KEYDB_HANDLE hd;
687
 
        int n_uids, n_sigs, n_subk;
 
849
        int n_uids, n_sigs, n_subk, n_sigs_cleaned, n_uids_cleaned;
688
850
 
689
851
        /* Compare the original against the new key; just to be sure nothing
690
852
         * weird is going on */
691
 
        if( cmp_public_keys( pk_orig, pk ) ) {
692
 
            log_error( _("key %08lX: doesn't match our copy\n"),
693
 
                                                          (ulong)keyid[1]);
 
853
        if( cmp_public_keys( pk_orig, pk ) )
 
854
          {
 
855
            log_error( _("key %s: doesn't match our copy\n"),keystr(keyid));
694
856
            goto leave;
695
 
        }
 
857
          }
696
858
 
697
859
        /* now read the original keyblock */
698
860
        hd = keydb_new (0);
705
867
                afp[an++] = 0;
706
868
            rc = keydb_search_fpr (hd, afp);
707
869
        }
708
 
        if( rc ) {
709
 
            log_error (_("key %08lX: can't locate original keyblock: %s\n"),
710
 
                                     (ulong)keyid[1], gpg_strerror (rc));
 
870
        if( rc )
 
871
          {
 
872
            log_error (_("key %s: can't locate original keyblock: %s\n"),
 
873
                       keystr(keyid), g10_errstr(rc));
711
874
            keydb_release (hd);
712
875
            goto leave;
713
 
        }
 
876
          }
714
877
        rc = keydb_get_keyblock (hd, &keyblock_orig );
715
 
        if (rc) {
716
 
            log_error (_("key %08lX: can't read original keyblock: %s\n"),
717
 
                                            (ulong)keyid[1], gpg_strerror (rc));
 
878
        if (rc)
 
879
          {
 
880
            log_error (_("key %s: can't read original keyblock: %s\n"),
 
881
                       keystr(keyid), g10_errstr(rc));
718
882
            keydb_release (hd);
719
883
            goto leave;
720
 
        }
 
884
          }
721
885
 
722
 
        collapse_uids( &keyblock );
723
886
        /* and try to merge the block */
724
887
        clear_kbnode_flags( keyblock_orig );
725
888
        clear_kbnode_flags( keyblock );
726
 
        n_uids = n_sigs = n_subk = 0;
 
889
        n_uids = n_sigs = n_subk = n_sigs_cleaned = n_uids_cleaned = 0;
727
890
        rc = merge_blocks( fname, keyblock_orig, keyblock,
728
 
                                keyid, &n_uids, &n_sigs, &n_subk );
729
 
        if( rc ) {
 
891
                           keyid, &n_uids, &n_sigs, &n_subk );
 
892
        if( rc )
 
893
          {
730
894
            keydb_release (hd);
731
895
            goto leave;
732
 
        }
733
 
        if( n_uids || n_sigs || n_subk ) {
 
896
          }
 
897
 
 
898
        if(options&IMPORT_CLEAN)
 
899
          clean_key(keyblock_orig,opt.verbose,options&IMPORT_MINIMAL,
 
900
                    &n_uids_cleaned,&n_sigs_cleaned);
 
901
 
 
902
        if( n_uids || n_sigs || n_subk || n_sigs_cleaned || n_uids_cleaned) {
734
903
            mod_key = 1;
735
904
            /* keyblock_orig has been updated; write */
736
905
            rc = keydb_update_keyblock (hd, keyblock_orig);
737
906
            if (rc)
738
907
                log_error (_("error writing keyring `%s': %s\n"),
739
 
                             keydb_get_resource_name (hd), gpg_strerror (rc) );
 
908
                             keydb_get_resource_name (hd), g10_errstr(rc) );
740
909
            else if(non_self)
741
910
              revalidation_mark ();
742
911
 
743
912
            /* we are ready */
744
 
            if( !opt.quiet ) {
745
 
                char *p=get_user_id_printable(keyid);
 
913
            if( !opt.quiet )
 
914
              {
 
915
                char *p=get_user_id_native(keyid);
746
916
                if( n_uids == 1 )
747
 
                    log_info( _("key %08lX: \"%s\" 1 new user ID\n"),
748
 
                                             (ulong)keyid[1], p);
 
917
                  log_info( _("key %s: \"%s\" 1 new user ID\n"),
 
918
                           keystr(keyid),p);
749
919
                else if( n_uids )
750
 
                    log_info( _("key %08lX: \"%s\" %d new user IDs\n"),
751
 
                                             (ulong)keyid[1], p, n_uids );
 
920
                  log_info( _("key %s: \"%s\" %d new user IDs\n"),
 
921
                            keystr(keyid),p,n_uids);
752
922
                if( n_sigs == 1 )
753
 
                    log_info( _("key %08lX: \"%s\" 1 new signature\n"),
754
 
                                             (ulong)keyid[1], p);
 
923
                  log_info( _("key %s: \"%s\" 1 new signature\n"),
 
924
                            keystr(keyid), p);
755
925
                else if( n_sigs )
756
 
                    log_info( _("key %08lX: \"%s\" %d new signatures\n"),
757
 
                                             (ulong)keyid[1], p, n_sigs );
 
926
                  log_info( _("key %s: \"%s\" %d new signatures\n"),
 
927
                            keystr(keyid), p, n_sigs );
758
928
                if( n_subk == 1 )
759
 
                    log_info( _("key %08lX: \"%s\" 1 new subkey\n"),
760
 
                                             (ulong)keyid[1], p);
 
929
                  log_info( _("key %s: \"%s\" 1 new subkey\n"),
 
930
                            keystr(keyid), p);
761
931
                else if( n_subk )
762
 
                    log_info( _("key %08lX: \"%s\" %d new subkeys\n"),
763
 
                                             (ulong)keyid[1], p, n_subk );
764
 
                xfree (p);
765
 
            }
 
932
                  log_info( _("key %s: \"%s\" %d new subkeys\n"),
 
933
                            keystr(keyid), p, n_subk );
 
934
                if(n_sigs_cleaned==1)
 
935
                  log_info(_("key %s: \"%s\" %d signature cleaned\n"),
 
936
                           keystr(keyid),p,n_sigs_cleaned);
 
937
                else if(n_sigs_cleaned)
 
938
                  log_info(_("key %s: \"%s\" %d signatures cleaned\n"),
 
939
                           keystr(keyid),p,n_sigs_cleaned);
 
940
                if(n_uids_cleaned==1)
 
941
                  log_info(_("key %s: \"%s\" %d user ID cleaned\n"),
 
942
                           keystr(keyid),p,n_uids_cleaned);
 
943
                else if(n_uids_cleaned)
 
944
                  log_info(_("key %s: \"%s\" %d user IDs cleaned\n"),
 
945
                           keystr(keyid),p,n_uids_cleaned);
 
946
                xfree(p);
 
947
              }
766
948
 
767
949
            stats->n_uids +=n_uids;
768
950
            stats->n_sigs +=n_sigs;
769
951
            stats->n_subk +=n_subk;
 
952
            stats->n_sigs_cleaned +=n_sigs_cleaned;
 
953
            stats->n_uids_cleaned +=n_uids_cleaned;
770
954
 
771
955
            if (is_status_enabled ()) 
772
956
                 print_import_ok (pk, NULL,
773
957
                                  ((n_uids?2:0)|(n_sigs?4:0)|(n_subk?8:0)));
774
958
        }
775
 
        else {
776
 
             if (is_status_enabled ()) 
777
 
                  print_import_ok (pk, NULL, 0);
778
 
        
779
 
            if( !opt.quiet ) {
780
 
                char *p=get_user_id_printable(keyid);
781
 
                log_info( _("key %08lX: \"%s\" not changed\n"),
782
 
                          (ulong)keyid[1],p);
783
 
                xfree (p);
784
 
            }
 
959
        else
 
960
          {
 
961
            if (is_status_enabled ()) 
 
962
              print_import_ok (pk, NULL, 0);
 
963
 
 
964
            if( !opt.quiet )
 
965
              {
 
966
                char *p=get_user_id_native(keyid);
 
967
                log_info( _("key %s: \"%s\" not changed\n"),keystr(keyid),p);
 
968
                xfree(p);
 
969
              }
 
970
 
785
971
            stats->unchanged++;
786
 
        }
 
972
          }
 
973
 
787
974
        keydb_release (hd); hd = NULL;
788
975
    }
789
976
 
790
977
  leave:
 
978
 
 
979
    /* Now that the key is definitely incorporated into the keydb, we
 
980
       need to check if a designated revocation is present or if the
 
981
       prefs are not rational so we can warn the user. */
 
982
 
 
983
    if(mod_key)
 
984
      {
 
985
        revocation_present(keyblock_orig);
 
986
        if(!from_sk && seckey_available(keyid)==0)
 
987
          check_prefs(keyblock_orig);
 
988
      }
 
989
    else if(new_key)
 
990
      {
 
991
        /* A little explanation for this: we fill in the fingerprint
 
992
           when importing keys as it can be useful to know the
 
993
           fingerprint in certain keyserver-related cases (a keyserver
 
994
           asked for a particular name, but the key doesn't have that
 
995
           name).  However, in cases where we're importing more than
 
996
           one key at a time, we cannot know which key to fingerprint.
 
997
           In these cases, rather than guessing, we do not fingerpring
 
998
           at all, and we must hope the user ID on the keys are
 
999
           useful. */
 
1000
        if(fpr)
 
1001
          {
 
1002
            xfree(*fpr);
 
1003
            if(stats->imported==1)
 
1004
              *fpr=fingerprint_from_pk(pk,NULL,fpr_len);
 
1005
            else
 
1006
              *fpr=NULL;
 
1007
          }
 
1008
 
 
1009
        revocation_present(keyblock);
 
1010
        if(!from_sk && seckey_available(keyid)==0)
 
1011
          check_prefs(keyblock);
 
1012
      }
 
1013
 
791
1014
    release_kbnode( keyblock_orig );
792
1015
    free_public_key( pk_orig );
793
1016
 
794
 
    revocation_present(keyblock);
795
 
 
796
1017
    return rc;
797
1018
}
798
1019
 
813
1034
             write the keyblock out. */
814
1035
 
815
1036
          PKT_secret_key *sk=secnode->pkt->pkt.secret_key;
816
 
          PACKET *pkt=xcalloc (1,sizeof(PACKET));
817
 
          PKT_public_key *pk=xcalloc (1,sizeof(PKT_public_key));
 
1037
          PACKET *pkt=xmalloc_clear(sizeof(PACKET));
 
1038
          PKT_public_key *pk=xmalloc_clear(sizeof(PKT_public_key));
818
1039
          int n;
819
1040
 
820
1041
          if(secnode->pkt->pkttype==PKT_SECRET_KEY)
831
1052
 
832
1053
          n=pubkey_get_npkey(pk->pubkey_algo);
833
1054
          if(n==0)
834
 
            pk->pkey[0]=mpi_copy(sk->skey[0]);
 
1055
            {
 
1056
              /* we can't properly extract the pubkey without knowing
 
1057
                 the number of MPIs */
 
1058
              release_kbnode(pub_keyblock);
 
1059
              return NULL;
 
1060
            }
835
1061
          else
836
1062
            {
837
1063
              int i;
880
1106
    keyid_from_sk( sk, keyid );
881
1107
    uidnode = find_next_kbnode( keyblock, PKT_USER_ID );
882
1108
 
883
 
    if( opt.verbose ) {
884
 
        log_info( "sec  %4u%c/%08lX %s   ",
 
1109
    if( opt.verbose )
 
1110
      {
 
1111
        log_info( "sec  %4u%c/%s %s   ",
885
1112
                  nbits_from_sk( sk ),
886
1113
                  pubkey_letter( sk->pubkey_algo ),
887
 
                  (ulong)keyid[1], datestr_from_sk(sk) );
 
1114
                  keystr_from_sk(sk), datestr_from_sk(sk) );
888
1115
        if( uidnode )
889
 
            print_utf8_string( stderr, uidnode->pkt->pkt.user_id->name,
890
 
                                       uidnode->pkt->pkt.user_id->len );
891
 
        putc('\n', stderr);
892
 
    }
 
1116
          print_utf8_string( stderr, uidnode->pkt->pkt.user_id->name,
 
1117
                             uidnode->pkt->pkt.user_id->len );
 
1118
        log_printf ("\n");
 
1119
      }
893
1120
    stats->secret_read++;
894
1121
 
895
 
    if( !uidnode ) {
896
 
        log_error( _("key %08lX: no user ID\n"), (ulong)keyid[1]);
 
1122
    if( !uidnode )
 
1123
      {
 
1124
        log_error( _("key %s: no user ID\n"), keystr_from_sk(sk));
897
1125
        return 0;
898
 
    }
 
1126
      }
899
1127
 
900
1128
    if(sk->protect.algo>110)
901
1129
      {
902
 
        log_error(_("key %08lX: secret key with invalid cipher %d "
903
 
                    "- skipped\n"),(ulong)keyid[1],sk->protect.algo);
 
1130
        log_error(_("key %s: secret key with invalid cipher %d"
 
1131
                    " - skipped\n"),keystr_from_sk(sk),sk->protect.algo);
904
1132
        return 0;
905
1133
      }
906
1134
 
 
1135
#ifdef ENABLE_SELINUX_HACKS
 
1136
    if (1)
 
1137
      {
 
1138
        /* We don't allow to import secret keys because that may be used
 
1139
           to put a secret key into the keyring and the user might later
 
1140
           be tricked into signing stuff with that key.  */
 
1141
        log_error (_("importing secret keys not allowed\n"));
 
1142
        return 0;
 
1143
      }
 
1144
#endif 
 
1145
    
907
1146
    clear_kbnode_flags( keyblock );
908
1147
 
909
1148
    /* do we have this key already in one of our secrings ? */
910
1149
    rc = seckey_available( keyid );
911
 
    if( gpg_err_code (rc) == GPG_ERR_NO_SECKEY && !opt.merge_only ) { 
912
 
        /* simply insert this key */
 
1150
    if( rc == G10ERR_NO_SECKEY && !(opt.import_options&IMPORT_MERGE_ONLY) )
 
1151
      {
 
1152
        /* simply insert this key */
913
1153
        KEYDB_HANDLE hd = keydb_new (1);
914
1154
 
915
1155
        /* get default resource */
916
1156
        rc = keydb_locate_writable (hd, NULL);
917
1157
        if (rc) {
918
 
            log_error (_("no default secret keyring: %s\n"), gpg_strerror (rc));
919
 
            keydb_release (hd);
920
 
            return GPG_ERR_GENERAL;
 
1158
          log_error (_("no default secret keyring: %s\n"), g10_errstr (rc));
 
1159
          keydb_release (hd);
 
1160
          return G10ERR_GENERAL;
921
1161
        }
922
1162
        rc = keydb_insert_keyblock (hd, keyblock );
923
1163
        if (rc)
924
 
            log_error (_("error writing keyring `%s': %s\n"),
925
 
                       keydb_get_resource_name (hd), gpg_strerror (rc) );
 
1164
          log_error (_("error writing keyring `%s': %s\n"),
 
1165
                     keydb_get_resource_name (hd), g10_errstr(rc) );
926
1166
        keydb_release (hd);
927
1167
        /* we are ready */
928
1168
        if( !opt.quiet )
929
 
            log_info( _("key %08lX: secret key imported\n"), (ulong)keyid[1]);
 
1169
          log_info( _("key %s: secret key imported\n"), keystr_from_sk(sk));
930
1170
        stats->secret_imported++;
931
1171
        if (is_status_enabled ()) 
932
 
             print_import_ok (NULL, sk, 1|16);
 
1172
          print_import_ok (NULL, sk, 1|16);
933
1173
 
934
1174
        if(options&IMPORT_SK2PK)
935
1175
          {
936
1176
            /* Try and make a public key out of this. */
937
1177
 
938
1178
            KBNODE pub_keyblock=sec_to_pub_keyblock(keyblock);
939
 
            import_one(fname,pub_keyblock,stats,opt.import_options);
940
 
            release_kbnode(pub_keyblock);
 
1179
            if(pub_keyblock)
 
1180
              {
 
1181
                import_one(fname,pub_keyblock,stats,
 
1182
                           NULL,NULL,opt.import_options,1);
 
1183
                release_kbnode(pub_keyblock);
 
1184
              }
941
1185
          }
942
1186
 
943
 
    }
944
 
    else if( !rc ) { /* we can't merge secret keys */
945
 
        log_error( _("key %08lX: already in secret keyring\n"),
946
 
                                                        (ulong)keyid[1]);
 
1187
        /* Now that the key is definitely incorporated into the keydb,
 
1188
           if we have the public part of this key, we need to check if
 
1189
           the prefs are rational. */
 
1190
        node=get_pubkeyblock(keyid);
 
1191
        if(node)
 
1192
          {
 
1193
            check_prefs(node);
 
1194
            release_kbnode(node);
 
1195
          }
 
1196
      }
 
1197
    else if( !rc )
 
1198
      { /* we can't merge secret keys */
 
1199
        log_error( _("key %s: already in secret keyring\n"),
 
1200
                   keystr_from_sk(sk));
947
1201
        stats->secret_dups++;
948
1202
        if (is_status_enabled ()) 
949
 
             print_import_ok (NULL, sk, 16);
 
1203
          print_import_ok (NULL, sk, 16);
950
1204
 
951
1205
        /* TODO: if we ever do merge secret keys, make sure to handle
952
1206
           the sec_to_pub_keyblock feature as well. */
953
 
    }
 
1207
      }
954
1208
    else
955
 
        log_error( _("key %08lX: secret key not found: %s\n"),
956
 
                                (ulong)keyid[1], gpg_strerror (rc));
 
1209
      log_error( _("key %s: secret key not found: %s\n"),
 
1210
                 keystr_from_sk(sk), g10_errstr(rc));
957
1211
 
958
1212
    return rc;
959
1213
}
978
1232
    keyid[0] = node->pkt->pkt.signature->keyid[0];
979
1233
    keyid[1] = node->pkt->pkt.signature->keyid[1];
980
1234
 
981
 
    pk = xcalloc (1, sizeof *pk );
 
1235
    pk = xmalloc_clear( sizeof *pk );
982
1236
    rc = get_pubkey( pk, keyid );
983
 
    if( gpg_err_code (rc) == GPG_ERR_NO_PUBKEY ) {
984
 
        log_error ( _("key %08lX: no public key - "
985
 
                      "can't apply revocation certificate\n"), (ulong)keyid[1]);
 
1237
    if( rc == G10ERR_NO_PUBKEY )
 
1238
      {
 
1239
        log_error(_("key %s: no public key -"
 
1240
                    " can't apply revocation certificate\n"), keystr(keyid));
986
1241
        rc = 0;
987
1242
        goto leave;
988
 
    }
989
 
    else if( rc ) {
990
 
        log_error( _("key %08lX: public key not found: %s\n"),
991
 
                                       (ulong)keyid[1], gpg_strerror (rc));
 
1243
      }
 
1244
    else if( rc )
 
1245
      {
 
1246
        log_error(_("key %s: public key not found: %s\n"),
 
1247
                  keystr(keyid), g10_errstr(rc));
992
1248
        goto leave;
993
 
    }
 
1249
      }
994
1250
 
995
1251
    /* read the original keyblock */
996
1252
    hd = keydb_new (0);
1003
1259
            afp[an++] = 0;
1004
1260
        rc = keydb_search_fpr (hd, afp);
1005
1261
    }
1006
 
    if (rc) {
1007
 
        log_error (_("key %08lX: can't locate original keyblock: %s\n"),
1008
 
                   (ulong)keyid[1], gpg_strerror (rc));
 
1262
    if (rc)
 
1263
      {
 
1264
        log_error (_("key %s: can't locate original keyblock: %s\n"),
 
1265
                   keystr(keyid), g10_errstr(rc));
1009
1266
        goto leave;
1010
 
    }
 
1267
      }
1011
1268
    rc = keydb_get_keyblock (hd, &keyblock );
1012
 
    if (rc) {
1013
 
        log_error (_("key %08lX: can't read original keyblock: %s\n"),
1014
 
                   (ulong)keyid[1], gpg_strerror (rc));
 
1269
    if (rc)
 
1270
      {
 
1271
        log_error (_("key %s: can't read original keyblock: %s\n"),
 
1272
                   keystr(keyid), g10_errstr(rc));
1015
1273
        goto leave;
1016
 
    }
1017
 
 
 
1274
      }
1018
1275
 
1019
1276
    /* it is okay, that node is not in keyblock because
1020
1277
     * check_key_signature works fine for sig_class 0x20 in this
1021
1278
     * special case. */
1022
1279
    rc = check_key_signature( keyblock, node, NULL);
1023
 
    if( rc ) {
1024
 
        log_error( _("key %08lX: invalid revocation certificate"
1025
 
                  ": %s - rejected\n"), (ulong)keyid[1], gpg_strerror (rc));
 
1280
    if( rc )
 
1281
      {
 
1282
        log_error( _("key %s: invalid revocation certificate"
 
1283
                     ": %s - rejected\n"), keystr(keyid), g10_errstr(rc));
1026
1284
        goto leave;
1027
 
    }
1028
 
 
 
1285
      }
1029
1286
 
1030
1287
    /* check whether we already have this */
1031
1288
    for(onode=keyblock->next; onode; onode=onode->next ) {
1032
1289
        if( onode->pkt->pkttype == PKT_USER_ID )
1033
1290
            break;
1034
1291
        else if( onode->pkt->pkttype == PKT_SIGNATURE
1035
 
                 && !cmp_signatures(node->pkt->pkt.signature,
1036
 
                                    onode->pkt->pkt.signature))
1037
 
          {
1038
 
            rc = 0;
1039
 
            goto leave; /* yes, we already know about it */
1040
 
          }
 
1292
                 && !cmp_signatures(node->pkt->pkt.signature,
 
1293
                                    onode->pkt->pkt.signature))
 
1294
          {
 
1295
            rc = 0;
 
1296
            goto leave; /* yes, we already know about it */
 
1297
          }
1041
1298
    }
1042
1299
 
1043
1300
 
1048
1305
    rc = keydb_update_keyblock (hd, keyblock );
1049
1306
    if (rc)
1050
1307
        log_error (_("error writing keyring `%s': %s\n"),
1051
 
                   keydb_get_resource_name (hd), gpg_strerror (rc) );
 
1308
                   keydb_get_resource_name (hd), g10_errstr(rc) );
1052
1309
    keydb_release (hd); hd = NULL;
1053
1310
    /* we are ready */
1054
 
    if( !opt.quiet ) {
1055
 
        char *p=get_user_id_printable (keyid);
1056
 
        log_info( _("key %08lX: \"%s\" revocation certificate imported\n"),
1057
 
                                        (ulong)keyid[1],p);
1058
 
        xfree (p);
1059
 
    }
 
1311
    if( !opt.quiet )
 
1312
      {
 
1313
        char *p=get_user_id_native (keyid);
 
1314
        log_info( _("key %s: \"%s\" revocation certificate imported\n"),
 
1315
                  keystr(keyid),p);
 
1316
        xfree(p);
 
1317
      }
1060
1318
    stats->n_revoc++;
1061
1319
 
1062
1320
    /* If the key we just revoked was ultimately trusted, remove its
1109
1367
        sig = n->pkt->pkt.signature;
1110
1368
        if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] ) {
1111
1369
 
1112
 
          /* This just caches the sigs for later use.  That way we
1113
 
             import a fully-cached key which speeds things up. */
1114
 
          if(!opt.no_sig_cache)
1115
 
            check_key_signature(keyblock,n,NULL);
 
1370
            /* This just caches the sigs for later use.  That way we
 
1371
               import a fully-cached key which speeds things up. */
 
1372
            if(!opt.no_sig_cache)
 
1373
              check_key_signature(keyblock,n,NULL);
1116
1374
 
1117
 
            if( (sig->sig_class&~3) == 0x10 ) {
 
1375
            if( IS_UID_SIG(sig) || IS_UID_REV(sig) )
 
1376
              {
1118
1377
                KBNODE unode = find_prev_kbnode( keyblock, n, PKT_USER_ID );
1119
 
                if( !unode )  {
1120
 
                    log_error( _("key %08lX: no user ID for signature\n"),
1121
 
                                            (ulong)keyid[1]);
 
1378
                if( !unode )
 
1379
                  {
 
1380
                    log_error( _("key %s: no user ID for signature\n"),
 
1381
                               keystr(keyid));
1122
1382
                    return -1;  /* the complete keyblock is invalid */
1123
 
                }
 
1383
                  }
1124
1384
 
1125
1385
                /* If it hasn't been marked valid yet, keep trying */
1126
1386
                if(!(unode->flag&1)) {
1127
1387
                  rc = check_key_signature( keyblock, n, NULL);
1128
1388
                  if( rc )
1129
1389
                    {
1130
 
                      if (opt.verbose)
1131
 
                        {
1132
 
                          char *p=utf8_to_native(unode->pkt->pkt.user_id->name,
1133
 
                                       strlen(unode->pkt->pkt.user_id->name),0);
1134
 
                          log_info( gpg_err_code (rc) == GPG_ERR_PUBKEY_ALGO ?
1135
 
                                    _("key %08lX: unsupported public key "
1136
 
                                      "algorithm on user id \"%s\"\n"):
1137
 
                                    _("key %08lX: invalid self-signature "
1138
 
                                      "on user id \"%s\"\n"),
1139
 
                                    (ulong)keyid[1],p);
1140
 
                          xfree (p);
1141
 
                        }
1142
 
                    }
1143
 
                  else
 
1390
                      if( opt.verbose )
 
1391
                        {
 
1392
                          char *p=utf8_to_native(unode->pkt->pkt.user_id->name,
 
1393
                                      strlen(unode->pkt->pkt.user_id->name),0);
 
1394
                          log_info( rc == G10ERR_PUBKEY_ALGO ?
 
1395
                                    _("key %s: unsupported public key "
 
1396
                                      "algorithm on user ID \"%s\"\n"):
 
1397
                                    _("key %s: invalid self-signature "
 
1398
                                      "on user ID \"%s\"\n"),
 
1399
                                    keystr(keyid),p);
 
1400
                          xfree(p);
 
1401
                        }
 
1402
                    }
 
1403
                  else
1144
1404
                    unode->flag |= 1; /* mark that signature checked */
1145
1405
                }
1146
 
            }
 
1406
              }
1147
1407
            else if( sig->sig_class == 0x18 ) {
1148
1408
              /* Note that this works based solely on the timestamps
1149
1409
                 like the rest of gpg.  If the standard gets
1150
1410
                 revocation targets, this may need to be revised. */
1151
1411
 
1152
1412
                if( !knode )
1153
 
                  {
1154
 
                    if (opt.verbose)
1155
 
                      log_info( _("key %08lX: no subkey for subkey "
1156
 
                                  "binding signature\n"),(ulong)keyid[1]);
1157
 
                    n->flag |= 4; /* delete this */
1158
 
                  }
 
1413
                  {
 
1414
                    if(opt.verbose)
 
1415
                      log_info( _("key %s: no subkey for key binding\n"),
 
1416
                                keystr(keyid));
 
1417
                    n->flag |= 4; /* delete this */
 
1418
                  }
1159
1419
                else
1160
 
                  {
1161
 
                    rc = check_key_signature( keyblock, n, NULL);
1162
 
                    if( rc )
1163
 
                      {
1164
 
                        if (opt.verbose)
1165
 
                          log_info(  gpg_err_code (rc) == GPG_ERR_PUBKEY_ALGO ?
1166
 
                            _("key %08lX: unsupported public key algorithm\n"):
1167
 
                            _("key %08lX: invalid subkey binding\n"),
1168
 
                                     (ulong)keyid[1]);
1169
 
                        n->flag|=4;
1170
 
                      }
1171
 
                    else
1172
 
                      {
1173
 
                        /* It's valid, so is it newer? */
1174
 
                        if(sig->timestamp>=bsdate) 
1175
 
                          {
1176
 
                            knode->flag |= 1;  /* the subkey is valid */
1177
 
                            if(bsnode)
1178
 
                              {
1179
 
                                bsnode->flag|=4; /* Delete the last binding
1180
 
                                                    sig since this one is
1181
 
                                                    newer */
1182
 
                                if (opt.verbose)
1183
 
                                  log_info(_("key %08lX: removed multiple "
1184
 
                                             "subkey binding\n"),
1185
 
                                           (ulong)keyid[1]);
1186
 
                              }
1187
 
                            
1188
 
                            bsnode=n;
1189
 
                            bsdate=sig->timestamp;
1190
 
                          }
1191
 
                        else
1192
 
                          n->flag|=4; /* older */
1193
 
                      }
1194
 
                  }
 
1420
                  {
 
1421
                    rc = check_key_signature( keyblock, n, NULL);
 
1422
                    if( rc )
 
1423
                      {
 
1424
                        if(opt.verbose)
 
1425
                          log_info(rc == G10ERR_PUBKEY_ALGO ?
 
1426
                                   _("key %s: unsupported public key"
 
1427
                                     " algorithm\n"):
 
1428
                                   _("key %s: invalid subkey binding\n"),
 
1429
                                   keystr(keyid));
 
1430
                        n->flag|=4;
 
1431
                      }
 
1432
                    else
 
1433
                      {
 
1434
                        /* It's valid, so is it newer? */
 
1435
                        if(sig->timestamp>=bsdate) {
 
1436
                          knode->flag |= 1;  /* the subkey is valid */
 
1437
                          if(bsnode)
 
1438
                            {
 
1439
                              bsnode->flag|=4; /* Delete the last binding
 
1440
                                                  sig since this one is
 
1441
                                                  newer */
 
1442
                              if(opt.verbose)
 
1443
                                log_info(_("key %s: removed multiple subkey"
 
1444
                                           " binding\n"),keystr(keyid));
 
1445
                            }
 
1446
 
 
1447
                          bsnode=n;
 
1448
                          bsdate=sig->timestamp;
 
1449
                        }
 
1450
                        else
 
1451
                          n->flag|=4; /* older */
 
1452
                      }
 
1453
                  }
1195
1454
            }
1196
1455
            else if( sig->sig_class == 0x28 ) {
1197
1456
              /* We don't actually mark the subkey as revoked right
1200
1459
                 the binding sig is newer than the revocation sig.
1201
1460
                 See the comment in getkey.c:merge_selfsigs_subkey for
1202
1461
                 more */
1203
 
                if( !knode ) {
1204
 
                  if (opt.verbose)
1205
 
                    log_info( _("key %08lX: no subkey for subkey "
1206
 
                                "revocation signature\n"),(ulong)keyid[1]);
1207
 
                  n->flag |= 4; /* delete this */
1208
 
                }
1209
 
                else {
1210
 
                  rc = check_key_signature( keyblock, n, NULL);
1211
 
                  if( rc ) {
1212
 
                    if (opt.verbose)
1213
 
                      log_info(  gpg_err_code (rc) == GPG_ERR_PUBKEY_ALGO ?
1214
 
                            _("key %08lX: unsupported public key algorithm\n"):
1215
 
                            _("key %08lX: invalid subkey revocation\n"),
1216
 
                               (ulong)keyid[1]);
1217
 
                    n->flag|=4;
 
1462
                if( !knode )
 
1463
                  {
 
1464
                    if(opt.verbose)
 
1465
                      log_info( _("key %s: no subkey for key revocation\n"),
 
1466
                                keystr(keyid));
 
1467
                    n->flag |= 4; /* delete this */
1218
1468
                  }
1219
 
                  else {
1220
 
                    /* It's valid, so is it newer? */
1221
 
                    if(sig->timestamp>=rsdate) {
1222
 
                      if(rsnode) {
1223
 
                        rsnode->flag|=4; /* Delete the last revocation
1224
 
                                            sig since this one is
1225
 
                                            newer */
1226
 
                        if (opt.verbose)
1227
 
                          log_info(_("key %08lX: removed multiple subkey "
1228
 
                                     "revocation signatures\n"),
1229
 
                                   (ulong)keyid[1]);
 
1469
                else
 
1470
                  {
 
1471
                    rc = check_key_signature( keyblock, n, NULL);
 
1472
                    if( rc )
 
1473
                      {
 
1474
                        if(opt.verbose)
 
1475
                          log_info(rc == G10ERR_PUBKEY_ALGO ?
 
1476
                                   _("key %s: unsupported public"
 
1477
                                     " key algorithm\n"):
 
1478
                                   _("key %s: invalid subkey revocation\n"),
 
1479
                                   keystr(keyid));
 
1480
                        n->flag|=4;
1230
1481
                      }
 
1482
                    else
 
1483
                      {
 
1484
                        /* It's valid, so is it newer? */
 
1485
                        if(sig->timestamp>=rsdate)
 
1486
                          {
 
1487
                            if(rsnode)
 
1488
                              {
 
1489
                                rsnode->flag|=4; /* Delete the last revocation
 
1490
                                                    sig since this one is
 
1491
                                                    newer */
 
1492
                                if(opt.verbose)
 
1493
                                  log_info(_("key %s: removed multiple subkey"
 
1494
                                             " revocation\n"),keystr(keyid));
 
1495
                              }
1231
1496
 
1232
 
                      rsnode=n;
1233
 
                      rsdate=sig->timestamp;
1234
 
                    }
1235
 
                    else
1236
 
                      n->flag|=4; /* older */
 
1497
                            rsnode=n;
 
1498
                            rsdate=sig->timestamp;
 
1499
                          }
 
1500
                        else
 
1501
                          n->flag|=4; /* older */
 
1502
                      }
1237
1503
                  }
1238
 
                }
1239
1504
            }
1240
1505
        }
1241
1506
        else
1263
1528
        if( node->pkt->pkttype == PKT_USER_ID ) {
1264
1529
            uid_seen = 1;
1265
1530
            if( (node->flag & 2) || !(node->flag & 1) ) {
1266
 
                if( opt.verbose ) {
1267
 
                    log_info( _("key %08lX: skipped user ID '"),
1268
 
                                                         (ulong)keyid[1]);
1269
 
                    print_utf8_string( stderr, node->pkt->pkt.user_id->name,
1270
 
                                       node->pkt->pkt.user_id->len );
1271
 
                    fputs("'\n", stderr );
1272
 
                }
 
1531
                if( opt.verbose )
 
1532
                  {
 
1533
                    char *p=utf8_to_native(node->pkt->pkt.user_id->name,
 
1534
                                           node->pkt->pkt.user_id->len,0);
 
1535
                    log_info( _("key %s: skipped user ID \"%s\"\n"),
 
1536
                              keystr(keyid),p);
 
1537
                    xfree(p);
 
1538
                  }
1273
1539
                delete_kbnode( node ); /* the user-id */
1274
1540
                /* and all following packets up to the next user-id */
1275
1541
                while( node->next
1286
1552
        else if(    node->pkt->pkttype == PKT_PUBLIC_SUBKEY
1287
1553
                 || node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
1288
1554
            if( (node->flag & 2) || !(node->flag & 1) ) {
1289
 
                if( opt.verbose ) {
1290
 
                    log_info( _("key %08lX: skipped subkey\n"),
1291
 
                                                         (ulong)keyid[1]);
1292
 
                }
 
1555
                if( opt.verbose )
 
1556
                  log_info( _("key %s: skipped subkey\n"),keystr(keyid));
 
1557
 
1293
1558
                delete_kbnode( node ); /* the subkey */
1294
1559
                /* and all following signature packets */
1295
1560
                while( node->next
1301
1566
            else
1302
1567
              subkey_seen = 1;
1303
1568
        }
1304
 
        else if( node->pkt->pkttype == PKT_SIGNATURE
1305
 
                 && openpgp_pk_test_algo( node->pkt->pkt.signature
1306
 
                                                     ->pubkey_algo, 0)
1307
 
                 && node->pkt->pkt.signature->pubkey_algo != PUBKEY_ALGO_RSA )
 
1569
        else if (node->pkt->pkttype == PKT_SIGNATURE
 
1570
                && openpgp_pk_test_algo (node->pkt->pkt.signature->pubkey_algo)
 
1571
                && node->pkt->pkt.signature->pubkey_algo != PUBKEY_ALGO_RSA )
1308
1572
            delete_kbnode( node ); /* build_packet() can't handle this */
1309
1573
        else if( node->pkt->pkttype == PKT_SIGNATURE &&
1310
1574
                 !node->pkt->pkt.signature->flags.exportable &&
1311
 
                 !(options&IMPORT_ALLOW_LOCAL_SIGS) &&
1312
 
                 seckey_available( node->pkt->pkt.signature->keyid ) ) {
1313
 
            /* Here we violate the rfc a bit by still allowing
 
1575
                 !(options&IMPORT_LOCAL_SIGS) &&
 
1576
                 seckey_available( node->pkt->pkt.signature->keyid ) )
 
1577
          {
 
1578
            /* here we violate the rfc a bit by still allowing
1314
1579
             * to import non-exportable signature when we have the
1315
1580
             * the secret key used to create this signature - it
1316
 
             * seems that this makes sense. */
1317
 
          if (opt.verbose)
1318
 
            log_info( _("key %08lX: non exportable signature "
1319
 
                                    "(class %02x) - skipped\n"),
1320
 
                      (ulong)keyid[1],
1321
 
                                     node->pkt->pkt.signature->sig_class );
1322
 
          delete_kbnode( node );
1323
 
        }
 
1581
             * seems that this makes sense */
 
1582
            if(opt.verbose)
 
1583
              log_info( _("key %s: non exportable signature"
 
1584
                          " (class 0x%02X) - skipped\n"),
 
1585
                        keystr(keyid), node->pkt->pkt.signature->sig_class );
 
1586
            delete_kbnode( node );
 
1587
          }
1324
1588
        else if( node->pkt->pkttype == PKT_SIGNATURE
1325
1589
                 && node->pkt->pkt.signature->sig_class == 0x20 )  {
1326
 
            if( uid_seen ) {
1327
 
              if (opt.verbose)
1328
 
                log_error( _("key %08lX: revocation certificate "
1329
 
                             "at wrong place - skipped\n"),
1330
 
                                    (ulong)keyid[1]);
1331
 
              delete_kbnode( node );
1332
 
            }
 
1590
            if( uid_seen )
 
1591
              {
 
1592
                if(opt.verbose)
 
1593
                  log_info( _("key %s: revocation certificate"
 
1594
                              " at wrong place - skipped\n"),keystr(keyid));
 
1595
                delete_kbnode( node );
 
1596
              }
1333
1597
            else {
1334
1598
              /* If the revocation cert is from a different key than
1335
1599
                 the one we're working on don't check it - it's
1342
1606
                  int rc = check_key_signature( keyblock, node, NULL);
1343
1607
                  if( rc )
1344
1608
                    {
1345
 
                      if (opt.verbose)
1346
 
                        log_info ( _("key %08lX: invalid revocation "
1347
 
                                     "certificate: %s - skipped\n"),
1348
 
                                   (ulong)keyid[1], gpg_strerror (rc));
 
1609
                      if(opt.verbose)
 
1610
                        log_info( _("key %s: invalid revocation"
 
1611
                                    " certificate: %s - skipped\n"),
 
1612
                                  keystr(keyid), g10_errstr(rc));
1349
1613
                      delete_kbnode( node );
1350
1614
                    }
1351
1615
                }
1354
1618
        else if( node->pkt->pkttype == PKT_SIGNATURE &&
1355
1619
                 (node->pkt->pkt.signature->sig_class == 0x18 ||
1356
1620
                  node->pkt->pkt.signature->sig_class == 0x28) &&
1357
 
                 !subkey_seen ) {
1358
 
          if (opt.verbose)
1359
 
            log_info ( _("key %08lX: subkey signature "
1360
 
                         "in wrong place - skipped\n"),
1361
 
                       (ulong)keyid[1]);
1362
 
          delete_kbnode( node );
1363
 
        }
 
1621
                 !subkey_seen )
 
1622
          {
 
1623
            if(opt.verbose)
 
1624
              log_info( _("key %s: subkey signature"
 
1625
                          " in wrong place - skipped\n"), keystr(keyid));
 
1626
            delete_kbnode( node );
 
1627
          }
1364
1628
        else if( node->pkt->pkttype == PKT_SIGNATURE
1365
1629
                 && !IS_CERT(node->pkt->pkt.signature))
1366
1630
          {
1367
 
            if (opt.verbose)
1368
 
              log_info (_("key %08lX: unexpected signature class (0x%02X) -"
1369
 
                          " skipped\n"),(ulong)keyid[1],
1370
 
                        node->pkt->pkt.signature->sig_class);
 
1631
            if(opt.verbose)
 
1632
              log_info(_("key %s: unexpected signature class (0x%02X) -"
 
1633
                         " skipped\n"),keystr(keyid),
 
1634
                       node->pkt->pkt.signature->sig_class);
1371
1635
            delete_kbnode(node);
1372
1636
          }
1373
1637
        else if( (node->flag & 4) ) /* marked for deletion */
1374
 
            delete_kbnode( node );
 
1638
          delete_kbnode( node );
1375
1639
    }
1376
1640
 
1377
1641
    /* note: because keyblock is the public key, it is never marked
1385
1649
 * It may happen that the imported keyblock has duplicated user IDs.
1386
1650
 * We check this here and collapse those user IDs together with their
1387
1651
 * sigs into one.
1388
 
 * Returns: True if the keyblock hash changed.
 
1652
 * Returns: True if the keyblock has changed.
1389
1653
 */
1390
1654
int
1391
1655
collapse_uids( KBNODE *keyblock )
1392
1656
{
1393
 
    KBNODE n, n2;
1394
 
    int in_uid;
1395
 
    int any=0;
1396
 
    u32 kid1;
1397
 
 
1398
 
  restart:
1399
 
    for( n = *keyblock; n; n = n->next ) {
1400
 
        if( n->pkt->pkttype != PKT_USER_ID )
1401
 
            continue;
1402
 
        for( n2 = n->next; n2; n2 = n2->next ) {
1403
 
            if( n2->pkt->pkttype == PKT_USER_ID
1404
 
                && !cmp_user_ids( n->pkt->pkt.user_id,
1405
 
                                  n2->pkt->pkt.user_id ) ) {
1406
 
                /* found a duplicate */
1407
 
                any = 1;
1408
 
                if( !n2->next
1409
 
                    || n2->next->pkt->pkttype == PKT_USER_ID
1410
 
                    || n2->next->pkt->pkttype == PKT_PUBLIC_SUBKEY
1411
 
                    || n2->next->pkt->pkttype == PKT_SECRET_SUBKEY  ) {
1412
 
                    /* no more signatures: delete the user ID
1413
 
                     * and start over */
1414
 
                    remove_kbnode( keyblock, n2 );
1415
 
                }
1416
 
                else {
1417
 
                    /* The simple approach: Move one signature and
1418
 
                     * then start over to delete the next one :-( */
1419
 
                    move_kbnode( keyblock, n2->next, n->next );
1420
 
                }
1421
 
                goto restart;
1422
 
            }
1423
 
        }
1424
 
    }
1425
 
    if( !any )
1426
 
        return 0;
1427
 
 
1428
 
  restart_sig:
1429
 
    /* now we may have duplicate signatures on one user ID: fix this */
1430
 
    for( in_uid = 0, n = *keyblock; n; n = n->next ) {
1431
 
        if( n->pkt->pkttype == PKT_USER_ID )
1432
 
            in_uid = 1;
1433
 
        else if( n->pkt->pkttype == PKT_PUBLIC_SUBKEY
1434
 
                 || n->pkt->pkttype == PKT_SECRET_SUBKEY )
1435
 
            in_uid = 0;
1436
 
        else if( in_uid ) {
1437
 
            n2 = n;
1438
 
            do {
1439
 
                KBNODE ncmp = NULL;
1440
 
                for( ; n2; n2 = n2->next ) {
1441
 
                    if(    n2->pkt->pkttype == PKT_USER_ID
1442
 
                        || n2->pkt->pkttype == PKT_PUBLIC_SUBKEY
1443
 
                        || n2->pkt->pkttype == PKT_SECRET_SUBKEY )
 
1657
  KBNODE uid1;
 
1658
  int any=0;
 
1659
 
 
1660
  for(uid1=*keyblock;uid1;uid1=uid1->next)
 
1661
    {
 
1662
      KBNODE uid2;
 
1663
 
 
1664
      if(is_deleted_kbnode(uid1))
 
1665
        continue;
 
1666
 
 
1667
      if(uid1->pkt->pkttype!=PKT_USER_ID)
 
1668
        continue;
 
1669
 
 
1670
      for(uid2=uid1->next;uid2;uid2=uid2->next)
 
1671
        {
 
1672
          if(is_deleted_kbnode(uid2))
 
1673
            continue;
 
1674
 
 
1675
          if(uid2->pkt->pkttype!=PKT_USER_ID)
 
1676
            continue;
 
1677
 
 
1678
          if(cmp_user_ids(uid1->pkt->pkt.user_id,
 
1679
                          uid2->pkt->pkt.user_id)==0)
 
1680
            {
 
1681
              /* We have a duplicated uid */
 
1682
              KBNODE sig1,last;
 
1683
 
 
1684
              any=1;
 
1685
 
 
1686
              /* Now take uid2's signatures, and attach them to
 
1687
                 uid1 */
 
1688
              for(last=uid2;last->next;last=last->next)
 
1689
                {
 
1690
                  if(is_deleted_kbnode(last))
 
1691
                    continue;
 
1692
 
 
1693
                  if(last->next->pkt->pkttype==PKT_USER_ID
 
1694
                     || last->next->pkt->pkttype==PKT_PUBLIC_SUBKEY
 
1695
                     || last->next->pkt->pkttype==PKT_SECRET_SUBKEY)
 
1696
                    break;
 
1697
                }
 
1698
 
 
1699
              /* Snip out uid2 */
 
1700
              (find_prev_kbnode(*keyblock,uid2,0))->next=last->next;
 
1701
 
 
1702
              /* Now put uid2 in place as part of uid1 */
 
1703
              last->next=uid1->next;
 
1704
              uid1->next=uid2;
 
1705
              delete_kbnode(uid2);
 
1706
 
 
1707
              /* Now dedupe uid1 */
 
1708
              for(sig1=uid1->next;sig1;sig1=sig1->next)
 
1709
                {
 
1710
                  KBNODE sig2;
 
1711
 
 
1712
                  if(is_deleted_kbnode(sig1))
 
1713
                    continue;
 
1714
 
 
1715
                  if(sig1->pkt->pkttype==PKT_USER_ID
 
1716
                     || sig1->pkt->pkttype==PKT_PUBLIC_SUBKEY
 
1717
                     || sig1->pkt->pkttype==PKT_SECRET_SUBKEY)
 
1718
                    break;
 
1719
 
 
1720
                  if(sig1->pkt->pkttype!=PKT_SIGNATURE)
 
1721
                    continue;
 
1722
 
 
1723
                  for(sig2=sig1->next,last=sig1;sig2;last=sig2,sig2=sig2->next)
 
1724
                    {
 
1725
                      if(is_deleted_kbnode(sig2))
 
1726
                        continue;
 
1727
 
 
1728
                      if(sig2->pkt->pkttype==PKT_USER_ID
 
1729
                         || sig2->pkt->pkttype==PKT_PUBLIC_SUBKEY
 
1730
                         || sig2->pkt->pkttype==PKT_SECRET_SUBKEY)
1444
1731
                        break;
1445
 
                    if( n2->pkt->pkttype != PKT_SIGNATURE )
1446
 
                        ;
1447
 
                    else if( !ncmp )
1448
 
                        ncmp = n2;
1449
 
                    else if( !cmp_signatures( ncmp->pkt->pkt.signature,
1450
 
                                                n2->pkt->pkt.signature )) {
1451
 
                        remove_kbnode( keyblock, n2 );
1452
 
                        goto restart_sig;
 
1732
 
 
1733
                      if(sig2->pkt->pkttype!=PKT_SIGNATURE)
 
1734
                        continue;
 
1735
 
 
1736
                      if(cmp_signatures(sig1->pkt->pkt.signature,
 
1737
                                        sig2->pkt->pkt.signature)==0)
 
1738
                        {
 
1739
                          /* We have a match, so delete the second
 
1740
                             signature */
 
1741
                          delete_kbnode(sig2);
 
1742
                          sig2=last;
 
1743
                        }
1453
1744
                    }
1454
1745
                }
1455
 
                n2 = ncmp? ncmp->next : NULL;
1456
 
            } while( n2 );
 
1746
            }
1457
1747
        }
1458
1748
    }
1459
1749
 
1460
 
    if( (n = find_kbnode( *keyblock, PKT_PUBLIC_KEY )) )
1461
 
        kid1 = keyid_from_pk( n->pkt->pkt.public_key, NULL );
1462
 
    else if( (n = find_kbnode( *keyblock, PKT_SECRET_KEY )) )
1463
 
        kid1 = keyid_from_sk( n->pkt->pkt.secret_key, NULL );
1464
 
    else
1465
 
        kid1 = 0;
1466
 
    if (!opt.quiet)
1467
 
      log_info (_("key %08lX: duplicated user ID detected - merged\n"),
1468
 
                (ulong)kid1);
1469
 
 
1470
 
    return 1;
 
1750
  commit_kbnode(keyblock);
 
1751
 
 
1752
  if(any && !opt.quiet)
 
1753
    {
 
1754
      const char *key="???";
 
1755
 
 
1756
      if( (uid1=find_kbnode( *keyblock, PKT_PUBLIC_KEY )) )
 
1757
        key=keystr_from_pk(uid1->pkt->pkt.public_key);
 
1758
      else if( (uid1 = find_kbnode( *keyblock, PKT_SECRET_KEY )) )
 
1759
        key=keystr_from_sk(uid1->pkt->pkt.secret_key);
 
1760
 
 
1761
      log_info(_("key %s: duplicated user ID detected - merged\n"),key);
 
1762
    }
 
1763
 
 
1764
  return any;
1471
1765
}
1472
1766
 
1473
1767
/* Check for a 0x20 revocation from a revocation key that is not
1474
 
   present.  This gets called without the benefit of merge_xxxx so you
1475
 
   can't rely on pk->revkey and friends. */
 
1768
   present.  This may be called without the benefit of merge_xxxx so
 
1769
   you can't rely on pk->revkey and friends. */
1476
1770
static void
1477
1771
revocation_present(KBNODE keyblock)
1478
1772
{
1517
1811
 
1518
1812
                      rc=get_pubkey_byfprint_fast (NULL,sig->revkey[idx]->fpr,
1519
1813
                                                   MAX_FINGERPRINT_LEN);
1520
 
                      if ( gpg_err_code (rc) == GPG_ERR_NO_PUBKEY
1521
 
                          || gpg_err_code (rc) == GPG_ERR_UNUSABLE_PUBKEY)
 
1814
                      if(rc==G10ERR_NO_PUBKEY || rc==G10ERR_UNU_PUBKEY)
1522
1815
                        {
 
1816
                          char *tempkeystr=xstrdup(keystr_from_pk(pk));
 
1817
 
1523
1818
                          /* No, so try and get it */
1524
 
                          if(opt.keyserver_scheme &&
1525
 
                             opt.keyserver_options.auto_key_retrieve)
 
1819
                          if(opt.keyserver
 
1820
                             && (opt.keyserver_options.options
 
1821
                                 & KEYSERVER_AUTO_KEY_RETRIEVE))
1526
1822
                            {
1527
 
                              log_info(_("WARNING: key %08lX may be revoked: "
1528
 
                                         "fetching revocation key %08lX\n"),
1529
 
                                       (ulong)keyid_from_pk(pk,NULL),
1530
 
                                       (ulong)keyid[1]);
 
1823
                              log_info(_("WARNING: key %s may be revoked:"
 
1824
                                         " fetching revocation key %s\n"),
 
1825
                                       tempkeystr,keystr(keyid));
1531
1826
                              keyserver_import_fprint(sig->revkey[idx]->fpr,
1532
 
                                                      MAX_FINGERPRINT_LEN);
 
1827
                                                      MAX_FINGERPRINT_LEN,
 
1828
                                                      opt.keyserver);
1533
1829
 
1534
1830
                              /* Do we have it now? */
1535
1831
                              rc=get_pubkey_byfprint_fast (NULL,
1537
1833
                                                     MAX_FINGERPRINT_LEN);
1538
1834
                            }
1539
1835
 
1540
 
                          if (gpg_err_code (rc) == GPG_ERR_NO_PUBKEY
1541
 
                              || gpg_err_code (rc) == GPG_ERR_UNUSABLE_PUBKEY)
1542
 
                            log_info(_("WARNING: key %08lX may be revoked: "
1543
 
                                       "revocation key %08lX not present.\n"),
1544
 
                                     (ulong)keyid_from_pk(pk,NULL),
1545
 
                                     (ulong)keyid[1]);
 
1836
                          if(rc==G10ERR_NO_PUBKEY || rc==G10ERR_UNU_PUBKEY)
 
1837
                            log_info(_("WARNING: key %s may be revoked:"
 
1838
                                       " revocation key %s not present.\n"),
 
1839
                                     tempkeystr,keystr(keyid));
 
1840
 
 
1841
                          xfree(tempkeystr);
1546
1842
                        }
1547
1843
                    }
1548
1844
                }
1584
1880
                         && onode->pkt->pkt.signature->sig_class == 0x20
1585
1881
                         && !cmp_signatures(onode->pkt->pkt.signature,
1586
1882
                                            node->pkt->pkt.signature))
1587
 
                  {
1588
 
                    found = 1;
1589
 
                    break;
1590
 
                  }
 
1883
                  {
 
1884
                    found = 1;
 
1885
                    break;
 
1886
                  }
1591
1887
            }
1592
1888
            if( !found ) {
1593
1889
                KBNODE n2 = clone_kbnode(node);
1594
1890
                insert_kbnode( keyblock_orig, n2, 0 );
1595
1891
                n2->flag |= 1;
1596
1892
                ++*n_sigs;
1597
 
 
1598
 
                if (!opt.quiet)
1599
 
                  {
1600
 
                    char *p=get_user_id_printable (keyid);
1601
 
                    log_info(_("key %08lX: \"%s\" "
1602
 
                               "revocation certificate added\n"),
1603
 
                             (ulong)keyid[1],p);
1604
 
                    xfree (p);
1605
 
                  }
 
1893
                if(!opt.quiet)
 
1894
                  {
 
1895
                    char *p=get_user_id_native (keyid);
 
1896
                    log_info(_("key %s: \"%s\" revocation"
 
1897
                               " certificate added\n"), keystr(keyid),p);
 
1898
                    xfree(p);
 
1899
                  }
1606
1900
            }
1607
1901
        }
1608
1902
    }
1626
1920
                    break;
1627
1921
                }
1628
1922
            }
1629
 
            if( !found ) {
 
1923
            if( !found )
 
1924
              {
1630
1925
                KBNODE n2 = clone_kbnode(node);
1631
1926
                insert_kbnode( keyblock_orig, n2, 0 );
1632
1927
                n2->flag |= 1;
1633
1928
                ++*n_sigs;
1634
 
                if (!opt.quiet)
1635
 
                  log_info( _("key %08lX: direct key signature added\n"),
1636
 
                            (ulong)keyid[1]);
1637
 
            }
 
1929
                if(!opt.quiet)
 
1930
                  log_info( _("key %s: direct key signature added\n"),
 
1931
                            keystr(keyid));
 
1932
              }
1638
1933
        }
1639
1934
    }
1640
1935
 
1806
2101
            {
1807
2102
              found++;
1808
2103
              break;
1809
 
            }   
 
2104
            }
1810
2105
        if( !found ) {
1811
2106
            /* This signature is new or newer, append N to DST.
1812
2107
             * We add a clone to the original keyblock, because this
1904
2199
 
1905
2200
    return 0;
1906
2201
}
 
2202
 
 
2203
 
 
2204
 
 
2205
/* Walk a public keyblock and produce a secret keyblock out of it.
 
2206
   Instead of inserting the secret key parameters (which we don't
 
2207
   have), we insert a stub.  */
 
2208
static KBNODE
 
2209
pub_to_sec_keyblock (KBNODE pub_keyblock)
 
2210
{
 
2211
  KBNODE pubnode, secnode;
 
2212
  KBNODE sec_keyblock = NULL;
 
2213
  KBNODE walkctx = NULL;
 
2214
 
 
2215
  while((pubnode = walk_kbnode (pub_keyblock,&walkctx,0)))
 
2216
    {
 
2217
      if (pubnode->pkt->pkttype == PKT_PUBLIC_KEY
 
2218
          || pubnode->pkt->pkttype == PKT_PUBLIC_SUBKEY)
 
2219
        {
 
2220
          /* Make a secret key.  We only need to convert enough to
 
2221
             write the keyblock out. */
 
2222
          PKT_public_key *pk = pubnode->pkt->pkt.public_key;
 
2223
          PACKET *pkt = xmalloc_clear (sizeof *pkt);
 
2224
          PKT_secret_key *sk = xmalloc_clear (sizeof *sk);
 
2225
          int i, n;
 
2226
          
 
2227
          if (pubnode->pkt->pkttype == PKT_PUBLIC_KEY)
 
2228
            pkt->pkttype = PKT_SECRET_KEY;
 
2229
          else
 
2230
            pkt->pkttype = PKT_SECRET_SUBKEY;
 
2231
          
 
2232
          pkt->pkt.secret_key = sk;
 
2233
 
 
2234
          copy_public_parts_to_secret_key ( pk, sk );
 
2235
          sk->version     = pk->version;
 
2236
          sk->timestamp   = pk->timestamp;
 
2237
        
 
2238
          n = pubkey_get_npkey (pk->pubkey_algo);
 
2239
          if (!n)
 
2240
            n = 1; /* Unknown number of parameters, however the data
 
2241
                      is stored in the first mpi. */
 
2242
          for (i=0; i < n; i++ )
 
2243
            sk->skey[i] = mpi_copy (pk->pkey[i]);
 
2244
  
 
2245
          sk->is_protected = 1;
 
2246
          sk->protect.s2k.mode = 1001;
 
2247
  
 
2248
          secnode = new_kbnode (pkt);
 
2249
        }
 
2250
      else
 
2251
        {
 
2252
          secnode = clone_kbnode (pubnode);
 
2253
        }
 
2254
      
 
2255
      if(!sec_keyblock)
 
2256
        sec_keyblock = secnode;
 
2257
      else
 
2258
        add_kbnode (sec_keyblock, secnode);
 
2259
    }
 
2260
 
 
2261
  return sec_keyblock;
 
2262
}
 
2263
 
 
2264
 
 
2265
/* Walk over the secret keyring SEC_KEYBLOCK and update any simple
 
2266
   stub keys with the serial number SNNUM of the card if one of the
 
2267
   fingerprints FPR1, FPR2 or FPR3 match.  Print a note if the key is
 
2268
   a duplicate (may happen in case of backed uped keys). 
 
2269
   
 
2270
   Returns: True if anything changed.
 
2271
*/
 
2272
static int
 
2273
update_sec_keyblock_with_cardinfo (KBNODE sec_keyblock, 
 
2274
                                   const unsigned char *fpr1,
 
2275
                                   const unsigned char *fpr2,
 
2276
                                   const unsigned char *fpr3,
 
2277
                                   const char *serialnostr)
 
2278
{
 
2279
  KBNODE node;
 
2280
  KBNODE walkctx = NULL;
 
2281
  PKT_secret_key *sk;
 
2282
  byte array[MAX_FINGERPRINT_LEN];
 
2283
  size_t n;
 
2284
  int result = 0;
 
2285
  const char *s;
 
2286
 
 
2287
  while((node = walk_kbnode (sec_keyblock, &walkctx, 0)))
 
2288
    {
 
2289
      if (node->pkt->pkttype != PKT_SECRET_KEY
 
2290
          && node->pkt->pkttype != PKT_SECRET_SUBKEY)
 
2291
        continue;
 
2292
      sk = node->pkt->pkt.secret_key;
 
2293
      
 
2294
      fingerprint_from_sk (sk, array, &n);
 
2295
      if (n != 20)
 
2296
        continue; /* Can't be a card key.  */
 
2297
      if ( !((fpr1 && !memcmp (array, fpr1, 20))
 
2298
             || (fpr2 && !memcmp (array, fpr2, 20))
 
2299
             || (fpr3 && !memcmp (array, fpr3, 20))) )
 
2300
        continue;  /* No match.  */
 
2301
 
 
2302
      if (sk->is_protected == 1 && sk->protect.s2k.mode == 1001)
 
2303
        {
 
2304
          /* Standard case: migrate that stub to a key stub.  */
 
2305
          sk->protect.s2k.mode = 1002;
 
2306
          s = serialnostr;
 
2307
          for (sk->protect.ivlen=0; sk->protect.ivlen < 16 && *s && s[1];
 
2308
               sk->protect.ivlen++, s += 2)
 
2309
            sk->protect.iv[sk->protect.ivlen] = xtoi_2 (s);
 
2310
          result = 1;
 
2311
        }
 
2312
      else if (sk->is_protected == 1 && sk->protect.s2k.mode == 1002)
 
2313
        {
 
2314
          s = serialnostr;
 
2315
          for (sk->protect.ivlen=0; sk->protect.ivlen < 16 && *s && s[1];
 
2316
               sk->protect.ivlen++, s += 2)
 
2317
            if (sk->protect.iv[sk->protect.ivlen] != xtoi_2 (s))
 
2318
              {
 
2319
                log_info (_("NOTE: a key's S/N does not "
 
2320
                            "match the card's one\n"));
 
2321
                break;
 
2322
              }
 
2323
        }
 
2324
      else
 
2325
        {
 
2326
          if (node->pkt->pkttype != PKT_SECRET_KEY)
 
2327
            log_info (_("NOTE: primary key is online and stored on card\n"));
 
2328
          else
 
2329
            log_info (_("NOTE: secondary key is online and stored on card\n"));
 
2330
        }
 
2331
    }
 
2332
 
 
2333
  return result;
 
2334
}
 
2335
 
 
2336
 
 
2337
 
 
2338
/* Check whether a secret key stub exists for the public key PK.  If
 
2339
   not create such a stub key and store it into the secring.  If it
 
2340
   exists, add appropriate subkey stubs and update the secring.
 
2341
   Return 0 if the key could be created. */
 
2342
int
 
2343
auto_create_card_key_stub ( const char *serialnostr, 
 
2344
                            const unsigned char *fpr1,
 
2345
                            const unsigned char *fpr2,
 
2346
                            const unsigned char *fpr3)
 
2347
{
 
2348
  KBNODE pub_keyblock;
 
2349
  KBNODE sec_keyblock;
 
2350
  KEYDB_HANDLE hd;
 
2351
  int rc;
 
2352
 
 
2353
  /* We only want to do this for an OpenPGP card.  */
 
2354
  if (!serialnostr || strncmp (serialnostr, "D27600012401", 12) 
 
2355
      || strlen (serialnostr) != 32 )
 
2356
    return G10ERR_GENERAL;
 
2357
 
 
2358
  /* First get the public keyring from any of the provided fingerprints. */
 
2359
  if ( (fpr1 && !get_keyblock_byfprint (&pub_keyblock, fpr1, 20))
 
2360
       || (fpr2 && !get_keyblock_byfprint (&pub_keyblock, fpr2, 20))
 
2361
       || (fpr3 && !get_keyblock_byfprint (&pub_keyblock, fpr3, 20)))
 
2362
    ;
 
2363
  else
 
2364
    return G10ERR_GENERAL;
 
2365
 
 
2366
  hd = keydb_new (1);
 
2367
 
 
2368
  /* Now check whether there is a secret keyring.  */
 
2369
  {
 
2370
    PKT_public_key *pk = pub_keyblock->pkt->pkt.public_key;
 
2371
    byte afp[MAX_FINGERPRINT_LEN];
 
2372
    size_t an;
 
2373
 
 
2374
    fingerprint_from_pk (pk, afp, &an);
 
2375
    if (an < MAX_FINGERPRINT_LEN)
 
2376
      memset (afp+an, 0, MAX_FINGERPRINT_LEN-an);
 
2377
    rc = keydb_search_fpr (hd, afp);
 
2378
  }
 
2379
 
 
2380
  if (!rc)
 
2381
    {
 
2382
      rc = keydb_get_keyblock (hd, &sec_keyblock);
 
2383
      if (rc)
 
2384
        {
 
2385
          log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
 
2386
          rc = G10ERR_GENERAL;
 
2387
        }
 
2388
      else
 
2389
        {
 
2390
          merge_keys_and_selfsig (sec_keyblock);
 
2391
          
 
2392
          /* FIXME: We need to add new subkeys first.  */
 
2393
          if (update_sec_keyblock_with_cardinfo (sec_keyblock,
 
2394
                                                 fpr1, fpr2, fpr3,
 
2395
                                                 serialnostr))
 
2396
            {
 
2397
              rc = keydb_update_keyblock (hd, sec_keyblock );
 
2398
              if (rc)
 
2399
                log_error (_("error writing keyring `%s': %s\n"),
 
2400
                           keydb_get_resource_name (hd), g10_errstr(rc) );
 
2401
            }
 
2402
        }
 
2403
    }
 
2404
  else  /* A secret key does not exists - create it.  */
 
2405
    {
 
2406
      sec_keyblock = pub_to_sec_keyblock (pub_keyblock);
 
2407
      update_sec_keyblock_with_cardinfo (sec_keyblock,
 
2408
                                         fpr1, fpr2, fpr3,
 
2409
                                         serialnostr);
 
2410
 
 
2411
      rc = keydb_locate_writable (hd, NULL);
 
2412
      if (rc)
 
2413
        {
 
2414
          log_error (_("no default secret keyring: %s\n"), g10_errstr (rc));
 
2415
          rc = G10ERR_GENERAL;
 
2416
        }
 
2417
      else
 
2418
        {
 
2419
          rc = keydb_insert_keyblock (hd, sec_keyblock );
 
2420
          if (rc)
 
2421
            log_error (_("error writing keyring `%s': %s\n"),
 
2422
                       keydb_get_resource_name (hd), g10_errstr(rc) );
 
2423
        }
 
2424
    }
 
2425
    
 
2426
  release_kbnode (sec_keyblock);
 
2427
  release_kbnode (pub_keyblock);
 
2428
  keydb_release (hd);
 
2429
  return rc;
 
2430
}