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

« back to all changes in this revision

Viewing changes to g10/pkclist.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
 
/* pkclist.c
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2002
3
 
 *               2003 Free Software Foundation, Inc.
 
1
/* pkclist.c - create a list of public keys
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
 
3
 *               2006 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 "main.h"
36
35
#include "trustdb.h"
39
38
#include "photoid.h"
40
39
#include "i18n.h"
41
40
 
42
 
 
43
41
#define CONTROL_D ('D' - 'A' + 1)
44
42
 
45
 
 
46
43
/****************
47
44
 * Show the revocation reason as it is stored with the given signature
48
45
 */
74
71
 
75
72
        log_info( _("reason for revocation: ") );
76
73
        if( text )
77
 
            fputs( text, log_get_stream () );
 
74
            fputs( text, log_get_stream() );
78
75
        else
79
 
            fprintf( log_get_stream (), "code=%02x", *p );
80
 
        putc( '\n', log_get_stream () );
 
76
            fprintf( log_get_stream(), "code=%02x", *p );
 
77
        log_printf ("\n");
81
78
        n--; p++;
82
79
        pp = NULL;
83
80
        do {
89
86
            if( n ) {
90
87
                pp = memchr( p, '\n', n );
91
88
                nn = pp? pp - p : n;
92
 
                log_info( _("revocation comment: ") );
93
 
                print_string( log_get_stream(), p, nn, 0 );
94
 
                putc( '\n', log_get_stream() );
 
89
                log_info ( _("revocation comment: ") );
 
90
                print_string ( log_get_stream(), p, nn, 0 );
 
91
                log_printf ("\n");
95
92
                p += nn; n -= nn;
96
93
            }
97
94
        } while( pp );
158
155
}
159
156
 
160
157
 
161
 
static void
162
 
show_paths (const PKT_public_key *pk, int only_first )
163
 
{
164
 
    log_debug("not yet implemented\n");
165
 
#if 0    
166
 
    void *context = NULL;
167
 
    unsigned otrust, validity;
168
 
    int last_level, level;
169
 
 
170
 
    last_level = 0;
171
 
    while( (level=enum_cert_paths( &context, &lid, &otrust, &validity)) != -1){
172
 
        char *p;
173
 
        int c, rc;
174
 
        size_t n;
175
 
        u32 keyid[2];
176
 
        PKT_public_key *pk ;
177
 
 
178
 
        if( level < last_level && only_first )
179
 
            break;
180
 
        last_level = level;
181
 
 
182
 
        rc = keyid_from_lid( lid, keyid );
183
 
 
184
 
        if( rc ) {
185
 
            log_error("ooops: can't get keyid for lid %lu\n", lid);
186
 
            return;
187
 
        }
188
 
 
189
 
        pk = xcalloc (1, sizeof *pk );
190
 
        rc = get_pubkey( pk, keyid );
191
 
        if( rc ) {
192
 
            log_error("key %08lX: public key not found: %s\n",
193
 
                                    (ulong)keyid[1], gpg_strerror (rc) );
194
 
            return;
195
 
        }
196
 
 
197
 
        tty_printf("%*s%4u%c/%08lX.%lu %s \"",
198
 
                  level*2, "",
199
 
                  nbits_from_pk( pk ), pubkey_letter( pk->pubkey_algo ),
200
 
                  (ulong)keyid[1], lid, datestr_from_pk( pk ) );
201
 
 
202
 
        c = trust_letter(otrust);
203
 
        if( c )
204
 
            putchar( c );
205
 
        else
206
 
            printf( "%02x", otrust );
207
 
        putchar('/');
208
 
        c = trust_letter(validity);
209
 
        if( c )
210
 
            putchar( c );
211
 
        else
212
 
            printf( "%02x", validity );
213
 
        putchar(' ');
214
 
 
215
 
        p = get_user_id( keyid, &n );
216
 
        tty_print_utf8_string( p, n ),
217
 
        xfree (p);
218
 
        tty_printf("\"\n");
219
 
        free_public_key( pk );
220
 
    }
221
 
    enum_cert_paths( &context, NULL, NULL, NULL ); /* release context */
222
 
#endif
223
 
    tty_printf("\n");
224
 
}
225
 
 
226
 
 
227
 
 
228
 
 
229
158
/****************
230
159
 * mode: 0 = standard
231
160
 *       1 = Without key info and additional menu option 'm'
241
170
                    unsigned *new_trust, int defer_help )
242
171
{
243
172
  char *p;
244
 
  size_t n;
245
173
  u32 keyid[2];
246
174
  int changed=0;
247
175
  int quit=0;
252
180
 
253
181
  switch(minimum)
254
182
    {
255
 
    default:              min_num=0; break;
 
183
    default:
256
184
    case TRUST_UNDEFINED: min_num=1; break;
257
185
    case TRUST_NEVER:     min_num=2; break;
258
186
    case TRUST_MARGINAL:  min_num=3; break;
261
189
 
262
190
  keyid_from_pk (pk, keyid);
263
191
  for(;;) {
264
 
    /* a string with valid answers */
 
192
    /* A string with valid answers.
 
193
 
 
194
       Note to translators: These are the allowed answers in lower and
 
195
       uppercase.  Below you will find the matching strings which
 
196
       should be translated accordingly and the letter changed to
 
197
       match the one in the answer string.
 
198
    
 
199
         i = please show me more information
 
200
         m = back to the main menu
 
201
         s = skip this key
 
202
         q = quit
 
203
    */
265
204
    const char *ans = _("iImMqQsS");
266
205
 
267
206
    if( !did_help ) 
270
209
          {
271
210
            KBNODE keyblock, un;
272
211
 
273
 
            tty_printf(_("No trust value assigned to:\n"
274
 
                         "%4u%c/%08lX %s \""),
275
 
                       nbits_from_pk( pk ), pubkey_letter( pk->pubkey_algo ),
276
 
                       (ulong)keyid[1], datestr_from_pk( pk ) );
277
 
            p = get_user_id( keyid, &n );
278
 
            tty_print_utf8_string( p, n ),
279
 
              xfree (p);
280
 
            tty_printf("\"\n");
 
212
            tty_printf(_("No trust value assigned to:\n"));
 
213
            tty_printf("%4u%c/%s %s\n",nbits_from_pk( pk ),
 
214
                       pubkey_letter( pk->pubkey_algo ),
 
215
                       keystr(keyid), datestr_from_pk( pk ) );
 
216
            p=get_user_id_native(keyid);
 
217
            tty_printf(_("      \"%s\"\n"),p);
 
218
            xfree(p);
281
219
 
282
220
            keyblock = get_pubkeyblock (keyid);
283
221
            if (!keyblock)
284
222
                BUG ();
285
 
            for (un=keyblock; un; un = un->next) {
 
223
            for (un=keyblock; un; un = un->next)
 
224
              {
286
225
                if (un->pkt->pkttype != PKT_USER_ID )
287
 
                    continue;
 
226
                  continue;
288
227
                if (un->pkt->pkt.user_id->is_revoked )
289
 
                    continue;
 
228
                  continue;
290
229
                if (un->pkt->pkt.user_id->is_expired )
291
 
                    continue;
 
230
                  continue;
292
231
                /* Only skip textual primaries */
293
 
                if (un->pkt->pkt.user_id->is_primary &&
294
 
                    !un->pkt->pkt.user_id->attrib_data )
295
 
                    continue;
 
232
                if (un->pkt->pkt.user_id->is_primary
 
233
                    && !un->pkt->pkt.user_id->attrib_data )
 
234
                  continue;
296
235
                
297
236
                if((opt.verify_options&VERIFY_SHOW_PHOTOS)
298
237
                   && un->pkt->pkt.user_id->attrib_data)
299
 
                    show_photos(un->pkt->pkt.user_id->attribs,
300
 
                                un->pkt->pkt.user_id->numattribs,pk,NULL);
301
 
                
302
 
                tty_printf ("      %s", _("                aka \""));
303
 
                tty_print_utf8_string (un->pkt->pkt.user_id->name,
304
 
                                       un->pkt->pkt.user_id->len );
305
 
                tty_printf("\"\n");
306
 
            }
 
238
                  show_photos(un->pkt->pkt.user_id->attribs,
 
239
                              un->pkt->pkt.user_id->numattribs,pk,NULL);
 
240
 
 
241
                p=utf8_to_native(un->pkt->pkt.user_id->name,
 
242
                                 un->pkt->pkt.user_id->len,0);
 
243
 
 
244
                tty_printf(_("  aka \"%s\"\n"),p);
 
245
              }
307
246
        
308
247
            print_fingerprint (pk, NULL, 2);
309
248
            tty_printf("\n");
 
249
            release_kbnode (keyblock);
310
250
          }
