~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to g10/pkclist.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
 
3
 *               2008 Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of GnuPG.
6
6
 *
236
236
                if((opt.verify_options&VERIFY_SHOW_PHOTOS)
237
237
                   && un->pkt->pkt.user_id->attrib_data)
238
238
                  show_photos(un->pkt->pkt.user_id->attribs,
239
 
                              un->pkt->pkt.user_id->numattribs,pk,NULL);
 
239
                              un->pkt->pkt.user_id->numattribs,pk,NULL,
 
240
                              un->pkt->pkt.user_id);
240
241
 
241
242
                p=utf8_to_native(un->pkt->pkt.user_id->name,
242
243
                                 un->pkt->pkt.user_id->len,0);
774
775
     Bit 1: This is a hidden recipient.
775
776
 
776
777
   USE is the desired use for the key - usually PUBKEY_USAGE_ENC.
777
 
   RET_PK_LIST.
778
778
 
779
779
   On success a list of keys is stored at the address RET_PK_LIST; the
780
780
   caller must free this list.  On error the value at this address is
825
825
          pk->req_usage = use;
826
826
 
827
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 )) ) 
 
828
             we pass 1for the second last argument and 1 as the last
 
829
             argument to disable AKL. */
 
830
          if ( (rc = get_pubkey_byname (NULL, pk, rov->d, NULL, NULL, 1, 1)) ) 
