~ubuntu-branches/ubuntu/precise/gnupg2/precise-security

« back to all changes in this revision

Viewing changes to g10/import.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2015-03-27 08:20:03 UTC
  • Revision ID: package-import@ubuntu.com-20150327082003-doob38ydqdxubnf1
Tags: 2.0.17-2ubuntu2.12.04.6
* Screen responses from keyservers (LP: #1409117)
  - d/p/0001-Screen-keyserver-responses.patch
  - d/p/0002-Make-screening-of-keyserver-result-work-with-multi-k.patch
  - d/p/0003-Add-kbnode_t-for-easier-backporting.patch
  - d/p/0004-gpg-Fix-regression-due-to-the-keyserver-import-filte.patch
* Fix large key size regression from CVE-2014-5270 changes (LP: #1371766)
  - d/p/Add-build-and-runtime-support-for-larger-RSA-key.patch
  - debian/rules: build with --enable-large-secmem
* SECURITY UPDATE: invalid memory read via invalid keyring
  - debian/patches/CVE-2015-1606.patch: skip all packets not allowed in
    a keyring in g10/keyring.c.
  - CVE-2015-1606
* SECURITY UPDATE: memcpy with overlapping ranges
  - debian/patches/CVE-2015-1607.patch: use inline functions to convert
    buffer data to scalars in common/iobuf.c, g10/build-packet.c,
    g10/getkey.c, g10/keygen.c, g10/keyid.c, g10/main.h, g10/misc.c,
    g10/parse-packet.c, g10/tdbio.c, g10/trustdb.c, include/host2net.h,
    kbx/keybox-dump.c, kbx/keybox-openpgp.c, kbx/keybox-search.c,
    kbx/keybox-update.c, scd/apdu.c, scd/app-openpgp.c,
    scd/ccid-driver.c, scd/pcsc-wrapper.c, tools/ccidmon.c.
  - CVE-2015-1607

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
 
60
60
 
61
61
static int import( IOBUF inp, const char* fname,struct stats_s *stats,
62
 
                   unsigned char **fpr,size_t *fpr_len,unsigned int options );
 
62
                   unsigned char **fpr,size_t *fpr_len,unsigned int options,
 
63
                   import_filter_t filter, void *filter_arg );
63
64
static int read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root );
64
65
static void revocation_present(KBNODE keyblock);
65
66
static int import_one(const char *fname, KBNODE keyblock,struct stats_s *stats,
66
67
                      unsigned char **fpr,size_t *fpr_len,
67
 
                      unsigned int options,int from_sk);
 
68
                      unsigned int options,int from_sk,
 
69
                      import_filter_t filter, void *filter_arg);
68
70
static int import_secret_one( const char *fname, KBNODE keyblock,
69
 
                              struct stats_s *stats, unsigned int options);
 
71
                              struct stats_s *stats, unsigned int options,
 
72
                              import_filter_t filter, void *filter_arg);
70
73
static int import_revoke_cert( const char *fname, KBNODE node,
71
74
                               struct stats_s *stats);
72
75
static int chk_self_sigs( const char *fname, KBNODE keyblock,
163
166
static int
164
167
import_keys_internal( IOBUF inp, char **fnames, int nnames,
165
168
                      void *stats_handle, unsigned char **fpr, size_t *fpr_len,
166
 
                      unsigned int options )
 
169
                      unsigned int options,
 
170
                      import_filter_t filter, void *filter_arg)