311
 
        /* This string also used in keyedit.c:sign_uids */
312
 
        tty_printf (_(
313
 
                     "Please decide how far you trust this user to correctly\n"
314
 
                     "verify other users' keys (by looking at passports,\n"
315
 
                     "checking fingerprints from different sources...)?\n\n"));
 
251
 
 
252
        if(opt.trust_model==TM_DIRECT)
 
253
          {
 
254
            tty_printf(_("How much do you trust that this key actually "
 
255
                         "belongs to the named user?\n"));
 
256
            tty_printf("\n");
 
257
          }
 
258
        else
 
259
          {
 
260
            /* This string also used in keyedit.c:trustsig_prompt */
 
261
            tty_printf(_("Please decide how far you trust this user to"
 
262
                         " correctly verify other users' keys\n"
 
263
                         "(by looking at passports, checking fingerprints from"
 
264
                         " different sources, etc.)\n"));
 
265
            tty_printf("\n");
 
266
          }
 
267
 
316
268
        if(min_num<=1)
317
 
          tty_printf (_(" %d = I don't know\n"), 1);
 
269
          tty_printf (_("  %d = I don't know or won't say\n"), 1);
318
270
        if(min_num<=2)
319
 
          tty_printf (_(" %d = I do NOT trust\n"), 2);
 
271
          tty_printf (_("  %d = I do NOT trust\n"), 2);
320
272
        if(min_num<=3)
321
 
          tty_printf (_(" %d = I trust marginally\n"), 3);
 
273
          tty_printf (_("  %d = I trust marginally\n"), 3);
322
274
        if(min_num<=4)
323
 
          tty_printf (_(" %d = I trust fully\n"), 4);
 
275
          tty_printf (_("  %d = I trust fully\n"), 4);
324
276
        if (mode)
325
 
          tty_printf (_(" %d = I trust ultimately\n"), 5);
 
277
          tty_printf (_("  %d = I trust ultimately\n"), 5);
326
278
#if 0
327
279
        /* not yet implemented */
328
 
        tty_printf (_(" i = please show me more information\n") );
 
280
        tty_printf ("  i = please show me more information\n");
329
281
#endif
330
282
        if( mode )
331
 
          tty_printf(_(" m = back to the main menu\n"));
 
283
          tty_printf(_("  m = back to the main menu\n"));
332
284
        else
333
285
          {
334
 
            tty_printf(_(" s = skip this key\n"));
335
 
            tty_printf(_(" q = quit\n"));
 
286
            tty_printf(_("  s = skip this key\n"));
 
287
            tty_printf(_("  q = quit\n"));
336
288
          }
337
289
        tty_printf("\n");
338
290
        if(minimum)
364
316
        if (trust == TRUST_ULTIMATE
365
317
            && !cpr_get_answer_is_yes ("edit_ownertrust.set_ultimate.okay",
366
318
                                       _("Do you really want to set this key"
367
 
                                         " to ultimate trust? ")))
 
319
                                         " to ultimate trust? (y/N) ")))
368
320
          ; /* no */
369
321
        else
370
322
          {
395
347
        quit = 1;
396
348
        break ; /* back to the menu */
397
349
      }
398
 
    xfree (p); p = NULL;
 
350
    xfree(p); p = NULL;
399
351
  }
400
 
  xfree (p);
 
352
  xfree(p);
401
353
  return show? -2: quit? -1 : changed;
402
354
}
403
355
 
409
361
int
410
362
edit_ownertrust (PKT_public_key *pk, int mode )
411
363
{
412
 
  unsigned int trust;
 
364
  unsigned int trust = 0;
413
365
  int no_help = 0;
414
366
 
415
367
  for(;;)
419
371
        case -1: /* quit */
420
372
          return -1;
421
373
        case -2: /* show info */
422
 
          show_paths(pk, 1);
423
374
          no_help = 1;
424
375
          break;
425
376
        case 1: /* trust value set */
439
390
 * Returns: true if we trust.
440
391
 */
441
392
static int
442
 
do_we_trust( PKT_public_key *pk, unsigned int *trustlevel )
 
393
do_we_trust( PKT_public_key *pk, unsigned int trustlevel )
443
394
{
444
 
    unsigned int trustmask = 0;
445
 
 
446
 
    /* FIXME: get_pubkey_byname already checks the validity and won't
447
 
     * return keys which are either expired or revoked - so these
448
 
     * question here won't get triggered.  We have to find a solution
449
 
     * for this.  It might make sense to have a function in getkey.c
450
 
     * which does only the basic checks and returns even revoked and
451
 
     * expired keys.  This fnction could then also returhn a list of
452
 
     * keys if the speicified name is ambiguous
453
 
     */
454
 
    if( (*trustlevel & TRUST_FLAG_REVOKED) ) {
455
 
        log_info(_("key %08lX: key has been revoked!\n"),
456
 
                                        (ulong)keyid_from_pk( pk, NULL) );
457
 
        show_revocation_reason( pk, 0 );
458
 
        if( opt.batch )
459
 
          return 0; /* no */
460
 
 
461
 
        if( !cpr_get_answer_is_yes("revoked_key.override",
462
 
                                    _("Use this key anyway? ")) )
463
 
          return 0; /* no */
464
 
        trustmask |= TRUST_FLAG_REVOKED;
465
 
    }
466
 
    if( (*trustlevel & TRUST_FLAG_SUB_REVOKED) ) {
467
 
        log_info(_("key %08lX: subkey has been revoked!\n"),
468
 
                                        (ulong)keyid_from_pk( pk, NULL) );
469
 
        show_revocation_reason( pk, 0 );
470
 
        if( opt.batch )
471
 
            return 0;
472
 
 
473
 
        if( !cpr_get_answer_is_yes("revoked_key.override",
474
 
                                    _("Use this key anyway? ")) )
475
 
            return 0;
476
 
        trustmask |= TRUST_FLAG_SUB_REVOKED;
477
 
    }
478
 
    *trustlevel &= ~trustmask;
479
 
 
480
 
    if( opt.trust_model==TM_ALWAYS ) {
481
 
        if( opt.verbose )
482
 
            log_info("No trust check due to --trust-model always option\n");
483
 
        return 1;
484
 
    }
485
 
 
486
 
    switch( (*trustlevel & TRUST_MASK) ) {
487
 
      case TRUST_EXPIRED:
488
 
        log_info(_("%08lX: key has expired\n"),
489
 
                                    (ulong)keyid_from_pk( pk, NULL) );
490
 
        return 0; /* no */
491
 
 
492
 
      default:
493
 
         log_error ("invalid trustlevel %u returned from validation layer\n",
494
 
                    *trustlevel);
495
 
         /* fall thru */
496
 
      case TRUST_UNKNOWN: 
497
 
      case TRUST_UNDEFINED:
498
 
        log_info(_("%08lX: There is no assurance this key belongs "
499
 
                   "to the named user\n"),(ulong)keyid_from_pk( pk, NULL) );
500
 
        return 0; /* no */
501
 
 
502
 
        /* No way to get here? */
503
 
      case TRUST_NEVER:
504
 
        log_info(_("%08lX: We do NOT trust this key\n"),
505
 
                                        (ulong)keyid_from_pk( pk, NULL) );
506
 
        return 0; /* no */
507
 
 
508
 
      case TRUST_MARGINAL:
509
 
        log_info(_("%08lX: There is limited assurance this key belongs "
510
 
                   "to the named user\n"),(ulong)keyid_from_pk(pk,NULL));
511
 
        return 1; /* yes */
512
 
 
513
 
      case TRUST_FULLY:
514
 
        if( opt.verbose )
515
 
            log_info(_("This key probably belongs to the named user\n"));
516
 
        return 1; /* yes */
517
 
 
518
 
      case TRUST_ULTIMATE:
519
 
        if( opt.verbose )
520
 
            log_info(_("This key belongs to us\n"));
521
 
        return 1; /* yes */
522
 
    }
523
 
 
524
 
    return 1; /* yes */
 
395
  /* We should not be able to get here with a revoked or expired
 
396
     key */
 
397
  if(trustlevel & TRUST_FLAG_REVOKED
 
398
     || trustlevel & TRUST_FLAG_SUB_REVOKED
 
399
     || (trustlevel & TRUST_MASK) == TRUST_EXPIRED)
 
400
    BUG();
 
401
 
 
402
  if( opt.trust_model==TM_ALWAYS )
 
403
    {
 
404
      if( opt.verbose )
 
405
        log_info("No trust check due to `--trust-model always' option\n");
 
406
      return 1;
 
407
    }
 
408
 
 
409
  switch(trustlevel & TRUST_MASK)
 
410
    {
 
411
    default:
 
412
      log_error ("invalid trustlevel %u returned from validation layer\n",
 
413
                 trustlevel);
 
414
      /* fall thru */
 
415
    case TRUST_UNKNOWN: 
 
416
    case TRUST_UNDEFINED:
 
417
      log_info(_("%s: There is no assurance this key belongs"
 
418
                 " to the named user\n"),keystr_from_pk(pk));
 
419
      return 0; /* no */
 
420
 
 
421
    case TRUST_MARGINAL:
 
422
      log_info(_("%s: There is limited assurance this key belongs"
 
423
                 " to the named user\n"),keystr_from_pk(pk));
 
424
      return 1; /* yes */
 
425
 
 
426
    case TRUST_FULLY:
 
427
      if( opt.verbose )
 
428
        log_info(_("This key probably belongs to the named user\n"));
 
429
      return 1; /* yes */
 
430
 
 
431
    case TRUST_ULTIMATE:
 
432
      if( opt.verbose )
 
433
        log_info(_("This key belongs to us\n"));
 
434
      return 1; /* yes */
 
435
    }
 
436
 
 
437
  return 1; /*NOTREACHED*/
525
438
}
526
439
 
527
440
 
528
 
 
529
441
/****************
530
442
 * wrapper around do_we_trust, so we can ask whether to use the
531
443
 * key anyway.
533
445
static int
534
446
do_we_trust_pre( PKT_public_key *pk, unsigned int trustlevel )
535
447
{
536
 
    int rc;
537
 
 
538
 
    rc = do_we_trust( pk, &trustlevel );
539
 
 
540
 
    if( (trustlevel & TRUST_FLAG_REVOKED) && !rc )
541
 
        return 0;
542
 
    if( (trustlevel & TRUST_FLAG_SUB_REVOKED) && !rc )
543
 
        return 0;
544
 
 
545
 
    if( !opt.batch && !rc ) {
546
 
        u32 keyid[2];
547
 
 
548
 
        keyid_from_pk( pk, keyid);
549
 
        tty_printf( "%4u%c/%08lX %s \"",
550
 
                  nbits_from_pk( pk ), pubkey_letter( pk->pubkey_algo ),
551
 
                  (ulong)keyid[1], datestr_from_pk( pk ) );
552
 
        /* If the pk was chosen by a particular user ID, this is the
553
 
           one to ask about. */
554
 
        if(pk->user_id)
555
 
          tty_print_utf8_string(pk->user_id->name,pk->user_id->len);
556
 
        else
557
 
          {
558
 
            size_t n;
559
 
            char *p = get_user_id( keyid, &n );
560
 
            tty_print_utf8_string( p, n );
561
 
            xfree (p);
562
 
          }
563
 
        tty_printf("\"\n");
564
 
        print_fingerprint (pk, NULL, 2);
565
 
        tty_printf("\n");
566
 
 
567
 
        tty_printf(_(
568
 
"It is NOT certain that the key belongs to the person named\n"
569
 
"in the user ID.  If you *really* know what you are doing,\n"
570
 
"you may answer the next question with yes\n\n"));
571
 
 
572
 
        if( cpr_get_answer_is_yes("untrusted_key.override",
573
 
                                  _("Use this key anyway? "))  )
574
 
            rc = 1;
575
 
 
576
 
        /* Hmmm: Should we set a flag to tell the user about
577
 
         *       his decision the next time he encrypts for this recipient?
578
 
         */
579
 
    }