830
831
            {
831
832
              free_public_key ( pk ); pk = NULL;
832
833
              log_error (_("%s: skipped: %s\n"), rov->d, g10_errstr(rc) );
965
966
            free_public_key (pk);
966
967
          pk = xmalloc_clear( sizeof *pk );
967
968
          pk->req_usage = use;
968
 
          rc = get_pubkey_byname( pk, answer, NULL, NULL, 0 );
 
969
          rc = get_pubkey_byname (NULL, pk, answer, NULL, NULL, 0, 0 );
969
970
          if (rc)
970
971
            tty_printf(_("No such user ID.\n"));
971
972
          else if ( !(rc=openpgp_pk_test_algo2 (pk->pubkey_algo, use)) ) 
1038
1039
      pk->req_usage = use;
1039
1040
 
1040
1041
      /* 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);
 
1042
         as second last argument.  We also don't want an AKL. */
 
1043
      rc = get_pubkey_byname (NULL, pk, def_rec, NULL, NULL, 1, 1);
1043
1044
      if (rc)
1044
1045
        log_error(_("unknown default recipient \"%s\"\n"), def_rec );
1045
1046
      else if ( !(rc=openpgp_pk_test_algo2(pk->pubkey_algo, use)) ) 
1079
1080
 
1080
1081
          pk = xmalloc_clear( sizeof *pk );
1081
1082
          pk->req_usage = use;
1082
 
          if ( (rc = get_pubkey_byname( pk, remusr->d, NULL, NULL, 0 )) ) 
 
1083
          if ((rc = get_pubkey_byname (NULL, pk, remusr->d, NULL, NULL, 0, 0)))
1083
1084
            {
1084
1085
              /* Key not found or other error. */
1085
1086
              free_public_key( pk ); pk = NULL;
1263
1264
    return 0;
1264
1265
}
1265
1266
 
1266
 
 
1267
 
 
1268
1267
/****************
1269
1268
 * Return -1 if we could not find an algorithm.
1270
1269
 */
1271
1270
int
1272
 
select_algo_from_prefs(PK_LIST pk_list, int preftype, int request,
1273
 
                       const union pref_hint *hint)
 
1271
select_algo_from_prefs(PK_LIST pk_list, int preftype,
 
1272
                       int request, const union pref_hint *hint)
1274
1273
{
1275
 
    PK_LIST pkr;
1276
 
    u32 bits[8];
1277
 
    const prefitem_t *prefs;
1278
 
    int i, j;
1279
 
    int compr_hack=0;
1280
 
    int any;
1281
 
 
1282
 
    if( !pk_list )
1283
 
        return -1;
1284
 
 
1285
 
    memset( bits, ~0, 8 * sizeof *bits );
1286
 
    for( pkr = pk_list; pkr; pkr = pkr->next ) {
1287
 
        u32 mask[8];
1288
 
 
1289
 
        memset( mask, 0, 8 * sizeof *mask );
1290
 
        if( preftype == PREFTYPE_SYM ) {
1291
 
          if( PGP2 &&
1292
 
              pkr->pk->version < 4 &&
1293
 
              pkr->pk->selfsigversion < 4 )
1294
 
            mask[0] |= (1<<1); /* IDEA is implicitly there for v3 keys
1295
 
                                  with v3 selfsigs (rfc2440:12.1) if
1296
 
                                  --pgp2 mode is on.  This doesn't
1297
 
                                  mean it's actually available, of
1298
 
                                  course. */
 
1274
  PK_LIST pkr;
 
1275
  u32 bits[8];
 
1276
  const prefitem_t *prefs;
 
1277
  int result=-1,i;
 
1278
  unsigned int best=-1;    
 
1279
  byte scores[256];
 
1280
    
 
1281
  if( !pk_list )
 
1282
    return -1;
 
1283
 
 
1284
  memset(bits,0xFF,sizeof(bits));
 
1285
  memset(scores,0,sizeof(scores));
 
1286
 
 
1287
  for( pkr = pk_list; pkr; pkr = pkr->next )
 
1288
    {
 
1289
      u32 mask[8];
 
1290
      int rank=1,implicit=-1;
 
1291
 
 
1292
      memset(mask,0,sizeof(mask));
 
1293
 
 
1294
      switch(preftype)
 
1295
        {
 
1296
        case PREFTYPE_SYM:
 
1297
          /* IDEA is implicitly there for v3 keys with v3 selfsigs if
 
1298
             --pgp2 mode is on.  This was a 2440 thing that was
 
1299
             dropped from 4880 but is still relevant to GPG's 1991
 
1300
             support.  All this doesn't mean IDEA is actually
 
1301
             available, of course. */
 
1302
          if(PGP2 && pkr->pk->version<4 && pkr->pk->selfsigversion<4)
 
1303
            implicit=CIPHER_ALGO_IDEA;
1299
1304
          else
1300
 
            mask[0] |= (1<<2); /* 3DES is implicitly there for everyone else */
1301
 
        }
1302
 
        else if( preftype == PREFTYPE_HASH ) {
 
1305
            implicit=CIPHER_ALGO_3DES;
 
1306
 
 
1307
          break;
 
1308
 
 
1309
        case PREFTYPE_HASH:
1303
1310
          /* While I am including this code for completeness, note
1304
1311
             that currently --pgp2 mode locks the hash at MD5, so this
1305
 
             function will never even be called.  Even if the hash
1306
 
             wasn't locked at MD5, we don't support sign+encrypt in
1307
 
             --pgp2 mode, and that's the only time PREFTYPE_HASH is
1308
 
             used anyway. -dms */
1309
 
          if( PGP2 &&
1310
 
              pkr->pk->version < 4 &&
1311
 
              pkr->pk->selfsigversion < 4 )
1312
 
            mask[0] |= (1<<1); /* MD5 is there for v3 keys with v3
1313
 
                                  selfsigs when --pgp2 is on. */
 
1312
             code will never even be called.  Even if the hash wasn't
 
1313
             locked at MD5, we don't support sign+encrypt in --pgp2
 
1314
             mode, and that's the only time PREFTYPE_HASH is used
 
1315
             anyway. -dms */
 
1316
 
 
1317
          /* MD5 is there for v3 keys with v3 selfsigs when --pgp2 is
 
1318
             on. */
 
1319
          if(PGP2 && pkr->pk->version<4 && pkr->pk->selfsigversion<4)
 
1320
            implicit=DIGEST_ALGO_MD5;
1314
1321
          else
1315
 
            mask[0] |= (1<<2); /* SHA1 is there for everyone else */
1316
 
        }
1317
 
        else if( preftype == PREFTYPE_ZIP )
1318
 
          mask[0] |= (1<<0); /* Uncompressed is implicit */
1319
 
 
1320
 
        if (pkr->pk->user_id) /* selected by user ID */
1321
 
            prefs = pkr->pk->user_id->prefs;
1322
 
        else
1323
 
            prefs = pkr->pk->prefs;
1324
 
 
1325
 
        any = 0;
1326
 
        if( prefs ) {
1327
 
            for (i=0; prefs[i].type; i++ ) {
1328
 
                if( prefs[i].type == preftype ) {
1329
 
                    mask[prefs[i].value/32] |= 1 << (prefs[i].value%32);
1330
 
                    any = 1;
1331
 
                }
1332
 
            }
1333
 
        }
1334
 
 
1335
 
        if( (!prefs || !any) && preftype == PREFTYPE_ZIP ) {
1336
 
            mask[0] |= 3; /* asume no_compression and old pgp */
1337
 
            compr_hack = 1;
1338
 
        }
1339
 
 
1340
 
#if 0
1341
 
        log_debug("pref mask=%08lX%08lX%08lX%08lX%08lX%08lX%08lX%08lX (%s)\n",
1342
 
               (ulong)mask[7], (ulong)mask[6], (ulong)mask[5], (ulong)mask[4],
1343
 
               (ulong)mask[3], (ulong)mask[2], (ulong)mask[1], (ulong)mask[0],
1344
 
               keystr_from_pk (pkr->pk));
1345
 
#endif
1346
 
        for(i=0; i < 8; i++ )
1347
 
            bits[i] &= mask[i];
1348
 
#if 0
1349
 
        log_debug("pref bits=%08lX%08lX%08lX%08lX%08lX%08lX%08lX%08lX\n",
1350
 
               (ulong)bits[7], (ulong)bits[6], (ulong)bits[5], (ulong)bits[4],
1351
 
             (ulong)bits[3], (ulong)bits[2], (ulong)bits[1], (ulong)bits[0]);
1352
 
#endif
1353
 
    }
1354
 
    /* usable algorithms are now in bits
1355
 
     * We now use the last key from pk_list to select
1356
 
     * the algorithm we want to use. there are no
1357
 
     * preferences for the last key, we select the one
1358
 
     * corresponding to first set bit.
1359
 
     */
1360
 
    i = -1;
1361
 
    any = 0;
1362
 
 
1363
 
    /* Can we use the requested algorithm? */
1364
 
    if(request>-1 && (bits[request/32] & (1<<(request%32))) &&
1365
 
       algo_available(preftype,request,hint))
1366
 
      return request;
1367
 
 
1368
 
    /* If we have personal prefs set, use them instead of the last key */
1369
 
    if(preftype==PREFTYPE_SYM && opt.personal_cipher_prefs)
1370
 
      prefs=opt.personal_cipher_prefs;
1371
 
    else if(preftype==PREFTYPE_HASH && opt.personal_digest_prefs)
1372
 
      prefs=opt.personal_digest_prefs;
1373
 
    else if(preftype==PREFTYPE_ZIP && opt.personal_compress_prefs)
1374
 
      prefs=opt.personal_compress_prefs;
1375
 
 
1376
 
    if( prefs ) {
1377
 
        for(j=0; prefs[j].type; j++ ) {
1378
 
            if( prefs[j].type == preftype ) {
1379
 
                if( (bits[prefs[j].value/32] & (1<<(prefs[j].value%32))) ) {
1380
 
                    if( algo_available( preftype, prefs[j].value, hint ) ) {
1381
 
                        any = 1;
1382
 
                        i = prefs[j].value;
1383
 
                        break;
1384
 
                    }
1385
 
                }
1386
 
            }
1387
 
        }
1388
 
    }
1389
 
    if( !prefs || !any ) {
1390
 
        for(j=0; j < 256; j++ )
1391
 
            if( (bits[j/32] & (1<<(j%32))) ) {
1392
 
                if( algo_available( preftype, j, hint ) ) {
1393
 
                    i = j;
1394
 
                    break;
1395
 
                }
1396
 
            }
1397
 
    }
1398
 
 
1399
 
#if 0
1400
 
    log_debug("prefs of type %d: selected %d\n", preftype, i );
1401
 
#endif
1402
 
    if( compr_hack && !i ) {
1403
 
        /* selected no compression, but we should check whether
1404
 
         * algorithm 1 is also available (the ordering is not relevant
1405
 
         * in this case). */
1406
 
        if( bits[0] & (1<<1) )
1407
 
            i = 1; /* yep; we can use compression algo 1 */
1408
 
    }
1409
 
 
1410
 
    /* "If you are building an authentication system, the recipient
1411
 
       may specify a preferred signing algorithm. However, the signer
1412
 
       would be foolish to use a weak algorithm simply because the
1413
 
       recipient requests it." RFC2440:13.  If we settle on MD5, and
1414
 
       SHA1 is also available, use SHA1 instead.  Of course, if the
1415
 
       user intentionally chose MD5 (by putting it in their personal
1416
 
       prefs), then we should do what they say. */
1417
 
 
1418
 
    if(preftype==PREFTYPE_HASH &&
1419
 
       i==DIGEST_ALGO_MD5 && (bits[0] & (1<<DIGEST_ALGO_SHA1)))
1420
 
      {
1421
 
        i=DIGEST_ALGO_SHA1;
1422
 
 
1423
 
        if(opt.personal_digest_prefs)
1424
 
          for(j=0; prefs[j].type; j++ )
1425
 
            if(opt.personal_digest_prefs[j].type==PREFTYPE_HASH &&
1426
 
               opt.personal_digest_prefs[j].value==DIGEST_ALGO_MD5)
 
1322
            implicit=DIGEST_ALGO_SHA1;
 
1323
 
 
1324
          break;
 
1325
 
 
1326
        case PREFTYPE_ZIP:
 
1327
          /* Uncompressed is always an option. */
 
1328
          implicit=COMPRESS_ALGO_NONE;
 
1329
        }
 
1330
 
 
1331
      if (pkr->pk->user_id) /* selected by user ID */
 
1332
        prefs = pkr->pk->user_id->prefs;
 
1333
      else
 
1334
        prefs = pkr->pk->prefs;
 
1335
 
 
1336
      if( prefs )
 
1337
        {
 
1338
          for (i=0; prefs[i].type; i++ )
 
1339
            {
 
1340
              if( prefs[i].type == preftype )
 
1341
                {
 
1342
                  scores[prefs[i].value]+=rank;
 
1343
                  mask[prefs[i].value/32] |= 1<<(prefs[i].value%32);
 
1344
 
 
1345
                  rank++;
 
1346
 
 
1347
                  /* We saw the implicit algorithm, so we don't need
 
1348
                     tack it on the end ourselves. */
 
1349
                  if(implicit==prefs[i].value)
 
1350
                    implicit=-1;
 
1351
                }
 
1352
            }
 
1353
        }
 
1354
 
 
1355
      if(rank==1 && preftype==PREFTYPE_ZIP)
 
1356
        {
 
1357
          /* If the compression preferences are not present, they are
 
1358
             assumed to be ZIP, Uncompressed (RFC4880:13.3.1) */
 
1359
          scores[1]=1; /* ZIP is first choice */
 
1360
          scores[0]=2; /* Uncompressed is second choice */
 
1361
          mask[0]|=3;
 
1362
        }
 
1363
 
 
1364
      /* If the key didn't have the implicit algorithm listed
 
1365
         explicitly, add it here at the tail of the list. */
 
1366
      if(implicit>-1)
 
1367
        {
 
1368
          scores[implicit]+=rank;
 
1369
          mask[implicit/32] |= 1<<(implicit%32);
 
1370
        }
 
1371
 
 
1372
      for(i=0;i<8;i++)
 
1373
        bits[i]&=mask[i];
 
1374
    }
 
1375
 
 
1376
  /* We've now scored all of the algorithms, and the usable ones have
 
1377
     bits set.  Let's pick the winner. */
 
1378
 
 
1379
  /* The caller passed us a request.  Can we use it? */
 
1380
  if(request>-1 && (bits[request/32] & (1<<(request%32))) &&
 
1381
     algo_available(preftype,request,hint))
 
1382
    result=request;
 
1383
 
 
1384
  if(result==-1)
 
1385
    {
 
1386
      /* If we have personal prefs set, use them. */
 
1387
      prefs=NULL;
 
1388
      if(preftype==PREFTYPE_SYM && opt.personal_cipher_prefs)
 
1389
        prefs=opt.personal_cipher_prefs;
 
1390
      else if(preftype==PREFTYPE_HASH && opt.personal_digest_prefs)
 
1391
        prefs=opt.personal_digest_prefs;
 
1392
      else if(preftype==PREFTYPE_ZIP && opt.personal_compress_prefs)
 
1393
        prefs=opt.personal_compress_prefs;
 
1394
 
 
1395
      if( prefs )
 
1396
        for(i=0; prefs[i].type; i++ )
 
1397
          {
 
1398
            if(bits[prefs[i].value/32] & (1<<(prefs[i].value%32))
 
1399
               && algo_available( preftype, prefs[i].value, hint))
1427
1400
              {
1428
 
                i=DIGEST_ALGO_MD5;
 
1401
                result = prefs[i].value;
1429
1402
                break;
1430
1403
              }
1431
 
      }
1432
 
 
1433
 
    return i;
 
1404
          }
 
1405
    }
 
1406
 
 
1407
  if(result==-1)
 
1408
    {
 
1409
      /* At this point, we have not selected an algorithm due to a
 
1410
         special request or via personal prefs.  Pick the highest
 
1411
         ranked algorithm (i.e. the one with the lowest score). */
 
1412
 
 
1413
      for(i=0;i<256;i++)
 
1414
        {
 
1415
          /* Note the '<' here.  This means in case of a tie, we will
 
1416
             favor the lower algorithm number.  We have a choice
 
1417
             between the lower number (probably an older algorithm
 
1418
             with more time in use), or the higher number (probably a
 
1419
             newer algorithm with less time in use).  Older is
 
1420
             probably safer here, even though the newer algorithms
 
1421
             tend to be "stronger". */
 
1422
          if(scores[i] && scores[i]<best
 
1423
             && (bits[i/32] & (1<<(i%32)))
 
1424
             && algo_available(preftype,i,hint))
 
1425
            {
 
1426
              best=scores[i];
 
1427
              result=i;
 
1428
            }
 
1429
        }
 
1430
 
 
1431
      /* "If you are building an authentication system, the recipient
 
1432
         may specify a preferred signing algorithm. However, the
 
1433
         signer would be foolish to use a weak algorithm simply
 
1434
         because the recipient requests it." (RFC4880:14).  If we
 
1435
         settle on MD5, and SHA1 is also available, use SHA1 instead.
 
1436
         Note that if the user intentionally chose MD5 by putting it
 
1437
         in their personal prefs, then we do what the user said (as we
 
1438
         never reach this code). */
 
1439
      if(preftype==PREFTYPE_HASH && result==DIGEST_ALGO_MD5
 
1440
         && (bits[0] & (1<<DIGEST_ALGO_SHA1)))
 
1441
        result=DIGEST_ALGO_SHA1;
 
1442
    }
 
1443
 
 
1444
  return result;
1434
1445
}
1435
1446
 
1436
1447
/*