167
171
{
168
172
    int i, rc = 0;
169
173
    struct stats_s *stats = stats_handle;
172
176
        stats = import_new_stats_handle ();
173
177
 
174
178
    if (inp) {
175
 
        rc = import( inp, "[stream]", stats, fpr, fpr_len, options);
 
179
        rc = import (inp, "[stream]", stats, fpr, fpr_len, options,
 
180
                     filter, filter_arg);
176
181
    }
177
182
    else {
178
183
        int once = (!fnames && !nnames);
192
197
                log_error(_("can't open `%s': %s\n"), fname, strerror(errno) );
193
198
            else
194
199
              {
195
 
                rc = import( inp2, fname, stats, fpr, fpr_len, options );
 
200
                rc = import (inp2, fname, stats, fpr, fpr_len, options,
 
201
                             NULL, NULL);
196
202
                iobuf_close(inp2);
197
203
                /* Must invalidate that ugly cache to actually close it. */
198
204
                iobuf_ioctl (NULL, 2, 0, (char*)fname);
223
229
import_keys( char **fnames, int nnames,
224
230
             void *stats_handle, unsigned int options )
225
231
{
226
 
  import_keys_internal(NULL,fnames,nnames,stats_handle,NULL,NULL,options);
 
232
  import_keys_internal (NULL, fnames, nnames, stats_handle, NULL, NULL,
 
233
                        options, NULL, NULL);
227
234
}
228
235
 
229
236
int
230
237
import_keys_stream( IOBUF inp, void *stats_handle,
231
 
                    unsigned char **fpr, size_t *fpr_len,unsigned int options )
 
238
                    unsigned char **fpr, size_t *fpr_len,unsigned int options,
 
239
                    import_filter_t filter, void *filter_arg)
232
240
{
233
 
  return import_keys_internal(inp,NULL,0,stats_handle,fpr,fpr_len,options);
 
241
  return import_keys_internal (inp, NULL, 0, stats_handle, fpr, fpr_len,
 
242
                               options, filter, filter_arg);
234
243
}
235
244
 
 
245
 
236
246
static int
237
 
import( IOBUF inp, const char* fname,struct stats_s *stats,
238
 
        unsigned char **fpr,size_t *fpr_len,unsigned int options )
 
247
import (IOBUF inp, const char* fname,struct stats_s *stats,
 
248
        unsigned char **fpr, size_t *fpr_len, unsigned int options,
 
249
        import_filter_t filter, void *filter_arg)
239
250
{
240
251
    PACKET *pending_pkt = NULL;
241
 
    KBNODE keyblock = NULL;  /* Need to initialize because gcc can't
242
 
                                grasp the return semantics of
243
 
                                read_block. */
 
252
    KBNODE keyblock = NULL;
244
253
    int rc = 0;
245
254
 
246
255
    getkey_disable_caches();
256
265
 
257
266
    while( !(rc = read_block( inp, &pending_pkt, &keyblock) )) {
258
267
        if( keyblock->pkt->pkttype == PKT_PUBLIC_KEY )
259
 
            rc = import_one( fname, keyblock, stats, fpr, fpr_len, options, 0);
260
 
        else if( keyblock->pkt->pkttype == PKT_SECRET_KEY ) 
261
 
                rc = import_secret_one( fname, keyblock, stats, options );
 
268
            rc = import_one (fname, keyblock, stats, fpr, fpr_len, options, 0,
 
269
                             filter, filter_arg);
 
270
        else if( keyblock->pkt->pkttype == PKT_SECRET_KEY )
 
271
            rc = import_secret_one (fname, keyblock, stats, options,
 
272
                                    filter, filter_arg);
262
273
        else if( keyblock->pkt->pkttype == PKT_SIGNATURE
263
274
                 && keyblock->pkt->pkt.signature->sig_class == 0x20 )
264
275
            rc = import_revoke_cert( fname, keyblock, stats );
634
645
  KBNODE node;
635
646
  PKT_public_key *pk;
636
647
  int problem=0;
637
 
  
 
648
 
638
649
  merge_keys_and_selfsig(keyblock);
639
650
  pk=keyblock->pkt->pkt.public_key;
640
651
 
659
670
                {
660
671
                  if (openpgp_cipher_test_algo (prefs->value))
661
672
                    {
662
 
                      const char *algo = 
 
673
                      const char *algo =
663
674
                        (openpgp_cipher_test_algo (prefs->value)
664
 
                         ? num 
 
675
                         ? num
665
676
                         : openpgp_cipher_algo_name (prefs->value));
666
677
                      if(!problem)
667
678
                        check_prefs_warning(pk);
676
687
                    {
677
688
                      const char *algo =
678
689
                        (gcry_md_test_algo (prefs->value)
679
 
                         ? num 
 
690
                         ? num
680
691
                         : gcry_md_algo_name (prefs->value));
681
692
                      if(!problem)
682
693
                        check_prefs_warning(pk);
745
756
static int
746
757
import_one( const char *fname, KBNODE keyblock, struct stats_s *stats,
747
758
            unsigned char **fpr,size_t *fpr_len,unsigned int options,
748
 
            int from_sk )
 
759
            int from_sk, import_filter_t filter, void *filter_arg)
749
760
{
750
761
    PKT_public_key *pk;
751
762
    PKT_public_key *pk_orig;
787
798
        log_error( _("key %s: no user ID\n"), keystr_from_pk(pk));
788
799
        return 0;
789
800
      }
790
 
    
 
801
 
 
802
    if (filter && filter (keyblock, filter_arg))
 
803
      {
 
804
        log_error (_("key %s: %s\n"), keystr_from_pk(pk),
 
805
                   _("rejected by import filter"));
 
806
        return 0;
 
807
      }
 
808
 
791
809
    if (opt.interactive) {
792
810
        if(is_status_enabled())
793
811
          print_import_check (pk, uidnode->pkt->pkt.user_id);
924
942
            size_t an;
925
943
 
926
944
            fingerprint_from_pk (pk_orig, afp, &an);
927
 
            while (an < MAX_FINGERPRINT_LEN) 
 
945
            while (an < MAX_FINGERPRINT_LEN)
928
946
                afp[an++] = 0;
929
947
            rc = keydb_search_fpr (hd, afp);
930
948
        }
948
966
        n_sigs_cleaned = fix_bad_direct_key_sigs (keyblock_orig, keyid);
949
967
        if (n_sigs_cleaned)
950
968
          commit_kbnode (&keyblock_orig);
951
 
            
 
969
 
952
970
        /* and try to merge the block */
953
971
        clear_kbnode_flags( keyblock_orig );
954
972
        clear_kbnode_flags( keyblock );
1018
1036
            stats->n_sigs_cleaned +=n_sigs_cleaned;
1019
1037
            stats->n_uids_cleaned +=n_uids_cleaned;
1020
1038
 
1021
 
            if (is_status_enabled ()) 
 
1039
            if (is_status_enabled ())
1022
1040
                 print_import_ok (pk, NULL,
1023
1041
                                  ((n_uids?2:0)|(n_sigs?4:0)|(n_subk?8:0)));
1024
1042
        }
1025
1043
        else
1026
1044
          {
1027
1045
            same_key = 1;
1028
 
            if (is_status_enabled ()) 
 
1046
            if (is_status_enabled ())
1029
1047
              print_import_ok (pk, NULL, 0);
1030
1048
 
1031
1049
            if( !opt.quiet )
1165
1183
 * with the trust calculation.
1166
1184
 */
1167
1185
static int
1168
 
import_secret_one( const char *fname, KBNODE keyblock, 
1169
 
                   struct stats_s *stats, unsigned int options)
 
1186
import_secret_one (const char *fname, KBNODE keyblock,
 
1187
                   struct stats_s *stats, unsigned int options,
 
1188
                   import_filter_t filter, void *filter_arg)
1170
1189
{
1171
1190
    PKT_secret_key *sk;
1172
1191
    KBNODE node, uidnode;
1173
1192
    u32 keyid[2];
1174
1193
    int rc = 0;
1175
1194
 
1176
 
    /* get the key and print some info about it */
 
1195
    /* Get the key and print some info about it. */
1177
1196
    node = find_kbnode( keyblock, PKT_SECRET_KEY );
1178
1197
    if( !node )
1179
1198
        BUG();
1182
1201
    keyid_from_sk( sk, keyid );
1183
1202
    uidnode = find_next_kbnode( keyblock, PKT_USER_ID );
1184
1203
 
 
1204
    if (filter && filter (keyblock, filter_arg)) {
 
1205
        log_error (_("secret key %s: %s\n"), keystr_from_sk(sk),
 
1206
                   _("rejected by import filter"));
 
1207
        return 0;
 
1208
    }
 
1209
 
1185
1210
    if( opt.verbose )
1186
1211
      {
1187
1212
        log_info( "sec  %4u%c/%s %s   ",
1217
1242
        log_error (_("importing secret keys not allowed\n"));
1218
1243
        return 0;
1219
1244
      }
1220
 
#endif 
1221
 
    
 
1245
#endif
 
1246
 
1222
1247
    clear_kbnode_flags( keyblock );
1223
1248
 
1224
1249
    /* do we have this key already in one of our secrings ? */
1244
1269
        if( !opt.quiet )
1245
1270
          log_info( _("key %s: secret key imported\n"), keystr_from_sk(sk));
1246
1271
        stats->secret_imported++;
1247
 
        if (is_status_enabled ()) 
 
1272
        if (is_status_enabled ())
1248
1273
          print_import_ok (NULL, sk, 1|16);
1249
1274
 
1250
1275
        if(options&IMPORT_SK2PK)
1254
1279
            KBNODE pub_keyblock=sec_to_pub_keyblock(keyblock);
1255
1280
            if(pub_keyblock)
1256
1281
              {
1257
 
                import_one(fname,pub_keyblock,stats,
1258
 
                           NULL,NULL,opt.import_options,1);
 
1282
                import_one (fname, pub_keyblock, stats,
 
1283
                            NULL, NULL, opt.import_options, 1,
 
1284
                            NULL, NULL);
1259
1285
                release_kbnode(pub_keyblock);
1260
1286
              }
1261
1287
          }
1275
1301
        log_error( _("key %s: already in secret keyring\n"),
1276
1302
                   keystr_from_sk(sk));
1277
1303
        stats->secret_dups++;
1278
 
        if (is_status_enabled ()) 
 
1304
        if (is_status_enabled ())
1279
1305
          print_import_ok (NULL, sk, 16);
1280
1306
 
1281
1307
        /* TODO: if we ever do merge secret keys, make sure to handle
1331
1357
    {
1332
1358
        byte afp[MAX_FINGERPRINT_LEN];
1333
1359
        size_t an;
1334
 
        
 
1360
 
1335
1361
        fingerprint_from_pk (pk, afp, &an);
1336
 
        while (an < MAX_FINGERPRINT_LEN) 
 
1362
        while (an < MAX_FINGERPRINT_LEN)
1337
1363
            afp[an++] = 0;
1338
1364
        rc = keydb_search_fpr (hd, afp);
1339
1365
    }
1429
1455
  int rc;
1430
1456
  u32 bsdate=0,rsdate=0;
1431
1457
  KBNODE bsnode = NULL, rsnode = NULL;
1432
 
  
 
1458
 
1433
1459
  (void)fname;
1434
1460
  (void)pk;
1435
1461
 
1436
 
  for (n=keyblock; (n = find_next_kbnode (n, 0)); ) 
 
1462
  for (n=keyblock; (n = find_next_kbnode (n, 0)); )
1437
1463
    {
1438
1464
      if (n->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1439
1465
        {
1447
1473
 
1448
1474
      if ( n->pkt->pkttype != PKT_SIGNATURE )
1449
1475
        continue;
1450
 
      
 
1476
 
1451
1477
      sig = n->pkt->pkt.signature;
1452
1478
      if ( keyid[0] != sig->keyid[0] || keyid[1] != sig->keyid[1] )
1453
1479
        {
1459
1485
         import a fully-cached key which speeds things up. */
1460
1486
      if (!opt.no_sig_cache)
1461
1487
        check_key_signature (keyblock, n, NULL);
1462
 
      
 
1488
 
1463
1489
      if ( IS_UID_SIG(sig) || IS_UID_REV(sig) )
1464
1490
        {
1465
1491
          KBNODE unode = find_prev_kbnode( keyblock, n, PKT_USER_ID );
1469
1495
                         keystr(keyid));
1470
1496
              return -1;  /* The complete keyblock is invalid.  */
1471
1497
            }
1472
 
          
 
1498
 
1473
1499
          /* If it hasn't been marked valid yet, keep trying.  */
1474
 
          if (!(unode->flag&1)) 
 
1500
          if (!(unode->flag&1))
1475
1501
            {
1476
1502
              rc = check_key_signature (keyblock, n, NULL);
1477
1503
              if ( rc )
1478
1504
                {
1479
1505
                  if ( opt.verbose )
1480
1506
                    {
1481
 
                      char *p = utf8_to_native 
 
1507
                      char *p = utf8_to_native
1482
1508
                        (unode->pkt->pkt.user_id->name,
1483
1509
                         strlen (unode->pkt->pkt.user_id->name),0);
1484
1510
                      log_info (gpg_err_code(rc) == G10ERR_PUBKEY_ALGO ?
1507
1533
              n->flag |= 4;
1508
1534
            }
1509
1535
        }
1510
 
      else if ( IS_SUBKEY_SIG (sig) ) 
 
1536
      else if ( IS_SUBKEY_SIG (sig) )
1511
1537
        {
1512
1538
          /* Note that this works based solely on the timestamps like
1513
1539
             the rest of gpg.  If the standard gets revocation
1536
1562
              else
1537
1563
                {
1538
1564
                  /* It's valid, so is it newer? */
1539
 
                  if (sig->timestamp >= bsdate) 
 
1565
                  if (sig->timestamp >= bsdate)
1540
1566
                    {
1541
1567
                      knode->flag |= 1;  /* The subkey is valid.  */
1542
1568
                      if (bsnode)
1543
1569
                        {
1544
1570
                          /* Delete the last binding sig since this
1545
1571
                             one is newer */
1546
 
                          bsnode->flag |= 4; 
 
1572
                          bsnode->flag |= 4;
1547
1573
                          if (opt.verbose)
1548
1574
                            log_info (_("key %s: removed multiple subkey"
1549
1575
                                        " binding\n"),keystr(keyid));
1550
1576
                        }
1551
 
                      
 
1577
 
1552
1578
                      bsnode = n;
1553
1579
                      bsdate = sig->timestamp;
1554
1580
                    }
1593
1619
                        {
1594
1620
                          /* Delete the last revocation sig since
1595
1621
                             this one is newer.  */
1596
 
                          rsnode->flag |= 4; 
 
1622
                          rsnode->flag |= 4;
1597
1623
                          if (opt.verbose)
1598
1624
                            log_info (_("key %s: removed multiple subkey"
1599
1625
                                        " revocation\n"),keystr(keyid));
1600
1626
                        }
1601
 
                      
 
1627
 
1602
1628
                      rsnode = n;
1603
1629
                      rsdate = sig->timestamp;
1604
1630
                    }
2339
2365
          PACKET *pkt = xmalloc_clear (sizeof *pkt);
2340
2366
          PKT_secret_key *sk = xmalloc_clear (sizeof *sk);
2341
2367
          int i, n;
2342
 
          
 
2368
 
2343
2369
          if (pubnode->pkt->pkttype == PKT_PUBLIC_KEY)
2344
2370
            pkt->pkttype = PKT_SECRET_KEY;
2345
2371
          else
2346
2372
            pkt->pkttype = PKT_SECRET_SUBKEY;
2347
 
          
 
2373
 
2348
2374
          pkt->pkt.secret_key = sk;
2349
2375
 
2350
2376
          copy_public_parts_to_secret_key ( pk, sk );
2351
2377
          sk->version     = pk->version;
2352
2378
          sk->timestamp   = pk->timestamp;
2353
 
        
 
2379
 
2354
2380
          n = pubkey_get_npkey (pk->pubkey_algo);
2355
2381
          if (!n)
2356
2382
            n = 1; /* Unknown number of parameters, however the data
2357
2383
                      is stored in the first mpi. */
2358
2384
          for (i=0; i < n; i++ )
2359
2385
            sk->skey[i] = mpi_copy (pk->pkey[i]);
2360
 
  
 
2386
 
2361
2387
          sk->is_protected = 1;
2362
2388
          sk->protect.s2k.mode = 1001;
2363
 
  
 
2389
 
2364
2390
          secnode = new_kbnode (pkt);
2365
2391
        }
2366
2392
      else
2367
2393
        {
2368
2394
          secnode = clone_kbnode (pubnode);
2369
2395
        }
2370
 
      
 
2396
 
2371
2397
      if(!sec_keyblock)
2372
2398
        sec_keyblock = secnode;
2373
2399
      else
2381
2407
/* Walk over the secret keyring SEC_KEYBLOCK and update any simple
2382
2408
   stub keys with the serial number SNNUM of the card if one of the
2383
2409
   fingerprints FPR1, FPR2 or FPR3 match.  Print a note if the key is
2384
 
   a duplicate (may happen in case of backed uped keys). 
2385
 
   
 
2410
   a duplicate (may happen in case of backed uped keys).
 
2411
 
2386
2412
   Returns: True if anything changed.
2387
2413
*/
2388
2414
static int
2389
 
update_sec_keyblock_with_cardinfo (KBNODE sec_keyblock, 
 
2415
update_sec_keyblock_with_cardinfo (KBNODE sec_keyblock,
2390
2416
                                   const unsigned char *fpr1,
2391
2417
                                   const unsigned char *fpr2,
2392
2418
                                   const unsigned char *fpr3,
2406
2432
          && node->pkt->pkttype != PKT_SECRET_SUBKEY)
2407
2433
        continue;
2408
2434
      sk = node->pkt->pkt.secret_key;
2409
 
      
 
2435
 
2410
2436
      fingerprint_from_sk (sk, array, &n);
2411
2437
      if (n != 20)
2412
2438
        continue; /* Can't be a card key.  */
2456
2482
   exists, add appropriate subkey stubs and update the secring.
2457
2483
   Return 0 if the key could be created. */
2458
2484
int
2459
 
auto_create_card_key_stub ( const char *serialnostr, 
 
2485
auto_create_card_key_stub ( const char *serialnostr,
2460
2486
                            const unsigned char *fpr1,
2461
2487
                            const unsigned char *fpr2,
2462
2488
                            const unsigned char *fpr3)
2467
2493
  int rc;
2468
2494
 
2469
2495
  /* We only want to do this for an OpenPGP card.  */
2470
 
  if (!serialnostr || strncmp (serialnostr, "D27600012401", 12) 
 
2496
  if (!serialnostr || strncmp (serialnostr, "D27600012401", 12)
2471
2497
      || strlen (serialnostr) != 32 )
2472
2498
    return G10ERR_GENERAL;
2473
2499
 
2478
2504
    ;
2479
2505
  else
2480
2506
    return G10ERR_GENERAL;
2481
 
 
 
2507
 
2482
2508
  hd = keydb_new (1);
2483
2509
 
2484
2510
  /* Now check whether there is a secret keyring.  */
2504
2530
      else
2505
2531
        {
2506
2532
          merge_keys_and_selfsig (sec_keyblock);
2507
 
          
 
2533
 
2508
2534
          /* FIXME: We need to add new subkeys first.  */
2509
2535
          if (update_sec_keyblock_with_cardinfo (sec_keyblock,
2510
2536
                                                 fpr1, fpr2, fpr3,
2538
2564
                       keydb_get_resource_name (hd), g10_errstr(rc) );
2539
2565
        }
2540
2566
    }
2541
 
    
 
2567
 
2542
2568
  release_kbnode (sec_keyblock);
2543
2569
  release_kbnode (pub_keyblock);
2544
2570
  keydb_release (hd);