580
 
    else if( opt.trust_model==TM_ALWAYS && !rc ) {
581
 
        if( !opt.quiet )
582
 
            log_info(_("WARNING: Using untrusted key!\n"));
 
448
  int rc;
 
449
 
 
450
  rc = do_we_trust( pk, trustlevel );
 
451
 
 
452
  if( !opt.batch && !rc )
 
453
    {
 
454
      print_pubkey_info(NULL,pk);
 
455
      print_fingerprint (pk, NULL, 2);
 
456
      tty_printf("\n");
 
457
 
 
458
      tty_printf(
 
459
               _("It is NOT certain that the key belongs to the person named\n"
 
460
                 "in the user ID.  If you *really* know what you are doing,\n"
 
461
                 "you may answer the next question with yes.\n"));
 
462
 
 
463
      tty_printf("\n");
 
464
 
 
465
      
 
466
      if (is_status_enabled ())
 
467
        {
 
468
          u32 kid[2];
 
469
          char *hint_str;
 
470
          
 
471
          keyid_from_pk (pk, kid);
 
472
          hint_str = get_long_user_id_string ( kid );
 
473
          write_status_text ( STATUS_USERID_HINT, hint_str );
 
474
          xfree (hint_str);
 
475
        }
 
476
 
 
477
      if( cpr_get_answer_is_yes("untrusted_key.override",
 
478
                                _("Use this key anyway? (y/N) "))  )
583
479
        rc = 1;
 
480
 
 
481
      /* Hmmm: Should we set a flag to tell the user about
 
482
       *         his decision the next time he encrypts for this recipient?
 
483
       */
584
484
    }
585
 
    return rc;
 
485
 
 
486
  return rc;
586
487
}
587
488
 
588
489
 
589
 
 
590
490
/****************
591
491
 * Check whether we can trust this signature.
592
492
 * Returns: Error if we shall not trust this signatures.
594
494
int
595
495
check_signatures_trust( PKT_signature *sig )
596
496
{
597
 
  PKT_public_key *pk = xcalloc (1, sizeof *pk );
 
497
  PKT_public_key *pk = xmalloc_clear( sizeof *pk );
598
498
  unsigned int trustlevel;
599
499
  int rc=0;
600
500
 
602
502
  if (rc) 
603
503
    { /* this should not happen */
604
504
      log_error("Ooops; the key vanished  - can't check the trust\n");
605
 
      rc = GPG_ERR_NO_PUBKEY;
 
505
      rc = G10ERR_NO_PUBKEY;
606
506
      goto leave;
607
507
    }
608
508
 
615
515
      goto leave;
616
516
    }
617
517
 
 
518
  if(pk->maybe_revoked && !pk->is_revoked)
 
519
    log_info(_("WARNING: this key might be revoked (revocation key"
 
520
               " not present)\n"));
 
521
 
618
522
  trustlevel = get_validity (pk, NULL);
619
523
 
620
524
  if ( (trustlevel & TRUST_FLAG_REVOKED) ) 
621
525
    {
622
526
      write_status( STATUS_KEYREVOKED );
623
 
      log_info(_("WARNING: This key has been revoked by its owner!\n"));
624
 
      log_info(_("         This could mean that the signature is forgery.\n"));
 
527
      if(pk->is_revoked==2)
 
528
        log_info(_("WARNING: This key has been revoked by its"
 
529
                   " designated revoker!\n"));
 
530
      else
 
531
        log_info(_("WARNING: This key has been revoked by its owner!\n"));
 
532
      log_info(_("         This could mean that the signature is forged.\n"));
625
533
      show_revocation_reason( pk, 0 );
626
534
    }
627
535
  else if ((trustlevel & TRUST_FLAG_SUB_REVOKED) ) 
634
542
  if ((trustlevel & TRUST_FLAG_DISABLED))
635
543
    log_info (_("Note: This key has been disabled.\n"));
636
544
 
 
545
  /* If we have PKA information adjust the trustlevel. */
 
546
  if (sig->pka_info && sig->pka_info->valid)
 
547
    {
 
548
      unsigned char fpr[MAX_FINGERPRINT_LEN];
 
549
      PKT_public_key *primary_pk;
 
550
      size_t fprlen;
 
551
      int okay;
 
552
 
 
553
 
 
554
      primary_pk = xmalloc_clear (sizeof *primary_pk);
 
555
      get_pubkey (primary_pk, pk->main_keyid);
 
556
      fingerprint_from_pk (primary_pk, fpr, &fprlen);
 
557
      free_public_key (primary_pk);
 
558
 
 
559
      if ( fprlen == 20 && !memcmp (sig->pka_info->fpr, fpr, 20) )
 
560
        {
 
561
          okay = 1;
 
562
          write_status_text (STATUS_PKA_TRUST_GOOD, sig->pka_info->email);
 
563
          log_info (_("Note: Verified signer's address is `%s'\n"),
 
564
                    sig->pka_info->email);
 
565
        }
 
566
      else
 
567
        {
 
568
          okay = 0;
 
569
          write_status_text (STATUS_PKA_TRUST_BAD, sig->pka_info->email);
 
570
          log_info (_("Note: Signer's address `%s' "
 
571
                      "does not match DNS entry\n"), sig->pka_info->email);
 
572
        }
 
573
 
 
574
      switch ( (trustlevel & TRUST_MASK) ) 
 
575
        {
 
576
        case TRUST_UNKNOWN: 
 
577
        case TRUST_UNDEFINED:
 
578
        case TRUST_MARGINAL:
 
579
          if (okay && opt.verify_options&VERIFY_PKA_TRUST_INCREASE)
 
580
            {
 
581
              trustlevel = ((trustlevel & ~TRUST_MASK) | TRUST_FULLY);
 
582
              log_info (_("trustlevel adjusted to FULL"
 
583
                          " due to valid PKA info\n"));
 
584
            }
 
585
          /* (fall through) */
 
586
        case TRUST_FULLY:
 
587
          if (!okay)
 
588
            {
 
589
              trustlevel = ((trustlevel & ~TRUST_MASK) | TRUST_NEVER);
 
590
              log_info (_("trustlevel adjusted to NEVER"
 
591
                          " due to bad PKA info\n"));
 
592
            }
 
593
          break;
 
594
        }
 
595
    }
 
596
 
 
597
  /* Now let the user know what up with the trustlevel. */
637
598
  switch ( (trustlevel & TRUST_MASK) ) 
638
599
    {
639
600
    case TRUST_EXPIRED:
701
662
    for( ; pk_list; pk_list = pk_rover ) {
702
663
        pk_rover = pk_list->next;
703
664
        free_public_key( pk_list->pk );
704
 
        xfree ( pk_list );
 
665
        xfree( pk_list );
705
666
    }
706
667
}
707
668
 
730
691
    int i;
731
692
 
732
693
    if( opt.def_recipient )
733
 
        return xstrdup ( opt.def_recipient );
 
694
        return xstrdup( opt.def_recipient );
734
695
    if( !opt.def_recipient_self )
735
696
        return NULL;
736
 
    sk = xcalloc (1, sizeof *sk );
 
697
    sk = xmalloc_clear( sizeof *sk );
737
698
    i = get_seckey_byname( sk, NULL, 0 );
738
699
    if( i ) {
739
700
        free_secret_key( sk );
742
703
    n = MAX_FINGERPRINT_LEN;
743
704
    fingerprint_from_sk( sk, fpr, &n );
744
705
    free_secret_key( sk );
745
 
    p = xmalloc ( 2*n+3 );
 
706
    p = xmalloc( 2*n+3 );
746
707
    *p++ = '0';
747
708
    *p++ = 'x';
748
709
    for(i=0; i < n; i++ )
752
713
}
753
714
 
754
715
static int
755
 
expand_id(const char *id,STRLIST *into,unsigned int flags)
 
716
expand_id(const char *id,strlist_t *into,unsigned int flags)
756
717
{
757
718
  struct groupitem *groups;
758
719
  int count=0;
762
723
      /* need strcasecmp() here, as this should be localized */
763
724
      if(strcasecmp(groups->name,id)==0)
764
725
        {
765
 
          STRLIST each,sl;
 
726
          strlist_t each,sl;
766
727
 
767
728
          /* this maintains the current utf8-ness */
768
729
          for(each=groups->values;each;each=each->next)
781
742
 
782
743
/* For simplicity, and to avoid potential loops, we only expand once -
783
744
   you can't make an alias that points to an alias. */
784
 
static STRLIST
785
 
expand_group(STRLIST input)
 
745
static strlist_t
 
746
expand_group(strlist_t input)
786
747
{
787
 
  STRLIST sl,output=NULL,rover;
 
748
  strlist_t sl,output=NULL,rover;
788
749
 
789
750
  for(rover=input;rover;rover=rover->next)
790
751
    if(expand_id(rover->d,&output,rover->flags)==0)
797
758
  return output;
798
759
}
799
760
 
 
761
 
 
762
/* This is the central function to collect the keys for recipients.
 
763
   It is thus used to prepare a public key encryption. encrypt-to
 
764
   keys, default keys and the keys for the actual recipients are all
 
765
   collected here.  When not in batch mode and no recipient has been
 
766
   passed on the commandline, the function will also ask for
 
767
   recipients.
 
768
 
 
769
   RCPTS is a string list with the recipients; NULL is an allowed
 
770
   value but not very useful.  Group expansion is done on these names;
 
771
   they may be in any of the user Id formats we can handle.  The flags
 
772
   bits for each string in the string list are used for:
 
773
     Bit 0: This is an encrypt-to recipient.
 
774
     Bit 1: This is a hidden recipient.
 
775
 
 
776
   USE is the desired use for the key - usually PUBKEY_USAGE_ENC.
 
777
   RET_PK_LIST.
 
778
 
 
779
   On success a list of keys is stored at the address RET_PK_LIST; the
 
780
   caller must free this list.  On error the value at this address is
 
781
   not changed.
 
782
 */
800
783
int
801
 
build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
 
784
build_pk_list( strlist_t rcpts, PK_LIST *ret_pk_list, unsigned int use )
802
785
{
803
 
    PK_LIST pk_list = NULL;
804
 
    PKT_public_key *pk=NULL;
805
 
    int rc=0;
806
 
    int any_recipients=0;
807
 
    STRLIST rov,remusr;
808
 
    char *def_rec = NULL;
809
 
 
810
 
    if(opt.grouplist)
811
 
      remusr=expand_group(rcpts);
812
 
    else
813
 
      remusr=rcpts;
814
 
 
815
 
    /* check whether there are any recipients in the list and build the
816
 
     * list of the encrypt-to ones (we always trust them) */
817
 
    for( rov = remusr; rov; rov = rov->next ) {
818
 
        if( !(rov->flags & 1) )
819
 
          {
820
 
            any_recipients = 1;
821
 
 
822
 
            if((rov->flags&2) && (PGP2 || PGP6 || PGP7 || PGP8))
823
 
              {
824
 
                log_info(_("you may not use %s while in %s mode\n"),
825
 
                         "--hidden-recipient",
826
 
                         compliance_option_string());
827
 
 
828
 
                compliance_failure();
829
 
              }
830
 
          }
831
 
        else if( (use & PUBKEY_USAGE_ENC) && !opt.no_encrypt_to ) {
832
 
            pk = xcalloc (1, sizeof *pk );
833
 
            pk->req_usage = use;
834
 
            /* We can encrypt-to a disabled key */
835
 
            if( (rc = get_pubkey_byname( pk, rov->d, NULL, NULL, 1 )) ) {
836
 
                free_public_key( pk ); pk = NULL;
837
 
                log_error(_("%s: skipped: %s\n"), rov->d, gpg_strerror (rc) );
838
 
                write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
839
 
                                              rov->d, strlen (rov->d), -1);
840
 
                goto fail;
841
 
            }
842
 
            else if( !(rc=openpgp_pk_test_algo (pk->pubkey_algo, use )) ) {
843
 
                /* Skip the actual key if the key is already present
844
 
                 * in the list */
845
 
                if (key_present_in_pk_list(pk_list, pk) == 0) {
846
 
                    free_public_key(pk); pk = NULL;
847
 
                    log_info(_("%s: skipped: public key already present\n"),
848
 
                                                            rov->d);
849
 
                }
850
 
                else {
851
 
                    PK_LIST r;
852
 
                    r = xmalloc ( sizeof *r );
853
 
                    r->pk = pk; pk = NULL;
854
 
                    r->next = pk_list;
855
 
                    r->flags = (rov->flags&2)?1:0;
856
 
                    pk_list = r;
857
 
 
858
 
                    if(r->flags&1 && (PGP2 || PGP6 || PGP7 || PGP8))
859
 
                      {
860
 
                        log_info(_("you may not use %s while in %s mode\n"),
861
 
                                 "--hidden-encrypt-to",
862
 
                                 compliance_option_string());
863
 
 
864
 
                        compliance_failure();
865
 
                      }
866
 
                }
867
 
            }
868
 
            else {
869
 
                free_public_key( pk ); pk = NULL;
870
 
                log_error(_("%s: skipped: %s\n"), rov->d, gpg_strerror (rc) );
871
 
                write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
872
 
                                              rov->d, strlen (rov->d), -1);
873
 
                goto fail;
874
 
            }
875
 
        }
 
786
  PK_LIST pk_list = NULL;
 
787
  PKT_public_key *pk=NULL;
 
788
  int rc=0;
 
789
  int any_recipients=0;
 
790
  strlist_t rov,remusr;
 
791
  char *def_rec = NULL;
 
792
 
 
793
  /* Try to expand groups if any have been defined. */
 
794
  if (opt.grouplist)
 
795
    remusr = expand_group (rcpts);
 
796
  else
 
797
    remusr = rcpts;
 
798
 
 
799
  /* Check whether there are any recipients in the list and build the
 
800
   * list of the encrypt-to ones (we always trust them). */
 
801
  for ( rov = remusr; rov; rov = rov->next ) 
 
802
    {
 
803
      if ( !(rov->flags & 1) )
 
804
        {
 
805
          /* This is a regular recipient; i.e. not an encrypt-to
 
806
             one. */
 
807
          any_recipients = 1;
 
808
 
 
809
          /* Hidden recipients are not allowed while in PGP mode,
 
810
             issue a warning and switch into GnuPG mode. */
 
811
          if ((rov->flags&2) && (PGP2 || PGP6 || PGP7 || PGP8))
 
812
            {
 
813
              log_info(_("you may not use %s while in %s mode\n"),
 
814
                       "--hidden-recipient",
 
815
                       compliance_option_string());
 
816
 
 
817
              compliance_failure();
 
818
            }
 
819
        }
 
820
      else if ( (use & PUBKEY_USAGE_ENC) && !opt.no_encrypt_to ) 
 
821
        {
 
822
          /* Encryption has been requested and --encrypt-to has not
 
823
             been disabled.  Check this encrypt-to key. */
 
824
          pk = xmalloc_clear( sizeof *pk );
 
825
          pk->req_usage = use;
 
826
 
 
827
          /* We explicitly allow encrypt-to to an disabled key; thus
 
828
             we pass 1 as last argument. */
 
829
          if ( (rc = get_pubkey_byname ( pk, rov->d, NULL, NULL, 1 )) ) 
 
830
            {
 
831
              free_public_key ( pk ); pk = NULL;
 
832
              log_error (_("%s: skipped: %s\n"), rov->d, g10_errstr(rc) );
 
833
              write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
 
834
                                            rov->d, strlen (rov->d), -1);
 
835
              goto fail;
 
836
            }
 
837
          else if ( !(rc=openpgp_pk_test_algo2 (pk->pubkey_algo, use)) ) 
 
838
            {
 
839
              /* Skip the actual key if the key is already present
 
840
               * in the list.  Add it to our list if not. */
 
841
              if (key_present_in_pk_list(pk_list, pk) == 0)
 
842
                {
 
843
                  free_public_key (pk); pk = NULL;
 
844
                  log_info (_("%s: skipped: public key already present\n"),
 
845
                            rov->d);
 
846
                }
 
847
              else
 
848
                {
 
849
                  PK_LIST r;
 
850
                  r = xmalloc( sizeof *r );
 
851
                  r->pk = pk; pk = NULL;
 
852
                  r->next = pk_list;
 
853
                  r->flags = (rov->flags&2)?1:0;
 
854
                  pk_list = r;
 
855
 
 
856
                  /* Hidden encrypt-to recipients are not allowed while
 
857
                     in PGP mode, issue a warning and switch into
 
858
                     GnuPG mode. */
 
859
                  if ((r->flags&1) && (PGP2 || PGP6 || PGP7 || PGP8))
 
860
                    {
 
861
                      log_info(_("you may not use %s while in %s mode\n"),
 
862
                               "--hidden-encrypt-to",
 
863
                               compliance_option_string());
 
864
 
 
865
                      compliance_failure();
 
866
                    }
 
867
                }
 
868
            }
 
869
          else 
 
870
            {
 
871
              /* The public key is not usable for encryption or not
 
872
                 available. */
 
873
              free_public_key( pk ); pk = NULL;
 
874
              log_error(_("%s: skipped: %s\n"), rov->d, g10_errstr(rc) );
 
875
              write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
 
876
                                            rov->d, strlen (rov->d), -1);
 
877
              goto fail;
 
878
            }
 
879
        }
876
880
    }
877
881
 
878
 
    if( !any_recipients && !opt.batch ) { /* ask */
879
 
        int have_def_rec;
880
 
        char *answer=NULL;
881
 
        STRLIST backlog=NULL;
882
 
 
883
 
        def_rec = default_recipient();
884
 
        have_def_rec = !!def_rec;
885
 
        if( !have_def_rec )
886
 
            tty_printf(_(
887
 
                "You did not specify a user ID. (you may use \"-r\")\n"));
888
 
        for(;;) {
889
 
            rc = 0;
890
 
            xfree (answer);
891
 
            if( have_def_rec ) {
892
 
                answer = def_rec;
893
 
                def_rec = NULL;
894
 
            }
895
 
            else if (backlog) {
896
 
              answer = strlist_pop (&backlog);
897
 
            }
898
 
            else {
899
 
                answer = cpr_get_utf8("pklist.user_id.enter",
900
 
                         _("\nEnter the user ID.  End with an empty line: "));
901
 
                trim_spaces(answer);
902
 
                cpr_kill_prompt();
903
 
            }
904
 
            if( !answer || !*answer ) {
905
 
                xfree (answer);
906
 
                break;
907
 
            }
908
 
            if(expand_id(answer,&backlog,0))
909
 
              continue;
910
 
            if( pk )
911
 
                free_public_key( pk );
912
 
            pk = xcalloc (1, sizeof *pk );
913
 
            pk->req_usage = use;
914
 
            rc = get_pubkey_byname( pk, answer, NULL, NULL, 0 );
915
 
            if( rc )
916
 
                tty_printf(_("No such user ID.\n"));
917
 
            else if( !(rc=openpgp_pk_test_algo (pk->pubkey_algo, use)) ) {
918
 
                if( have_def_rec ) {
919
 
                    if (key_present_in_pk_list(pk_list, pk) == 0) {
920
 
                        free_public_key(pk); pk = NULL;
921
 
                        log_info(_("skipped: public key "
922
 
                                   "already set as default recipient\n") );
923
 
                    }
924
 
                    else {
925
 
                        PK_LIST r = xmalloc ( sizeof *r );
926
 
                        r->pk = pk; pk = NULL;
927
 
                        r->next = pk_list;
928
 
                        r->flags = 0; /* no throwing default ids */
929
 
                        pk_list = r;
930
 
                    }
931
 
                    any_recipients = 1;
932
 
                    continue;
933
 
                }
934
 
                else {
935
 
                    int trustlevel;
 
882
  /* If we don't have any recipients yet and we are not in batch mode
 
883
     drop into interactive selection mode. */
 
884
  if ( !any_recipients && !opt.batch ) 
 
885
    { 
 
886
      int have_def_rec;
 
887
      char *answer = NULL;
 
888
      strlist_t backlog = NULL;
 
889
 
 
890
      if (pk_list)
 
891
        any_recipients = 1;
 
892
      def_rec = default_recipient();
 
893
      have_def_rec = !!def_rec;
 
894
      if ( !have_def_rec )
 
895
        tty_printf(_("You did not specify a user ID. (you may use \"-r\")\n"));
 
896
 
 
897
      for (;;) 
 
898
        {
 
899
          rc = 0;
 
900
          xfree(answer);
 
901
          if ( have_def_rec )
 
902
            {
 
903
              /* A default recipient is taken as the first entry. */
 
904
              answer = def_rec;
 
905
              def_rec = NULL;
 
906
            }
 
907
          else if (backlog) 
 
908
            {
 
909
              /* This is part of our trick to expand and display groups. */
 
910
              answer = strlist_pop (&backlog);
 
911
            }
 
912
          else
 
913
            {
 
914
              /* Show the list of already collected recipients and ask
 
915
                 for more. */
 
916
              PK_LIST iter;
 
917
 
 
918
              tty_printf("\n");
 
919
              tty_printf(_("Current recipients:\n"));
 
920
              for (iter=pk_list;iter;iter=iter->next)
 
921
                {
 
922
                  u32 keyid[2];
 
923
 
 
924
                  keyid_from_pk(iter->pk,keyid);
 
925
                  tty_printf("%4u%c/%s %s \"",
 
926
                             nbits_from_pk(iter->pk),
 
927
                             pubkey_letter(iter->pk->pubkey_algo),
 
928
                             keystr(keyid),
 
929
                             datestr_from_pk(iter->pk));
 
930
 
 
931
                  if (iter->pk->user_id)
 
932
                    tty_print_utf8_string(iter->pk->user_id->name,
 
933
                                          iter->pk->user_id->len);
 
934
                  else
 
935
                    {
 
936
                      size_t n;
 
937
                      char *p = get_user_id( keyid, &n );
 
938
                      tty_print_utf8_string( p, n );
 
939
                      xfree(p);
 
940
                    }
 
941
                  tty_printf("\"\n");
 
942
                }
 
943
 
 
944
              answer = cpr_get_utf8("pklist.user_id.enter",
 
945
                                    _("\nEnter the user ID.  "
 
946
                                      "End with an empty line: "));
 
947
              trim_spaces(answer);
 
948
              cpr_kill_prompt();
 
949
            }
 
950
          
 
951
          if ( !answer || !*answer ) 
 
952
            {
 
953
              xfree(answer);
 
954
              break;  /* No more recipients entered - get out of loop. */
 
955
            }
 
956
 
 
957
          /* Do group expand here too.  The trick here is to continue
 
958
             the loop if any expansion occured.  The code above will
 
959
             then list all expanded keys. */
 
960
          if (expand_id(answer,&backlog,0))
 
961
            continue;
 
962
 
 
963
          /* Get and check key for the current name. */
 
964
          if (pk)
 
965
            free_public_key (pk);
 
966
          pk = xmalloc_clear( sizeof *pk );
 
967
          pk->req_usage = use;
 
968
          rc = get_pubkey_byname( pk, answer, NULL, NULL, 0 );
 
969
          if (rc)
 
970
            tty_printf(_("No such user ID.\n"));
 
971
          else if ( !(rc=openpgp_pk_test_algo2 (pk->pubkey_algo, use)) ) 
 
972
            {
 
973
              if ( have_def_rec )
 
974
                {
 
975
                  /* No validation for a default recipient. */
 
976
                  if (!key_present_in_pk_list(pk_list, pk)) 
 
977
                    {
 
978
                      free_public_key (pk); pk = NULL;
 
979
                      log_info (_("skipped: public key "
 
980
                                  "already set as default recipient\n") );
 
981
                    }
 
982
                  else
 
983
                    {
 
984
                      PK_LIST r = xmalloc (sizeof *r);
 
985
                      r->pk = pk; pk = NULL;
 
986
                      r->next = pk_list;
 
987
                      r->flags = 0; /* No throwing default ids. */
 
988
                      pk_list = r;
 
989
                    }
 
990
                  any_recipients = 1;
 
991
                  continue;
 
992
                }
 
993
              else
 
994
                { /* Check validity of this key. */
 
995
                  int trustlevel;
936
996
                    
937
 
                    trustlevel = get_validity (pk, pk->user_id);
938
 
                    if( (trustlevel & TRUST_FLAG_DISABLED) ) {
939
 
                        tty_printf(_("Public key is disabled.\n") );
940
 
                    }
941
 
                    else if( do_we_trust_pre( pk, trustlevel ) ) {
942
 
                        /* Skip the actual key if the key is already present
943
 
                         * in the list */
944
 
                        if (key_present_in_pk_list(pk_list, pk) == 0) {
945
 
                            free_public_key(pk); pk = NULL;
946
 
                            log_info(_("skipped: public key already set\n") );
947
 
                        }
948
 
                        else {
949
 
                            PK_LIST r;
950
 
                            u32 keyid[2];
951
 
 
952
 
                            keyid_from_pk( pk, keyid);
953
 
                            tty_printf("Added %4u%c/%08lX %s \"",
954
 
                                       nbits_from_pk( pk ),
955
 
                                       pubkey_letter( pk->pubkey_algo ),
956
 
                                       (ulong)keyid[1],
957
 
                                       datestr_from_pk( pk ) );
958
 
                            if(pk->user_id)
959
 
                              tty_print_utf8_string(pk->user_id->name,
960
 
                                                    pk->user_id->len);
961
 
                            else
962
 
                              {
963
 
                                size_t n;
964
 
                                char *p = get_user_id( keyid, &n );
965
 
                                tty_print_utf8_string( p, n );
966
 
                                xfree (p);
967
 
                              }
968
 
                            tty_printf("\"\n");
969
 
 
970
 
                            r = xmalloc ( sizeof *r );
971
 
                            r->pk = pk; pk = NULL;
972
 
                            r->next = pk_list;
973
 
                            r->flags = 0; /* no throwing interactive ids */
974
 
                            pk_list = r;
975
 
                        }
976
 
                        any_recipients = 1;
977
 
                        continue;
978
 
                    }
979
 
                }
980
 
            }
981
 
            xfree (def_rec); def_rec = NULL;
982
 
            have_def_rec = 0;
983
 
        }
984
 
        if( pk ) {
985
 
            free_public_key( pk );
986
 
            pk = NULL;
987
 
        }
 
997
                  trustlevel = get_validity (pk, pk->user_id);
 
998
                  if ( (trustlevel & TRUST_FLAG_DISABLED) ) 
 
999
                    {
 
1000
                      tty_printf (_("Public key is disabled.\n") );
 
1001
                    }
 
1002
                  else if ( do_we_trust_pre (pk, trustlevel) ) 
 
1003
                    {
 
1004
                      /* Skip the actual key if the key is already
 
1005
                       * present in the list */
 
1006
                      if (!key_present_in_pk_list(pk_list, pk))
 
1007
                        {
 
1008
                          free_public_key(pk); pk = NULL;
 
1009
                          log_info(_("skipped: public key already set\n") );
 
1010
                        }
 
1011
                      else
 
1012
                        {
 
1013
                          PK_LIST r;
 
1014
                          r = xmalloc( sizeof *r );
 
1015
                          r->pk = pk; pk = NULL;
 
1016
                          r->next = pk_list;
 
1017
                          r->flags = 0; /* No throwing interactive ids. */
 
1018
                          pk_list = r;
 
1019
                        }
 
1020
                      any_recipients = 1;
 
1021
                      continue;
 
1022
                    }
 
1023
                }
 
1024
            }
 
1025
          xfree(def_rec); def_rec = NULL;
 
1026
          have_def_rec = 0;
 
1027
        }
 
1028
      if ( pk )
 
1029
        {
 
1030
          free_public_key( pk );
 
1031
          pk = NULL;
 
1032
        }
988
1033
    }
989
 
    else if( !any_recipients && (def_rec = default_recipient()) ) {
990
 
        pk = xcalloc (1, sizeof *pk );
991
 
        pk->req_usage = use;
992
 
        /* The default recipient may be disabled */
993
 
        rc = get_pubkey_byname( pk, def_rec, NULL, NULL, 1 );
994
 
        if( rc )
995
 
            log_error(_("unknown default recipient `%s'\n"), def_rec );
996
 
        else if( !(rc=openpgp_pk_test_algo (pk->pubkey_algo, use)) ) {
997
 
          /* Mark any_recipients here since the default recipient
 
1034
  else if ( !any_recipients && (def_rec = default_recipient()) ) 
 
1035
    {
 
1036
      /* We are in batch mode and have only a default recipient. */
 
1037
      pk = xmalloc_clear( sizeof *pk );
 
1038
      pk->req_usage = use;
 
1039
 
 
1040
      /* The default recipient is allowed to be disabled; thus pass 1
 
1041
         as last argument. */
 
1042
      rc = get_pubkey_byname (pk, def_rec, NULL, NULL, 1);
 
1043
      if (rc)
 
1044
        log_error(_("unknown default recipient \"%s\"\n"), def_rec );
 
1045
      else if ( !(rc=openpgp_pk_test_algo2(pk->pubkey_algo, use)) ) 
 
1046
        {
 
1047
          /* Mark any_recipients here since the default recipient
998
1048
             would have been used if it wasn't already there.  It
999
1049
             doesn't really matter if we got this key from the default
1000
1050
             recipient or an encrypt-to. */
1001
 
          any_recipients = 1;
1002
 
          if (key_present_in_pk_list(pk_list, pk) == 0)
1003
 
            log_info(_("skipped: public key already set as default recipient\n"));
1004
 
          else {
1005
 
            PK_LIST r = xmalloc ( sizeof *r );
1006
 
            r->pk = pk; pk = NULL;
1007
 
            r->next = pk_list;
1008
 
            r->flags = 0; /* no throwing default ids */
1009
 
            pk_list = r;
1010
 
          }
1011
 
        }
1012
 
        if( pk ) {
1013
 
            free_public_key( pk );
1014
 
            pk = NULL;
1015
 
        }
1016
 
        xfree (def_rec); def_rec = NULL;
1017
 
    }
1018
 
    else {
1019
 
        any_recipients = 0;
1020
 
        for(; remusr; remusr = remusr->next ) {
1021
 
            if( (remusr->flags & 1) )
1022
 
                continue; /* encrypt-to keys are already handled */
1023
 
 
1024
 
            pk = xcalloc (1, sizeof *pk );
1025
 
            pk->req_usage = use;
1026
 
            if( (rc = get_pubkey_byname( pk, remusr->d, NULL, NULL, 0 )) ) {
1027
 
                free_public_key( pk ); pk = NULL;
1028
 
                log_error(_("%s: skipped: %s\n"), remusr->d, gpg_strerror (rc) );
1029
 
                write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
1030
 
                                              remusr->d, strlen (remusr->d),
1031
 
                                              -1);
1032
 
                goto fail;
1033
 
            }
1034
 
            else if( !(rc=openpgp_pk_test_algo (pk->pubkey_algo, use )) ) {
1035
 
                int trustlevel;
1036
 
 
1037
 
                trustlevel = get_validity (pk, pk->user_id);
1038
 
                if( (trustlevel & TRUST_FLAG_DISABLED) ) {
1039
 
                    free_public_key(pk); pk = NULL;
1040
 
                    log_info(_("%s: skipped: public key is disabled\n"),
1041
 
                                                                    remusr->d);
1042
 
                    write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
1043
 
                                                  remusr->d,
1044
 
                                                  strlen (remusr->d),
1045
 
                                                  -1);
1046
 
                    rc = gpg_error (GPG_ERR_UNUSABLE_PUBKEY);
1047
 
                    goto fail;
1048
 
                }
1049
 
                else if( do_we_trust_pre( pk, trustlevel ) ) {
1050
 
                    /* note: do_we_trust may have changed the trustlevel */
1051
 
 
1052
 
                    /* We have at least one valid recipient. It doesn't matters
1053
 
                     * if this recipient is already present. */
1054
 
                    any_recipients = 1;
1055
 
 
1056
 
                    /* Skip the actual key if the key is already present
1057
 
                     * in the list */
1058
 
                    if (key_present_in_pk_list(pk_list, pk) == 0) {
1059
 
                        free_public_key(pk); pk = NULL;
1060
 
                        log_info(_("%s: skipped: public key already present\n"),
1061
 
                                                                    remusr->d);
1062
 
                    }
1063
 
                    else {
1064
 
                        PK_LIST r;
1065
 
                        r = xmalloc ( sizeof *r );
1066
 
                        r->pk = pk; pk = NULL;
1067
 
                        r->next = pk_list;
1068
 
                        r->flags = (remusr->flags&2)?1:0;
1069
 
                        pk_list = r;
1070
 
                    }
1071
 
                }
1072
 
                else { /* we don't trust this pk */
1073
 
                    free_public_key( pk ); pk = NULL;
1074
 
                    write_status_text_and_buffer (STATUS_INV_RECP, "10 ",
1075
 
                                                  remusr->d,
1076
 
                                                  strlen (remusr->d),
1077
 
                                                  -1);
1078
 
                    rc = gpg_error (GPG_ERR_UNUSABLE_PUBKEY);
1079
 
                    goto fail;
1080
 
                }
1081
 
            }
1082
 
            else {
1083
 
                free_public_key( pk ); pk = NULL;
1084
 
                write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
1085
 
                                              remusr->d,
1086
 
                                              strlen (remusr->d),
1087
 
                                              -1);
1088
 
                log_error(_("%s: skipped: %s\n"), remusr->d, gpg_strerror (rc) );
1089
 
                goto fail;
1090
 
            }
1091
 
        }
1092
 
    }
1093
 
 
1094
 
    if( !rc && !any_recipients ) {
1095
 
        log_error(_("no valid addressees\n"));
1096
 
        write_status_text (STATUS_NO_RECP, "0");
1097
 
        rc = GPG_ERR_NO_USER_ID;
1098
 
    }
1099
 
 
 
1051
          any_recipients = 1;
 
1052
          if (!key_present_in_pk_list(pk_list, pk))
 
1053
            log_info (_("skipped: public key already set "
 
1054
                        "as default recipient\n"));
 
1055
          else 
 
1056
            {
 
1057
              PK_LIST r = xmalloc( sizeof *r );
 
1058
              r->pk = pk; pk = NULL;
 
1059
              r->next = pk_list;
 
1060
              r->flags = 0; /* No throwing default ids. */
 
1061
              pk_list = r;
 
1062
            }
 
1063
        }
 
1064
      if ( pk )
 
1065
        {
 
1066
          free_public_key( pk );
 
1067
          pk = NULL;
 
1068
        }
 
1069
      xfree(def_rec); def_rec = NULL;
 
1070
    }
 
1071
  else 
 
1072
    {
 
1073
      /* General case: Check all keys. */
 
1074
      any_recipients = 0;
 
1075
      for (; remusr; remusr = remusr->next ) 
 
1076
        {
 
1077
          if ( (remusr->flags & 1) )
 
1078
            continue; /* encrypt-to keys are already handled. */
 
1079
 
 
1080
          pk = xmalloc_clear( sizeof *pk );
 
1081
          pk->req_usage = use;
 
1082
          if ( (rc = get_pubkey_byname( pk, remusr->d, NULL, NULL, 0 )) ) 
 
1083
            {
 
1084
              /* Key not found or other error. */
 
1085
              free_public_key( pk ); pk = NULL;
 
1086
              log_error(_("%s: skipped: %s\n"), remusr->d, g10_errstr(rc) );
 
1087
              write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
 
1088
                                            remusr->d, strlen (remusr->d),
 
1089
                                            -1);
 
1090
              goto fail;
 
1091
            }
 
1092
          else if ( !(rc=openpgp_pk_test_algo2(pk->pubkey_algo, use )) ) 
 
1093
            {
 
1094
              /* Key found and usable.  Check validity. */
 
1095
              int trustlevel;
 
1096
              
 
1097
              trustlevel = get_validity (pk, pk->user_id);
 
1098
              if ( (trustlevel & TRUST_FLAG_DISABLED) ) 
 
1099
                {
 
1100
                  /*Key has been disabled. */
 
1101
                  free_public_key(pk); pk = NULL;
 
1102
                  log_info(_("%s: skipped: public key is disabled\n"),
 
1103
                           remusr->d);
 
1104
                  write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
 
1105
                                                remusr->d,
 
1106
                                                strlen (remusr->d),
 
1107
                                                -1);
 
1108
                  rc=G10ERR_UNU_PUBKEY;
 
1109
                  goto fail;
 
1110
                }
 
1111
              else if ( do_we_trust_pre( pk, trustlevel ) ) 
 
1112
                {
 
1113
                  /* Note: do_we_trust may have changed the trustlevel */
 
1114
 
 
1115
                  /* We have at least one valid recipient. It doesn't
 
1116
                   * matters if this recipient is already present. */
 
1117
                  any_recipients = 1;
 
1118
 
 
1119
                  /* Skip the actual key if the key is already present
 
1120
                   * in the list */
 
1121
                  if (!key_present_in_pk_list(pk_list, pk)) 
 
1122
                    {
 
1123
                      free_public_key(pk); pk = NULL;
 
1124
                      log_info(_("%s: skipped: public key already present\n"),
 
1125
                               remusr->d);
 
1126
                    }
 
1127
                  else
 
1128
                    {
 
1129
                      PK_LIST r;
 
1130
                      r = xmalloc( sizeof *r );
 
1131
                      r->pk = pk; pk = NULL;
 
1132
                      r->next = pk_list;
 
1133
                      r->flags = (remusr->flags&2)?1:0;
 
1134
                      pk_list = r;
 
1135
                    }
 
1136
                }
 
1137
              else
 
1138
                { /* We don't trust this key. */
 
1139
                  free_public_key( pk ); pk = NULL;
 
1140
                  write_status_text_and_buffer (STATUS_INV_RECP, "10 ",
 
1141
                                                remusr->d,
 
1142
                                                strlen (remusr->d),
 
1143
                                                -1);
 
1144
                  rc=G10ERR_UNU_PUBKEY;
 
1145
                  goto fail;
 
1146
                }
 
1147
            }
 
1148
          else
 
1149
            {
 
1150
              /* Key found but not usable for us (e.g. sign-only key). */
 
1151
              free_public_key( pk ); pk = NULL;
 
1152
              write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
 
1153
                                            remusr->d,
 
1154
                                            strlen (remusr->d),
 
1155
                                            -1);
 
1156
              log_error(_("%s: skipped: %s\n"), remusr->d, g10_errstr(rc) );
 
1157
              goto fail;
 
1158
            }
 
1159
        }
 
1160
    }
 
1161
  
 
1162
  if ( !rc && !any_recipients ) 
 
1163
    {
 
1164
      log_error(_("no valid addressees\n"));
 
1165
      write_status_text (STATUS_NO_RECP, "0");
 
1166
      rc = G10ERR_NO_USER_ID;
 
1167
    }
 
1168
  
1100
1169
 fail:
1101
1170
 
1102
 
    if( rc )
1103
 
        release_pk_list( pk_list );
1104
 
    else
1105
 
        *ret_pk_list = pk_list;
1106
 
    if(opt.grouplist)
1107
 
      free_strlist(remusr);
1108
 
    return rc;
 
1171
  if ( rc )
 
1172
    release_pk_list( pk_list );
 
1173
  else
 
1174
    *ret_pk_list = pk_list;
 
1175
  if (opt.grouplist)
 
1176
    free_strlist(remusr);
 
1177
  return rc;
1109
1178
}
1110
1179
 
1111
1180
 
1127
1196
   preference list, so I'm including it. -dms */
1128
1197
 
1129
1198
int
1130
 
algo_available( preftype_t preftype, int algo, void *hint )
 
1199
algo_available( preftype_t preftype, int algo, const union pref_hint *hint)
1131
1200
{
1132
1201
  if( preftype == PREFTYPE_SYM )
1133
1202
    {
1136
1205
                  && algo != CIPHER_ALGO_CAST5))
1137
1206
        return 0;
1138
1207
      
1139
 
      if((PGP7 || PGP8) && (algo != CIPHER_ALGO_IDEA
1140
 
                            && algo != CIPHER_ALGO_3DES
1141
 
                            && algo != CIPHER_ALGO_CAST5
1142
 
                            && algo != CIPHER_ALGO_AES
1143
 
                            && algo != CIPHER_ALGO_AES192
1144
 
                            && algo != CIPHER_ALGO_AES256
1145
 
                            && algo != CIPHER_ALGO_TWOFISH))
 
1208
      if(PGP7 && (algo != CIPHER_ALGO_IDEA
 
1209
                  && algo != CIPHER_ALGO_3DES
 
1210
                  && algo != CIPHER_ALGO_CAST5
 
1211
                  && algo != CIPHER_ALGO_AES
 
1212
                  && algo != CIPHER_ALGO_AES192
 
1213
                  && algo != CIPHER_ALGO_AES256
 
1214
                  && algo != CIPHER_ALGO_TWOFISH))
1146
1215
        return 0;
1147
1216
 
1148
 
      return algo && !gcry_cipher_test_algo (algo);
 
1217
      /* PGP8 supports all the ciphers we do.. */
 
1218
 
 
1219
      return algo && !openpgp_cipher_test_algo ( algo );
1149
1220
    }
1150
1221
  else if( preftype == PREFTYPE_HASH )
1151
1222
    {
1152
 
      if(hint && ((*(int *)hint) != gcry_md_get_algo_dlen (algo)))
1153
 
        return 0;
 
1223
      if (hint && hint->digest_length)
 
1224
        {
 
1225
          if (hint->digest_length!=20 || opt.flags.dsa2)
 
1226
            {
 
1227
              /* If --enable-dsa2 is set or the hash isn't 160 bits
 
1228
                 (which implies DSA2), then we'll accept a hash that
 
1229
                 is larger than we need.  Otherwise we won't accept
 
1230
                 any hash that isn't exactly the right size. */
 
1231
              if (hint->digest_length > gcry_md_get_algo_dlen (algo))
 
1232
                return 0;
 
1233
            }
 
1234
          else if (hint->digest_length != gcry_md_get_algo_dlen (algo))
 
1235
            return 0;
 
1236
        }
1154
1237
 
1155
1238
      if((PGP6 || PGP7) && (algo != DIGEST_ALGO_MD5
1156
1239
                            && algo != DIGEST_ALGO_SHA1
1164
1247
                  && algo != DIGEST_ALGO_SHA256))
1165
1248
        return 0;
1166
1249
 
1167
 
      return algo && !gcry_md_test_algo( algo );
 
1250
      return algo && !openpgp_md_test_algo (algo);
1168
1251
    }
1169
1252
  else if( preftype == PREFTYPE_ZIP )
1170
1253
    {
1171
 
      if((PGP6 || PGP7 || PGP8) && (algo != COMPRESS_ALGO_NONE
1172
 
                                    && algo != COMPRESS_ALGO_ZIP))
 
1254
      if((PGP6 || PGP7) && (algo != COMPRESS_ALGO_NONE
 
1255
                            && algo != COMPRESS_ALGO_ZIP))
1173
1256
        return 0;
1174
1257
 
 
1258
      /* PGP8 supports all the compression algos we do */
 
1259
 
1175
1260
      return !check_compress_algo( algo );
1176
1261
    }
1177
1262
  else
1184
1269
 * Return -1 if we could not find an algorithm.
1185
1270
 */
1186
1271
int
1187
 
select_algo_from_prefs(PK_LIST pk_list, int preftype, int request, void *hint)
 
1272
select_algo_from_prefs(PK_LIST pk_list, int preftype, int request,
 
1273
                       const union pref_hint *hint)
1188
1274
{
1189
1275
    PK_LIST pkr;
1190
1276
    u32 bits[8];
1252
1338
        }
1253
1339
 
1254
1340
#if 0
1255
 
        log_debug("pref mask=%08lX%08lX%08lX%08lX%08lX%08lX%08lX%08lX\n",
 
1341
        log_debug("pref mask=%08lX%08lX%08lX%08lX%08lX%08lX%08lX%08lX (%s)\n",
1256
1342
               (ulong)mask[7], (ulong)mask[6], (ulong)mask[5], (ulong)mask[4],
1257
 
             (ulong)mask[3], (ulong)mask[2], (ulong)mask[1], (ulong)mask[0]);
 
1343
               (ulong)mask[3], (ulong)mask[2], (ulong)mask[1], (ulong)mask[0],
 
1344
               keystr_from_pk (pkr->pk));
1258
1345
#endif
1259
1346
        for(i=0; i < 8; i++ )
1260
1347
            bits[i] &= mask[i];
1347
1434
}
1348
1435
 
1349
1436
/*
1350
 
 * Select the MDC flag from the pk_list.  We can only use MDC if all recipients
1351
 
 * support this feature 
 
1437
 * Select the MDC flag from the pk_list.  We can only use MDC if all
 
1438
 * recipients support this feature.
1352
1439
 */
1353
1440
int
1354
1441
select_mdc_from_pklist (PK_LIST pk_list)
1355
1442
{
1356
 
    PK_LIST pkr;
1357
 
 
1358
 
    if( !pk_list )
1359
 
        return 0;
1360
 
 
1361
 
    for (pkr = pk_list; pkr; pkr = pkr->next) {
1362
 
        int mdc;
1363
 
 
1364
 
        if (pkr->pk->user_id) /* selected by user ID */
1365
 
            mdc = pkr->pk->user_id->mdc_feature;
1366
 
        else
1367
 
            mdc = pkr->pk->mdc_feature;
1368
 
        if (!mdc)
1369
 
            return 0; /* at least one recipient does not support it */
1370
 
    }
1371
 
    return 1; /* can be used */
 
1443
  PK_LIST pkr;
 
1444
 
 
1445
  if ( !pk_list )
 
1446
    return 0;
 
1447
  
 
1448
  for (pkr = pk_list; pkr; pkr = pkr->next) 
 
1449
    {
 
1450
      int mdc;
 
1451
      
 
1452
      if (pkr->pk->user_id) /* selected by user ID */
 
1453
        mdc = pkr->pk->user_id->flags.mdc;
 
1454
      else
 
1455
        mdc = pkr->pk->mdc_feature;
 
1456
      if (!mdc)
 
1457
        return 0;  /* At least one recipient does not support it. */
 
1458
    }
 
1459
  return 1; /* Can be used. */
 
1460
}
 
1461
 
 
1462
 
 
1463
/* Print a warning for all keys in PK_LIST missing the MDC feature. */
 
1464
void
 
1465
warn_missing_mdc_from_pklist (PK_LIST pk_list)
 
1466
{
 
1467
  PK_LIST pkr;
 
1468
  
 
1469
  for (pkr = pk_list; pkr; pkr = pkr->next) 
 
1470
    {
 
1471
      int mdc;
 
1472
 
 
1473
      if (pkr->pk->user_id) /* selected by user ID */
 
1474
        mdc = pkr->pk->user_id->flags.mdc;
 
1475
      else
 
1476
        mdc = pkr->pk->mdc_feature;
 
1477
      if (!mdc)
 
1478
        log_info (_("Note: key %s has no %s feature\n"),
 
1479
                  keystr_from_pk (pkr->pk), "MDC");
 
1480
    }
 
1481
}
 
1482
 
 
1483
void
 
1484
warn_missing_aes_from_pklist (PK_LIST pk_list)
 
1485
{
 
1486
  PK_LIST pkr;
 
1487
 
 
1488
  for (pkr = pk_list; pkr; pkr = pkr->next) 
 
1489
    {
 
1490
      const prefitem_t *prefs;
 
1491
      int i;
 
1492
      int gotit = 0;
 
1493
 
 
1494
      prefs = pkr->pk->user_id? pkr->pk->user_id->prefs : pkr->pk->prefs;
 
1495
      if (prefs)
 
1496
        {
 
1497
          for (i=0; !gotit && prefs[i].type; i++ )
 
1498
            if (prefs[i].type == PREFTYPE_SYM 
 
1499
                && prefs[i].value == CIPHER_ALGO_AES)
 
1500
              gotit++;
 
1501
        }
 
1502
      if (!gotit)
 
1503
        log_info (_("Note: key %s has no preference for %s\n"),
 
1504
                  keystr_from_pk (pkr->pk), "AES");
 
1505
    }
1372
1506
}