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

« back to all changes in this revision

Viewing changes to g10/getkey.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
1
/* getkey.c -  Get a key from the database
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3
 
 *               2003 Free Software Foundation, Inc.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
 
3
 *               2006, 2007 Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of GnuPG.
6
6
 *
7
7
 * GnuPG is free software; you can redistribute it and/or modify
8
8
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * the Free Software Foundation; either version 3 of the License, or
10
10
 * (at your option) any later version.
11
11
 *
12
12
 * GnuPG is distributed in the hope that it will be useful,
15
15
 * GNU General Public License for more details.
16
16
 *
17
17
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20
19
 */
21
20
 
22
21
#include <config.h>
29
28
#include "gpg.h"
30
29
#include "util.h"
31
30
#include "packet.h"
32
 
#include "memory.h"
33
31
#include "iobuf.h"
34
32
#include "keydb.h"
35
33
#include "options.h"
36
34
#include "main.h"
37
35
#include "trustdb.h"
38
36
#include "i18n.h"
 
37
#include "keyserver-internal.h"
39
38
 
40
 
#define MAX_PK_CACHE_ENTRIES   200
41
 
#define MAX_UID_CACHE_ENTRIES  200
 
39
#define MAX_PK_CACHE_ENTRIES   PK_UID_CACHE_SIZE
 
40
#define MAX_UID_CACHE_ENTRIES  PK_UID_CACHE_SIZE
42
41
 
43
42
#if MAX_PK_CACHE_ENTRIES < 2
44
43
#error We need the cache for key creation
45
44
#endif
46
45
 
47
 
 
48
46
struct getkey_ctx_s {
49
47
    int exact;
50
48
    KBNODE keyblock;
154
152
        return;
155
153
    }
156
154
    pk_cache_entries++;
157
 
    ce = xmalloc ( sizeof *ce );
 
155
    ce = xmalloc( sizeof *ce );
158
156
    ce->next = pk_cache;
159
157
    pk_cache = ce;
160
158
    ce->pk = copy_public_key( NULL, pk );
164
162
}
165
163
 
166
164
 
 
165
/* Return a const utf-8 string with the text "[User ID not found]".
 
166
   This fucntion is required so that we don't need to switch gettext's
 
167
   encoding temporary. */
 
168
static const char *
 
169
user_id_not_found_utf8 (void)
 
170
{
 
171
  static char *text;
 
172
 
 
173
  if (!text)
 
174
    text = native_to_utf8 (_("[User ID not found]"));
 
175
  return text;
 
176
}
 
177
 
 
178
 
 
179
 
167
180
/*
168
181
 * Return the user ID from the given keyblock.
169
182
 * We use the primary uid flag which has been set by the merge_selfsigs
184
197
            return k->pkt->pkt.user_id->name;
185
198
        }
186
199
    } 
187
 
    /* fixme: returning translatable constants instead of a user ID is 
188
 
     * not good because they are probably not utf-8 encoded. */
189
 
    s = _("[User id not found]");
 
200
    s = user_id_not_found_utf8 ();
190
201
    *uidlen = strlen (s);
191
202
    return s;
192
203
}
218
229
    for (k=keyblock; k; k = k->next ) {
219
230
        if ( k->pkt->pkttype == PKT_PUBLIC_KEY
220
231
             || k->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
221
 
            keyid_list_t a = xcalloc (1, sizeof *a );
 
232
            keyid_list_t a = xmalloc_clear ( sizeof *a );
222
233
            /* Hmmm: For a long list of keyids it might be an advantage
223
234
             * to append the keys */
224
235
            keyid_from_pk( k->pkt->pkt.public_key, a->keyid );
252
263
        r = user_id_db;
253
264
        user_id_db = r->next;
254
265
        release_keyid_list ( r->keyids );
255
 
        xfree (r);
 
266
        xfree(r);
256
267
        uid_cache_entries--;
257
268
    }
258
 
    r = xmalloc ( sizeof *r + uidlen-1 );
 
269
    r = xmalloc( sizeof *r + uidlen-1 );
259
270
    r->keyids = keyids;
260
271
    r->len = uidlen;
261
272
    memcpy(r->name, uid, r->len);
275
286
        for( ce = pk_cache; ce; ce = ce2 ) {
276
287
            ce2 = ce->next;
277
288
            free_public_key( ce->pk );
278
 
            xfree ( ce );
 
289
            xfree( ce );
279
290
        }
280
291
        pk_cache_disabled=1;
281
292
        pk_cache_entries = 0;
322
333
    int rc = 0;
323
334
 
324
335
#if MAX_PK_CACHE_ENTRIES
325
 
    {   /* Try to get it from the cache */
 
336
    if(pk)
 
337
      {
 
338
        /* Try to get it from the cache.  We don't do this when pk is
 
339
           NULL as it does not guarantee that the user IDs are
 
340
           cached. */
326
341
        pk_cache_entry_t ce;
327
 
        for( ce = pk_cache; ce; ce = ce->next ) {
328
 
            if( ce->keyid[0] == keyid[0] && ce->keyid[1] == keyid[1] ) {
329
 
                if( pk )
330
 
                    copy_public_key( pk, ce->pk );
 
342
        for( ce = pk_cache; ce; ce = ce->next )
 
343
          {
 
344
            if( ce->keyid[0] == keyid[0] && ce->keyid[1] == keyid[1] )
 
345
              {
 
346
                copy_public_key( pk, ce->pk );
331
347
                return 0;
332
 
            }
333
 
        }
334
 
    }
 
348
              }
 
349
          }
 
350
      }
335
351
#endif
336
352
    /* more init stuff */
337
353
    if( !pk ) {
338
 
        pk = xcalloc (1, sizeof *pk );
 
354
        pk = xmalloc_clear( sizeof *pk );
339
355
        internal++;
340
356
    }
341
357
 
363
379
    if( !rc )
364
380
        goto leave;
365
381
 
366
 
    rc = GPG_ERR_NO_PUBKEY;
 
382
    rc = G10ERR_NO_PUBKEY;
367
383
 
368
384
  leave:
369
385
    if( !rc )
376
392
 
377
393
/* Get a public key and store it into the allocated pk.  This function
378
394
   differs from get_pubkey() in that it does not do a check of the key
379
 
   to avoid recursion.  It should be used only in very certain cases.  */
 
395
   to avoid recursion.  It should be used only in very certain cases.
 
396
   It will only retrieve primary keys. */
380
397
int
381
398
get_pubkey_fast (PKT_public_key *pk, u32 *keyid)
382
399
{
383
400
  int rc = 0;
384
401
  KEYDB_HANDLE hd;
385
402
  KBNODE keyblock;
 
403
  u32 pkid[2];
386
404
  
387
405
  assert (pk);
388
406
#if MAX_PK_CACHE_ENTRIES
406
424
  if (rc == -1)
407
425
    {
408
426
      keydb_release (hd);
409
 
      return GPG_ERR_NO_PUBKEY;
 
427
      return G10ERR_NO_PUBKEY;
410
428
    }
411
429
  rc = keydb_get_keyblock (hd, &keyblock);
412
430
  keydb_release (hd);
413
431
  if (rc) 
414
432
    {
415
 
      log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
416
 
      return GPG_ERR_NO_PUBKEY;
 
433
      log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc));
 
434
      return G10ERR_NO_PUBKEY;
417
435
    }
418
 
  
 
436
 
419
437
  assert ( keyblock->pkt->pkttype == PKT_PUBLIC_KEY
420
438
           ||  keyblock->pkt->pkttype == PKT_PUBLIC_SUBKEY );
421
 
  copy_public_key (pk, keyblock->pkt->pkt.public_key );
 
439
 
 
440
  keyid_from_pk(keyblock->pkt->pkt.public_key,pkid);
 
441
  if(keyid[0]==pkid[0] && keyid[1]==pkid[1])
 
442
    copy_public_key (pk, keyblock->pkt->pkt.public_key );
 
443
  else
 
444
    rc=G10ERR_NO_PUBKEY;
 
445
 
422
446
  release_kbnode (keyblock);
423
447
 
424
448
  /* Not caching key here since it won't have all of the fields
425
449
     properly set. */
426
450
 
427
 
  return 0;
 
451
  return rc;
428
452
}
429
453
 
430
454
 
431
 
 
432
455
KBNODE
433
456
get_pubkeyblock( u32 *keyid )
434
457
{
496
519
 * check and does not tell us whether the secret key is valid.  It
497
520
 * merely tells other whether there is some secret key.
498
521
 * Returns: 0 := key is available
499
 
 * GPG_ERR_NO_SECKEY := not availabe
 
522
 * G10ERR_NO_SECKEY := not availabe
500
523
 */
501
524
int
502
525
seckey_available( u32 *keyid )
506
529
 
507
530
    rc = keydb_search_kid (hd, keyid);
508
531
    if ( rc == -1 )
509
 
        rc = GPG_ERR_NO_SECKEY;
 
532
        rc = G10ERR_NO_SECKEY;
510
533
    keydb_release (hd);
511
534
    return rc;
512
535
}
553
576
 *   Words are delimited by white space or "()<>[]{}.@-+_,;/&!"
554
577
 *   (note that you can't search for these characters). Compare
555
578
 *   is not case sensitive.
 
579
 * - If the userid starts with a '&' a 40 hex digits keygrip is expected.
556
580
 */
557
581
 
558
582
int
579
603
        case 0:    /* empty string is an error */
580
604
            return 0;
581
605
 
 
606
#if 0
582
607
        case '.':  /* an email address, compare from end */
583
608
            mode = KEYDB_SEARCH_MODE_MAILEND;
584
609
            s++;
585
610
            desc->u.name = s;
586
611
            break;
 
612
#endif
587
613
 
588
614
        case '<':  /* an email address */
589
615
            mode = KEYDB_SEARCH_MODE_MAIL;
608
634
            desc->u.name = s;
609
635
            break;
610
636
 
 
637
#if 0
611
638
        case '+':  /* compare individual words */
612
639
            mode = KEYDB_SEARCH_MODE_WORDS;
613
640
            s++;
614
641
            desc->u.name = s;
615
642
            break;
 
643
#endif
616
644
 
617
645
        case '#':  /* local user id */
618
 
            return 0; /* This is now obsolete and van't not be used anymore*/
 
646
            return 0; /* This is now obsolete and can't not be used anymore*/
619
647
        
620
648
        case ':': /*Unified fingerprint */
621
649
            {  
640
668
            } 
641
669
            break;
642
670
           
 
671
        case '&':  /* keygrip */
 
672
          return 0; /* Not yet implememted. */
 
673
 
643
674
        default:
644
675
            if (s[0] == '0' && s[1] == 'x') {
645
676
                hexprefix = 1;
653
684
            }
654
685
 
655
686
            /* check if a hexadecimal number is terminated by EOS or blank */
656
 
            if (hexlength && s[hexlength] && !spacep (s+hexlength)) {
 
687
            if (hexlength && s[hexlength] && !spacep(s+hexlength)) {
657
688
                if (hexprefix)      /* a "0x" prefix without correct */
658
689
                    return 0;       /* termination is an error */
659
690
                else                /* The first chars looked like */
728
759
 
729
760
 
730
761
static int
731
 
skip_disabled(void *dummy,u32 *keyid)
 
762
skip_unusable(void *dummy,u32 *keyid,PKT_user_id *uid)
732
763
{
733
 
  int rc,disabled=0;
734
 
  PKT_public_key *pk=xcalloc (1,sizeof(PKT_public_key));
 
764
  int unusable=0;
 
765
  KBNODE keyblock;
735
766
 
736
 
  rc = get_pubkey(pk, keyid);
737
 
  if(rc)
 
767
  keyblock=get_pubkeyblock(keyid);
 
768
  if(!keyblock)
738
769
    {
739
 
      log_error("error checking disabled status of %08lX: %s\n",
740
 
                (ulong)keyid[1],gpg_strerror (rc));
 
770
      log_error("error checking usability status of %s\n",keystr(keyid));
741
771
      goto leave;
742
772
    }
743
 
 
744
 
  disabled=pk_is_disabled(pk);
 
773
 
 
774
  /* Is the user ID in question revoked/expired? */
 
775
  if(uid)
 
776
    {
 
777
      KBNODE node;
 
778
 
 
779
      for(node=keyblock;node;node=node->next)
 
780
        {
 
781
          if(node->pkt->pkttype==PKT_USER_ID)
 
782
            {
 
783
              if(cmp_user_ids(uid,node->pkt->pkt.user_id)==0
 
784
                 && (node->pkt->pkt.user_id->is_revoked
 
785
                     || node->pkt->pkt.user_id->is_expired))
 
786
                {
 
787
                  unusable=1;
 
788
                  break;
 
789
                }
 
790
            }
 
791
        }
 
792
    }
 
793
 
 
794
  if(!unusable)
 
795
    unusable=pk_is_disabled(keyblock->pkt->pkt.public_key);
745
796
 
746
797
 leave:
747
 
  free_public_key(pk);
748
 
  return disabled;
 
798
  release_kbnode(keyblock);
 
799
  return unusable;
749
800
}
750
801
 
751
802
/****************
752
803
 * Try to get the pubkey by the userid. This function looks for the
753
 
 * first pubkey certificate which has the given name in a user_id.
754
 
 * if pk/sk has the pubkey algo set, the function will only return
755
 
 * a pubkey with that algo.
756
 
 * The caller should provide storage for either the pk or the sk.
757
 
 * If ret_kb is not NULL the function will return the keyblock there.
 
804
 * first pubkey certificate which has the given name in a user_id.  if
 
805
 * pk/sk has the pubkey algo set, the function will only return a
 
806
 * pubkey with that algo.  If namelist is NULL, the first key is
 
807
 * returned.  The caller should provide storage for either the pk or
 
808
 * the sk.  If ret_kb is not NULL the function will return the
 
809
 * keyblock there.
758
810
 */
759
811
 
760
812
static int
761
 
key_byname( GETKEY_CTX *retctx, STRLIST namelist,
 
813
key_byname( GETKEY_CTX *retctx, strlist_t namelist,
762
814
            PKT_public_key *pk, PKT_secret_key *sk,
763
 
            int secmode, int include_disabled,
 
815
            int secmode, int include_unusable,
764
816
            KBNODE *ret_kb, KEYDB_HANDLE *ret_kdbhd )
765
817
{
766
818
    int rc = 0;
767
819
    int n;
768
 
    STRLIST r;
 
820
    strlist_t r;
769
821
    GETKEY_CTX ctx;
770
822
    KBNODE help_kb = NULL;
771
823
    
777
829
    if (ret_kdbhd)
778
830
        *ret_kdbhd = NULL;
779
831
 
780
 
    /* build the search context */
781
 
    for(n=0, r=namelist; r; r = r->next )
782
 
        n++;
783
 
    ctx = xcalloc (1,sizeof *ctx + (n-1)*sizeof ctx->items );
784
 
    ctx->nitems = n;
785
 
 
786
 
    for(n=0, r=namelist; r; r = r->next, n++ ) {
787
 
        classify_user_id (r->d, &ctx->items[n]);
 
832
    if(!namelist)
 
833
      {
 
834
        ctx = xmalloc_clear (sizeof *ctx);
 
835
        ctx->nitems = 1;
 
836
        ctx->items[0].mode=KEYDB_SEARCH_MODE_FIRST;
 
837
        if(!include_unusable)
 
838
          ctx->items[0].skipfnc=skip_unusable;
 
839
      }
 
840
    else
 
841
      {
 
842
        /* build the search context */
 
843
        for(n=0, r=namelist; r; r = r->next )
 
844
          n++;
 
845
 
 
846
        ctx = xmalloc_clear (sizeof *ctx + (n-1)*sizeof ctx->items );
 
847
        ctx->nitems = n;
 
848
 
 
849
        for(n=0, r=namelist; r; r = r->next, n++ )
 
850
          {
 
851
            classify_user_id (r->d, &ctx->items[n]);
788
852
        
789
 
        if (ctx->items[n].exact)
790
 
            ctx->exact = 1;
791
 
        if (!ctx->items[n].mode) {
792
 
            xfree (ctx);
793
 
            return GPG_ERR_INV_USER_ID;
794
 
        }
795
 
        if(!include_disabled
796
 
           && ctx->items[n].mode!=KEYDB_SEARCH_MODE_SHORT_KID
797
 
           && ctx->items[n].mode!=KEYDB_SEARCH_MODE_LONG_KID
798
 
           && ctx->items[n].mode!=KEYDB_SEARCH_MODE_FPR16
799
 
           && ctx->items[n].mode!=KEYDB_SEARCH_MODE_FPR20
800
 
           && ctx->items[n].mode!=KEYDB_SEARCH_MODE_FPR)
801
 
          ctx->items[n].skipfnc=skip_disabled;
802
 
    }
 
853
            if (ctx->items[n].exact)
 
854
              ctx->exact = 1;
 
855
            if (!ctx->items[n].mode)
 
856
              {
 
857
                xfree (ctx);
 
858
                return G10ERR_INV_USER_ID;
 
859
              }
 
860
            if(!include_unusable
 
861
               && ctx->items[n].mode!=KEYDB_SEARCH_MODE_SHORT_KID
 
862
               && ctx->items[n].mode!=KEYDB_SEARCH_MODE_LONG_KID
 
863
               && ctx->items[n].mode!=KEYDB_SEARCH_MODE_FPR16
 
864
               && ctx->items[n].mode!=KEYDB_SEARCH_MODE_FPR20
 
865
               && ctx->items[n].mode!=KEYDB_SEARCH_MODE_FPR)
 
866
              ctx->items[n].skipfnc=skip_unusable;
 
867
          }
 
868
      }
803
869
 
804
870
    ctx->kr_handle = keydb_new (secmode);
805
871
    if ( !ret_kb ) 
841
907
    return rc;
842
908
}
843
909
 
844
 
/*
845
 
 * Find a public key from NAME and returh the keyblock or the key.
846
 
 * If ret_kdb is not NULL, the KEYDB handle used to locate this keyblock is
847
 
 * returned and the caller is responsible for closing it.
848
 
 */
 
910
 
 
911
 
 
912
/* Find a public key from NAME and return the keyblock or the key.  If
 
913
   ret_kdb is not NULL, the KEYDB handle used to locate this keyblock
 
914
   is returned and the caller is responsible for closing it.  If a key
 
915
   was not found and NAME is a valid RFC822 mailbox and --auto-key-locate
 
916
   has been enabled, we try to import the key via the online mechanisms
 
917
   defined by --auto-key-locate.  */
849
918
int
850
919
get_pubkey_byname (PKT_public_key *pk,
851
920
                   const char *name, KBNODE *ret_keyblock,
852
 
                   KEYDB_HANDLE *ret_kdbhd, int include_disabled ) 
 
921
                   KEYDB_HANDLE *ret_kdbhd, int include_unusable )
853
922
{
854
 
    int rc;
855
 
    STRLIST namelist = NULL;
856
 
 
857
 
    add_to_strlist( &namelist, name );
858
 
    rc = key_byname( NULL, namelist, pk, NULL, 0,
859
 
                     include_disabled, ret_keyblock, ret_kdbhd);
860
 
    free_strlist( namelist );
861
 
    return rc;
 
923
  int rc;
 
924
  strlist_t namelist = NULL;
 
925
 
 
926
  add_to_strlist( &namelist, name );
 
927
 
 
928
  rc = key_byname( NULL, namelist, pk, NULL, 0,
 
929
                   include_unusable, ret_keyblock, ret_kdbhd);
 
930
 
 
931
  /* If the requested name resembles a valid mailbox and automatic
 
932
     retrieval has been enabled, we try to import the key. */
 
933
 
 
934
  if (rc == G10ERR_NO_PUBKEY && is_valid_mailbox(name))
 
935
    {
 
936
      struct akl *akl;
 
937
 
 
938
      for(akl=opt.auto_key_locate;akl;akl=akl->next)
 
939
        {
 
940
          unsigned char *fpr=NULL;
 
941
          size_t fpr_len;
 
942
 
 
943
          switch(akl->type)
 
944
            {
 
945
            case AKL_CERT:
 
946
              glo_ctrl.in_auto_key_retrieve++;
 
947
              rc=keyserver_import_cert(name,&fpr,&fpr_len);
 
948
              glo_ctrl.in_auto_key_retrieve--;
 
949
 
 
950
              if(rc==0)
 
951
                log_info(_("automatically retrieved `%s' via %s\n"),
 
952
                         name,"DNS CERT");
 
953
              break;
 
954
 
 
955
            case AKL_PKA:
 
956
              glo_ctrl.in_auto_key_retrieve++;
 
957
              rc=keyserver_import_pka(name,&fpr,&fpr_len);
 
958
              glo_ctrl.in_auto_key_retrieve--;
 
959
 
 
960
              if(rc==0)
 
961
                log_info(_("automatically retrieved `%s' via %s\n"),
 
962
                         name,"PKA");
 
963
              break;
 
964
 
 
965
            case AKL_LDAP:
 
966
              glo_ctrl.in_auto_key_retrieve++;
 
967
              rc=keyserver_import_ldap(name,&fpr,&fpr_len);
 
968
              glo_ctrl.in_auto_key_retrieve--;
 
969
 
 
970
              if(rc==0)
 
971
                log_info(_("automatically retrieved `%s' via %s\n"),
 
972
                         name,"LDAP");
 
973
              break;
 
974
 
 
975
            case AKL_KEYSERVER:
 
976
              /* Strictly speaking, we don't need to only use a valid
 
977
                 mailbox for the getname search, but it helps cut down
 
978
                 on the problem of searching for something like "john"
 
979
                 and getting a whole lot of keys back. */
 
980
              if(opt.keyserver)
 
981
                {
 
982
                  glo_ctrl.in_auto_key_retrieve++;
 
983
                  rc=keyserver_import_name(name,&fpr,&fpr_len,opt.keyserver);
 
984
                  glo_ctrl.in_auto_key_retrieve--;
 
985
 
 
986
                  if(rc==0)
 
987
                    log_info(_("automatically retrieved `%s' via %s\n"),
 
988
                             name,opt.keyserver->uri);
 
989
                }
 
990
              break;
 
991
 
 
992
            case AKL_SPEC:
 
993
              {
 
994
                struct keyserver_spec *keyserver;
 
995
 
 
996
                keyserver=keyserver_match(akl->spec);
 
997
                glo_ctrl.in_auto_key_retrieve++;
 
998
                rc=keyserver_import_name(name,&fpr,&fpr_len,keyserver);
 
999
                glo_ctrl.in_auto_key_retrieve--;
 
1000
 
 
1001
                if(rc==0)
 
1002
                  log_info(_("automatically retrieved `%s' via %s\n"),
 
1003
                           name,akl->spec->uri);
 
1004
              }
 
1005
              break;
 
1006
            }
 
1007
 
 
1008
          /* Use the fingerprint of the key that we actually fetched.
 
1009
             This helps prevent problems where the key that we fetched
 
1010
             doesn't have the same name that we used to fetch it.  In
 
1011
             the case of CERT and PKA, this is an actual security
 
1012
             requirement as the URL might point to a key put in by an
 
1013
             attacker.  By forcing the use of the fingerprint, we
 
1014
             won't use the attacker's key here. */
 
1015
          if(rc==0 && fpr)
 
1016
            {
 
1017
              int i;
 
1018
              char fpr_string[MAX_FINGERPRINT_LEN*2+1];
 
1019
 
 
1020
              assert(fpr_len<=MAX_FINGERPRINT_LEN);
 
1021
 
 
1022
              free_strlist(namelist);
 
1023
              namelist=NULL;
 
1024
 
 
1025
              for(i=0;i<fpr_len;i++)
 
1026
                sprintf(fpr_string+2*i,"%02X",fpr[i]);
 
1027
 
 
1028
              if(opt.verbose)
 
1029
                log_info("auto-key-locate found fingerprint %s\n",fpr_string);
 
1030
 
 
1031
              add_to_strlist( &namelist, fpr_string );
 
1032
 
 
1033
              xfree(fpr);
 
1034
            }
 
1035
 
 
1036
          rc = key_byname( NULL, namelist, pk, NULL, 0,
 
1037
                           include_unusable, ret_keyblock, ret_kdbhd);
 
1038
          if(rc!=G10ERR_NO_PUBKEY)
 
1039
            break;
 
1040
        }
 
1041
    }
 
1042
 
 
1043
  free_strlist( namelist );
 
1044
  return rc;
862
1045
}
863
1046
 
864
1047
int
865
1048
get_pubkey_bynames( GETKEY_CTX *retctx, PKT_public_key *pk,
866
 
                    STRLIST names, KBNODE *ret_keyblock )
 
1049
                    strlist_t names, KBNODE *ret_keyblock )
867
1050
{
868
1051
    return key_byname( retctx, names, pk, NULL, 0, 1, ret_keyblock, NULL);
869
1052
}
880
1063
    return rc;
881
1064
}
882
1065
 
883
 
 
884
1066
void
885
1067
get_pubkey_end( GETKEY_CTX ctx )
886
1068
{
888
1070
        memset (&ctx->kbpos, 0, sizeof ctx->kbpos);
889
1071
        keydb_release (ctx->kr_handle);
890
1072
        if( !ctx->not_allocated )
891
 
            xfree ( ctx );
 
1073
            xfree( ctx );
892
1074
    }
893
1075
}
894
1076
 
895
1077
 
896
 
 
897
 
 
898
1078
/****************
899
1079
 * Search for a key with the given fingerprint.
900
1080
 * FIXME:
901
 
 * We should replace this with the _byname function.  This can be done
 
1081
 * We should replace this with the _byname function.  Thiscsan be done
902
1082
 * by creating a userID conforming to the unified fingerprint style. 
903
1083
 */
904
1084
int
926
1106
        get_pubkey_end( &ctx );
927
1107
    }
928
1108
    else
929
 
        rc = GPG_ERR_GENERAL; /* Oops */
 
1109
        rc = G10ERR_GENERAL; /* Oops */
930
1110
    return rc;
931
1111
}
932
1112
 
956
1136
  if (rc == -1)
957
1137
    {
958
1138
      keydb_release (hd);
959
 
      return GPG_ERR_NO_PUBKEY;
 
1139
      return G10ERR_NO_PUBKEY;
960
1140
    }
961
1141
  rc = keydb_get_keyblock (hd, &keyblock);
962
1142
  keydb_release (hd);
963
1143
  if (rc) 
964
1144
    {
965
 
      log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
966
 
      return GPG_ERR_NO_PUBKEY;
 
1145
      log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc));
 
1146
      return G10ERR_NO_PUBKEY;
967
1147
    }
968
1148
  
969
1149
  assert ( keyblock->pkt->pkttype == PKT_PUBLIC_KEY
1002
1182
        get_pubkey_end( &ctx );
1003
1183
    }
1004
1184
    else
1005
 
        rc = GPG_ERR_GENERAL; /* Oops */
 
1185
        rc = G10ERR_GENERAL; /* Oops */
1006
1186
 
1007
1187
    return rc;
1008
1188
}
1014
1194
 */
1015
1195
static int
1016
1196
get_seckey_byname2( GETKEY_CTX *retctx,
1017
 
                   PKT_secret_key *sk, const char *name, int unprotect,
1018
 
                   KBNODE *retblock )
 
1197
                    PKT_secret_key *sk, const char *name, int unprotect,
 
1198
                    KBNODE *retblock )
1019
1199
{
1020
 
    STRLIST namelist = NULL;
1021
 
    int rc;
1022
 
 
1023
 
    if( !name && opt.def_secret_key && *opt.def_secret_key ) {
1024
 
        add_to_strlist( &namelist, opt.def_secret_key );
1025
 
        rc = key_byname( retctx, namelist, NULL, sk, 1, 1, retblock, NULL );
1026
 
    }
1027
 
    else if( !name ) { /* use the first one as default key */
1028
 
        struct getkey_ctx_s ctx;
1029
 
        KBNODE kb = NULL;
1030
 
 
1031
 
        assert (!retctx ); /* do we need this at all */
1032
 
        assert (!retblock);
1033
 
        memset( &ctx, 0, sizeof ctx );
1034
 
        ctx.not_allocated = 1;
1035
 
        ctx.kr_handle = keydb_new (1);
1036
 
        ctx.nitems = 1;
1037
 
        ctx.items[0].mode = KEYDB_SEARCH_MODE_FIRST;
1038
 
        rc = lookup( &ctx, &kb, 1 );
1039
 
        if (!rc && sk )
1040
 
            sk_from_block ( &ctx, sk, kb );
1041
 
        release_kbnode ( kb );
1042
 
        get_seckey_end( &ctx );
1043
 
    }
1044
 
    else {
1045
 
        add_to_strlist( &namelist, name );
1046
 
        rc = key_byname( retctx, namelist, NULL, sk, 1, 1, retblock, NULL );
1047
 
    }
1048
 
 
1049
 
    free_strlist( namelist );
1050
 
 
1051
 
    if( !rc && unprotect )
1052
 
        rc = check_secret_key( sk, 0 );
1053
 
 
1054
 
    return rc;
 
1200
  strlist_t namelist = NULL;
 
1201
  int rc,include_unusable=1;
 
1202
 
 
1203
  /* If we have no name, try to use the default secret key.  If we
 
1204
     have no default, we'll use the first usable one. */
 
1205
 
 
1206
  if( !name && opt.def_secret_key && *opt.def_secret_key )
 
1207
    add_to_strlist( &namelist, opt.def_secret_key );
 
1208
  else if(name)
 
1209
    add_to_strlist( &namelist, name );
 
1210
  else
 
1211
    include_unusable=0;
 
1212
 
 
1213
  rc = key_byname( retctx, namelist, NULL, sk, 1, include_unusable,
 
1214
                   retblock, NULL );
 
1215
 
 
1216
  free_strlist( namelist );
 
1217
 
 
1218
  if( !rc && unprotect )
 
1219
    rc = check_secret_key( sk, 0 );
 
1220
 
 
1221
  return rc;
1055
1222
}
1056
1223
 
1057
1224
int 
1063
1230
 
1064
1231
int
1065
1232
get_seckey_bynames( GETKEY_CTX *retctx, PKT_secret_key *sk,
1066
 
                    STRLIST names, KBNODE *ret_keyblock )
 
1233
                    strlist_t names, KBNODE *ret_keyblock )
1067
1234
{
1068
1235
    return key_byname( retctx, names, NULL, sk, 1, 1, ret_keyblock, NULL );
1069
1236
}
1117
1284
        if (!rc && sk )
1118
1285
            sk_from_block ( &ctx, sk, kb );
1119
1286
        release_kbnode ( kb );
1120
 
        get_pubkey_end( &ctx );
 
1287
        get_seckey_end( &ctx );
1121
1288
    }
1122
1289
    else
1123
 
        rc = GPG_ERR_GENERAL; /* Oops */
 
1290
        rc = G10ERR_GENERAL; /* Oops */
1124
1291
    return rc;
1125
1292
}
1126
1293
 
 
1294
 
 
1295
/* Search for a secret key with the given fingerprint and return the
 
1296
   complete keyblock which may have more than only this key. */
 
1297
int
 
1298
get_seckeyblock_byfprint (KBNODE *ret_keyblock, const byte *fprint,
 
1299
                          size_t fprint_len )
 
1300
{
 
1301
  int rc;
 
1302
  struct getkey_ctx_s ctx;
 
1303
  
 
1304
  if (fprint_len != 20 && fprint_len == 16)
 
1305
    return G10ERR_GENERAL; /* Oops */
 
1306
    
 
1307
  memset (&ctx, 0, sizeof ctx);
 
1308
  ctx.not_allocated = 1;
 
1309
  ctx.kr_handle = keydb_new (1);
 
1310
  ctx.nitems = 1;
 
1311
  ctx.items[0].mode = (fprint_len==16
 
1312
                       ? KEYDB_SEARCH_MODE_FPR16
 
1313
                       : KEYDB_SEARCH_MODE_FPR20);
 
1314
  memcpy (ctx.items[0].u.fpr, fprint, fprint_len);
 
1315
  rc = lookup (&ctx, ret_keyblock, 1);
 
1316
  get_seckey_end (&ctx);
 
1317
  
 
1318
  return rc;
 
1319
}
 
1320
 
 
1321
 
1127
1322
 
1128
1323
/************************************************
1129
1324
 ************* Merging stuff ********************
1220
1415
    }
1221
1416
}
1222
1417
 
 
1418
static int
 
1419
parse_key_usage(PKT_signature *sig)
 
1420
{
 
1421
  int key_usage=0;
 
1422
  const byte *p;
 
1423
  size_t n;
 
1424
  byte flags;
 
1425
 
 
1426
  p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_KEY_FLAGS,&n);
 
1427
  if(p && n)
 
1428
    {
 
1429
      /* first octet of the keyflags */
 
1430
      flags=*p;
 
1431
 
 
1432
      if(flags & 1)
 
1433
        {
 
1434
          key_usage |= PUBKEY_USAGE_CERT;
 
1435
          flags&=~1;
 
1436
        }
 
1437
 
 
1438
      if(flags & 2)
 
1439
        {
 
1440
          key_usage |= PUBKEY_USAGE_SIG;
 
1441
          flags&=~2;
 
1442
        }
 
1443
 
 
1444
      /* We do not distinguish between encrypting communications and
 
1445
         encrypting storage. */
 
1446
      if(flags & (0x04|0x08))
 
1447
        {
 
1448
          key_usage |= PUBKEY_USAGE_ENC;
 
1449
          flags&=~(0x04|0x08);
 
1450
        }
 
1451
 
 
1452
      if(flags & 0x20)
 
1453
        {
 
1454
          key_usage |= PUBKEY_USAGE_AUTH;
 
1455
          flags&=~0x20;
 
1456
        }
 
1457
 
 
1458
      if(flags)
 
1459
        key_usage |= PUBKEY_USAGE_UNKNOWN;
 
1460
    }
 
1461
 
 
1462
  /* We set PUBKEY_USAGE_UNKNOWN to indicate that this key has a
 
1463
     capability that we do not handle.  This serves to distinguish
 
1464
     between a zero key usage which we handle as the default
 
1465
     capabilities for that algorithm, and a usage that we do not
 
1466
     handle. */
 
1467
 
 
1468
  return key_usage;
 
1469
}
 
1470
 
1223
1471
/*
1224
1472
 * Apply information from SIGNODE (which is the valid self-signature
1225
1473
 * associated with that UID) to the UIDNODE:
1238
1486
    const byte *p, *sym, *hash, *zip;
1239
1487
    size_t n, nsym, nhash, nzip;
1240
1488
 
 
1489
    sig->flags.chosen_selfsig = 1; /* we chose this one */
1241
1490
    uid->created = 0; /* not created == invalid */
1242
 
    if ( IS_UID_REV ( sig ) ) {
 
1491
    if ( IS_UID_REV ( sig ) ) 
 
1492
      {
1243
1493
        uid->is_revoked = 1;
1244
1494
        return; /* has been revoked */
1245
 
    }
 
1495
      }
 
1496
    else
 
1497
      uid->is_revoked = 0;
 
1498
 
 
1499
    uid->expiredate = sig->expiredate;
 
1500
 
 
1501
    if (sig->flags.expired)
 
1502
      {
 
1503
        uid->is_expired = 1;
 
1504
        return; /* has expired */
 
1505
      }
 
1506
    else
 
1507
      uid->is_expired = 0;
1246
1508
 
1247
1509
    uid->created = sig->timestamp; /* this one is okay */
1248
1510
    uid->selfsigversion = sig->version;
1249
1511
    /* If we got this far, it's not expired :) */
1250
1512
    uid->is_expired = 0;
1251
 
    uid->expiredate = sig->expiredate;
1252
1513
 
1253
1514
    /* store the key flags in the helper variable for later processing */
1254
 
    uid->help_key_usage = 0;
1255
 
    p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_KEY_FLAGS, &n );
1256
 
    if ( p && n ) {
1257
 
        /* first octet of the keyflags */   
1258
 
        if ( (*p & 0x03) )
1259
 
            uid->help_key_usage |= PUBKEY_USAGE_SIG;
1260
 
        if ( (*p & 0x0c) )    
1261
 
            uid->help_key_usage |= PUBKEY_USAGE_ENC;
1262
 
        /* Note: we do not set the CERT flag here because it can be assumed
1263
 
         * that thre is no real policy to set it. */
1264
 
        if ( (*p & 0x20) )    
1265
 
            uid->help_key_usage |= PUBKEY_USAGE_AUTH;
1266
 
    }
 
1515
    uid->help_key_usage=parse_key_usage(sig);
1267
1516
 
1268
 
    /* ditto or the key expiration */
1269
 
    uid->help_key_expire = 0;
 
1517
    /* ditto for the key expiration */
1270
1518
    p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_KEY_EXPIRE, NULL);
1271
 
    if ( p ) { 
1272
 
        uid->help_key_expire = keycreated + buffer_to_u32(p);
1273
 
    }
 
1519
    if( p && buffer_to_u32(p) )
 
1520
      uid->help_key_expire = keycreated + buffer_to_u32(p);
 
1521
    else
 
1522
      uid->help_key_expire = 0;
1274
1523
 
1275
1524
    /* Set the primary user ID flag - we will later wipe out some
1276
1525
     * of them to only have one in our keyblock */
1318
1567
    }
1319
1568
 
1320
1569
    /* see whether we have the MDC feature */
1321
 
    uid->mdc_feature = 0;
 
1570
    uid->flags.mdc = 0;
1322
1571
    p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_FEATURES, &n);
1323
1572
    if (p && n && (p[0] & 0x01))
1324
 
        uid->mdc_feature = 1;
 
1573
        uid->flags.mdc = 1;
1325
1574
 
1326
1575
    /* and the keyserver modify flag */
1327
 
    uid->ks_modify = 1;
 
1576
    uid->flags.ks_modify = 1;
1328
1577
    p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_KS_FLAGS, &n);
1329
1578
    if (p && n && (p[0] & 0x80))
1330
 
        uid->ks_modify = 0;
1331
 
}
1332
 
 
1333
 
static void
1334
 
merge_selfsigs_main( KBNODE keyblock, int *r_revoked )
 
1579
        uid->flags.ks_modify = 0;
 
1580
}
 
1581
 
 
1582
static void
 
1583
sig_to_revoke_info(PKT_signature *sig,struct revoke_info *rinfo)
 
1584
{
 
1585
  rinfo->date = sig->timestamp;
 
1586
  rinfo->algo = sig->pubkey_algo;
 
1587
  rinfo->keyid[0] = sig->keyid[0];
 
1588
  rinfo->keyid[1] = sig->keyid[1];
 
1589
}
 
1590
 
 
1591
static void
 
1592
merge_selfsigs_main(KBNODE keyblock, int *r_revoked, struct revoke_info *rinfo)
1335
1593
{
1336
1594
    PKT_public_key *pk = NULL;
1337
1595
    KBNODE k;
1346
1604
    byte sigversion = 0;
1347
1605
 
1348
1606
    *r_revoked = 0;
 
1607
    memset(rinfo,0,sizeof(*rinfo));
 
1608
 
1349
1609
    if ( keyblock->pkt->pkttype != PKT_PUBLIC_KEY )
1350
1610
        BUG ();
1351
1611
    pk = keyblock->pkt->pkt.public_key;
1368
1628
     */
1369
1629
 
1370
1630
    /* In case this key was already merged */
1371
 
    xfree (pk->revkey);
 
1631
    xfree(pk->revkey);
1372
1632
    pk->revkey=NULL;
1373
1633
    pk->numrevkeys=0;
1374
1634
 
1391
1651
                     * that key.
1392
1652
                     */ 
1393
1653
                    *r_revoked = 1;
 
1654
                    sig_to_revoke_info(sig,rinfo);
1394
1655
                }
1395
1656
                else if ( IS_KEY_SIG (sig) ) {
1396
1657
                  /* Add any revocation keys onto the pk.  This is
1459
1720
                               pk->numrevkeys*sizeof(struct revocation_key));
1460
1721
      }
1461
1722
 
1462
 
    if ( signode ) {
 
1723
    if ( signode )
 
1724
      {
1463
1725
        /* some information from a direct key signature take precedence
1464
1726
         * over the same information given in UID sigs.
1465
1727
         */
1466
1728
        PKT_signature *sig = signode->pkt->pkt.signature;
1467
1729
        const byte *p;
1468
 
        size_t n;
1469
 
        
1470
 
        p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_KEY_FLAGS, &n );
1471
 
        if ( p && n ) {
1472
 
            /* first octet of the keyflags */   
1473
 
            if ( (*p & 0x03) )
1474
 
                key_usage |= PUBKEY_USAGE_SIG;
1475
 
            if ( (*p & 0x0c) )    
1476
 
                key_usage |= PUBKEY_USAGE_ENC;
1477
 
            if ( (*p & 0x20) )    
1478
 
                key_usage |= PUBKEY_USAGE_AUTH;
1479
 
        }
 
1730
 
 
1731
        key_usage=parse_key_usage(sig);
1480
1732
 
1481
1733
        p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_KEY_EXPIRE, NULL);
1482
 
        if ( p ) {
1483
 
          key_expire = keytimestamp + buffer_to_u32(p);
1484
 
          key_expire_seen = 1;
1485
 
        }
 
1734
        if( p && buffer_to_u32(p) )
 
1735
          {
 
1736
            key_expire = keytimestamp + buffer_to_u32(p);
 
1737
            key_expire_seen = 1;
 
1738
          }
1486
1739
 
1487
1740
        /* mark that key as valid: one direct key signature should 
1488
1741
         * render a key as valid */
1489
1742
        pk->is_valid = 1;
1490
 
    }
 
1743
      }
1491
1744
 
1492
1745
    /* pass 1.5: look for key revocation signatures that were not made
1493
1746
       by the key (i.e. did a revocation key issue a revocation for
1494
1747
       us?).  Only bother to do this if there is a revocation key in
1495
 
       the first place. */
 
1748
       the first place and we're not revoked already. */
1496
1749
 
1497
 
    if(pk->revkey)
 
1750
    if(!*r_revoked && pk->revkey)
1498
1751
      for(k=keyblock; k && k->pkt->pkttype != PKT_USER_ID; k = k->next )
1499
1752
        {
1500
1753
          if ( k->pkt->pkttype == PKT_SIGNATURE )
1504
1757
              if(IS_KEY_REV(sig) &&
1505
1758
                 (sig->keyid[0]!=kid[0] || sig->keyid[1]!=kid[1]))
1506
1759
                { 
1507
 
                  /* Failure here means the sig did not verify, is was
 
1760
                  int rc=check_revocation_keys(pk,sig);
 
1761
                  if(rc==0)
 
1762
                    {
 
1763
                      *r_revoked=2;
 
1764
                      sig_to_revoke_info(sig,rinfo);
 
1765
                      /* don't continue checking since we can't be any
 
1766
                         more revoked than this */
 
1767
                      break;
 
1768
                    }
 
1769
                  else if(rc==G10ERR_NO_PUBKEY)
 
1770
                    pk->maybe_revoked=1;
 
1771
 
 
1772
                  /* A failure here means the sig did not verify, was
1508
1773
                     not issued by a revocation key, or a revocation
1509
 
                     key loop was broken. */
1510
 
 
1511
 
                  if(check_revocation_keys(pk,sig)==0)
1512
 
                    *r_revoked=1;
1513
 
 
1514
 
                  /* In the future handle subkey and cert revocations?
1515
 
                     PGP doesn't, but it's in 2440. */
 
1774
                     key loop was broken.  If a revocation key isn't
 
1775
                     findable, however, the key might be revoked and
 
1776
                     we don't know it. */
 
1777
 
 
1778
                  /* TODO: In the future handle subkey and cert
 
1779
                     revocations?  PGP doesn't, but it's in 2440. */
1516
1780
                }
1517
1781
            }
1518
1782
        }
1537
1801
                if ( check_key_signature( keyblock, k, NULL ) )
1538
1802
                    ; /* signature did not verify */
1539
1803
                else if ( (IS_UID_SIG (sig) || IS_UID_REV (sig))
1540
 
                          && sig->timestamp >= sigdate ) {
 
1804
                          && sig->timestamp >= sigdate )
 
1805
                  {
1541
1806
                    /* Note: we allow to invalidate cert revocations
1542
1807
                     * by a newer signature.  An attacker can't use this
1543
1808
                     * because a key should be revoced with a key revocation.
1546
1811
                     * the same email address may become valid again (hired,
1547
1812
                     * fired, hired again).
1548
1813
                     */
1549
 
                    if(sig->flags.expired) {
1550
 
                      /* Expired uids don't get to be primary unless
1551
 
                         they are the only uid there is. */
1552
 
                      uidnode->pkt->pkt.user_id->is_primary=0;
1553
 
                      uidnode->pkt->pkt.user_id->is_expired=1;
1554
 
                      uidnode->pkt->pkt.user_id->expiredate=sig->expiredate;
1555
 
                    }
1556
 
                    else {
1557
 
                        sigdate = sig->timestamp;
1558
 
                        signode = k;
1559
 
                        if( sig->version > sigversion )
1560
 
                          sigversion = sig->version;
1561
 
                    }
1562
 
                }
 
1814
 
 
1815
                    sigdate = sig->timestamp;
 
1816
                    signode = k;
 
1817
                    signode->pkt->pkt.signature->flags.chosen_selfsig=0;
 
1818
                    if( sig->version > sigversion )
 
1819
                      sigversion = sig->version;
 
1820
                  }
1563
1821
            }
1564
1822
        }
1565
1823
    }
1573
1831
    if(!pk->is_valid && opt.allow_non_selfsigned_uid)
1574
1832
      {
1575
1833
        if(opt.verbose)
1576
 
          log_info(_("Invalid key %08lX made valid by "
1577
 
                     "--allow-non-selfsigned-uid\n"),
1578
 
                   (ulong)keyid_from_pk(pk,NULL));
1579
 
 
 
1834
          log_info(_("Invalid key %s made valid by"
 
1835
                     " --allow-non-selfsigned-uid\n"),keystr_from_pk(pk));
1580
1836
        pk->is_valid = 1;
1581
1837
      }
1582
1838
 
1598
1854
                  {
1599
1855
                    PKT_public_key *ultimate_pk;
1600
1856
 
1601
 
                    ultimate_pk=xcalloc (1,sizeof(*ultimate_pk));
 
1857
                    ultimate_pk=xmalloc_clear(sizeof(*ultimate_pk));
1602
1858
 
1603
1859
                    /* We don't want to use the full get_pubkey to
1604
1860
                       avoid infinite recursion in certain cases.
1608
1864
                       ultimate trust flag.  */
1609
1865
                    if(get_pubkey_fast(ultimate_pk,sig->keyid)==0
1610
1866
                       && check_key_signature2(keyblock,k,ultimate_pk,
1611
 
                                               NULL, NULL, NULL, NULL)==0
 
1867
                                               NULL,NULL,NULL,NULL)==0
1612
1868
                       && get_ownertrust(ultimate_pk)==TRUST_ULTIMATE)
1613
1869
                      {
1614
1870
                        free_public_key(ultimate_pk);
1659
1915
        if ( x ) /* mask it down to the actual allowed usage */
1660
1916
            key_usage &= x; 
1661
1917
    }
1662
 
    pk->pubkey_usage = key_usage;
 
1918
 
 
1919
    /* Whatever happens, it's a primary key, so it can certify. */
 
1920
    pk->pubkey_usage = key_usage|PUBKEY_USAGE_CERT;
1663
1921
 
1664
1922
    if ( !key_expire_seen ) {
1665
1923
        /* find the latest valid user ID with a key expiration set 
1799
2057
    u32 keytimestamp = 0;
1800
2058
    u32 key_expire = 0;
1801
2059
    const byte *p;
1802
 
    size_t n;
1803
2060
 
1804
2061
    if ( subnode->pkt->pkttype != PKT_PUBLIC_SUBKEY )
1805
2062
        BUG ();
1834
2091
                     problem is in the distribution.  Plus, PGP (7)
1835
2092
                     does this the same way.  */
1836
2093
                    subpk->is_revoked = 1;
 
2094
                    sig_to_revoke_info(sig,&subpk->revoked);
1837
2095
                    /* although we could stop now, we continue to 
1838
2096
                     * figure out other information like the old expiration
1839
2097
                     * time */
1840
2098
                }
1841
 
                else if ( IS_SUBKEY_SIG (sig) && sig->timestamp >= sigdate ) {
 
2099
                else if ( IS_SUBKEY_SIG (sig) && sig->timestamp >= sigdate )
 
2100
                  {
1842
2101
                    if(sig->flags.expired)
1843
 
                        ; /* signature has expired - ignore it */
1844
 
                    else {
 
2102
                      ; /* signature has expired - ignore it */
 
2103
                    else
 
2104
                      {
1845
2105
                        sigdate = sig->timestamp;
1846
2106
                        signode = k;
1847
 
                    }
1848
 
                }
 
2107
                        signode->pkt->pkt.signature->flags.chosen_selfsig=0;
 
2108
                      }
 
2109
                  }
1849
2110
            }
1850
2111
        }
1851
2112
    }
1852
2113
 
1853
 
    if ( !signode ) {
1854
 
        return;  /* no valid key binding */
1855
 
    }
 
2114
    /* no valid key binding */
 
2115
    if ( !signode )
 
2116
      return;
1856
2117
 
1857
 
    subpk->is_valid = 1;
1858
2118
    sig = signode->pkt->pkt.signature;
1859
 
        
1860
 
    p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_KEY_FLAGS, &n );
1861
 
    if ( p && n ) {
1862
 
        /* first octet of the keyflags */   
1863
 
        if ( (*p & 0x03) )
1864
 
            key_usage |= PUBKEY_USAGE_SIG;
1865
 
        if ( (*p & 0x0c) )    
1866
 
            key_usage |= PUBKEY_USAGE_ENC;
1867
 
        if ( (*p & 0x20) )    
1868
 
            key_usage |= PUBKEY_USAGE_AUTH;
1869
 
    }
1870
 
    if ( !key_usage ) { /* no key flags at all: get it from the algo */
 
2119
    sig->flags.chosen_selfsig=1; /* so we know which selfsig we chose later */
 
2120
 
 
2121
    key_usage=parse_key_usage(sig);
 
2122
    if ( !key_usage )
 
2123
      {
 
2124
        /* no key flags at all: get it from the algo */
1871
2125
        key_usage = openpgp_pk_algo_usage ( subpk->pubkey_algo );
1872
 
    }
1873
 
    else { /* check that the usage matches the usage as given by the algo */
 
2126
      }
 
2127
    else
 
2128
      {
 
2129
        /* check that the usage matches the usage as given by the algo */
1874
2130
        int x = openpgp_pk_algo_usage ( subpk->pubkey_algo );
1875
2131
        if ( x ) /* mask it down to the actual allowed usage */
1876
 
            key_usage &= x; 
1877
 
    }
 
2132
          key_usage &= x; 
 
2133
      }
 
2134
 
1878
2135
    subpk->pubkey_usage = key_usage;
1879
2136
    
1880
2137
    p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_KEY_EXPIRE, NULL);
1881
 
    if ( p ) 
 
2138
    if ( p && buffer_to_u32(p) )
1882
2139
        key_expire = keytimestamp + buffer_to_u32(p);
1883
2140
    else
1884
2141
        key_expire = 0;
1885
2142
    subpk->has_expired = key_expire >= curtime? 0 : key_expire;
1886
2143
    subpk->expiredate = key_expire;
 
2144
 
 
2145
    /* algo doesn't exist */
 
2146
    if(openpgp_pk_test_algo(subpk->pubkey_algo))
 
2147
      return;
 
2148
 
 
2149
    subpk->is_valid = 1;
 
2150
 
 
2151
    /* Find the first 0x19 embedded signature on our self-sig. */
 
2152
    if(subpk->backsig==0)
 
2153
      {
 
2154
        int seq=0;
 
2155
        size_t n;
 
2156
 
 
2157
        /* We do this while() since there may be other embedded
 
2158
           signatures in the future.  We only want 0x19 here. */
 
2159
        while((p=enum_sig_subpkt(sig->hashed,
 
2160
                                 SIGSUBPKT_SIGNATURE,&n,&seq,NULL)))
 
2161
          if(n>3 && ((p[0]==3 && p[2]==0x19) || (p[0]==4 && p[1]==0x19)))
 
2162
            break;
 
2163
 
 
2164
        if(p==NULL)
 
2165
          {
 
2166
            seq=0;
 
2167
            /* It is safe to have this in the unhashed area since the
 
2168
               0x19 is located on the selfsig for convenience, not
 
2169
               security. */
 
2170
            while((p=enum_sig_subpkt(sig->unhashed,SIGSUBPKT_SIGNATURE,
 
2171
                                     &n,&seq,NULL)))
 
2172
              if(n>3 && ((p[0]==3 && p[2]==0x19) || (p[0]==4 && p[1]==0x19)))
 
2173
                break;
 
2174
          }
 
2175
 
 
2176
        if(p)
 
2177
          {
 
2178
            PKT_signature *backsig=xmalloc_clear(sizeof(PKT_signature));
 
2179
            IOBUF backsig_buf=iobuf_temp_with_content(p,n);
 
2180
            int save_mode=set_packet_list_mode(0);
 
2181
 
 
2182
            if(parse_signature(backsig_buf,PKT_SIGNATURE,n,backsig)==0)
 
2183
              {
 
2184
                if(check_backsig(mainpk,subpk,backsig)==0)
 
2185
                  subpk->backsig=2;
 
2186
                else
 
2187
                  subpk->backsig=1;
 
2188
              }
 
2189
 
 
2190
            set_packet_list_mode(save_mode);
 
2191
 
 
2192
            iobuf_close(backsig_buf);
 
2193
            free_seckey_enc(backsig);
 
2194
          }
 
2195
      }
1887
2196
}
1888
2197
 
1889
2198
 
1890
 
 
1891
2199
/* 
1892
2200
 * Merge information from the self-signatures with the key, so that
1893
2201
 * we can later use them more easy.
1905
2213
{
1906
2214
    KBNODE k;
1907
2215
    int revoked;
 
2216
    struct revoke_info rinfo;
1908
2217
    PKT_public_key *main_pk;
1909
2218
    prefitem_t *prefs;
1910
2219
    int mdc_feature;
1921
2230
        BUG ();
1922
2231
    }
1923
2232
 
1924
 
    merge_selfsigs_main ( keyblock, &revoked );
 
2233
    merge_selfsigs_main ( keyblock, &revoked, &rinfo );
1925
2234
 
1926
2235
    /* now merge in the data from each of the subkeys */
1927
2236
    for(k=keyblock; k; k = k->next ) {
1941
2250
                PKT_public_key *pk = k->pkt->pkt.public_key;
1942
2251
                if(!main_pk->is_valid)
1943
2252
                  pk->is_valid = 0;
1944
 
                if(revoked)
1945
 
                  pk->is_revoked = 1;
 
2253
                if(revoked && !pk->is_revoked)
 
2254
                  {
 
2255
                    pk->is_revoked = revoked;
 
2256
                    memcpy(&pk->revoked,&rinfo,sizeof(rinfo));
 
2257
                  }
1946
2258
                if(main_pk->has_expired)
1947
2259
                  pk->has_expired = main_pk->has_expired;
1948
2260
            }
1966
2278
            && !k->pkt->pkt.user_id->attrib_data
1967
2279
            && k->pkt->pkt.user_id->is_primary) {
1968
2280
            prefs = k->pkt->pkt.user_id->prefs;
1969
 
            mdc_feature = k->pkt->pkt.user_id->mdc_feature;
 
2281
            mdc_feature = k->pkt->pkt.user_id->flags.mdc;
1970
2282
            break;
1971
2283
        }
1972
2284
    }    
2076
2388
                KBNODE next, ll;
2077
2389
 
2078
2390
                if (opt.verbose)
2079
 
                  log_info ( _("no secret subkey "
2080
 
                               "for public subkey %08lX - ignoring\n"),  
2081
 
                           (ulong)keyid_from_pk (pk,NULL) );
 
2391
                  log_info (_("no secret subkey"
 
2392
                              " for public subkey %s - ignoring\n"),  
 
2393
                            keystr_from_pk (pk));
2082
2394
                /* we have to remove the subkey in this case */
2083
2395
                assert ( last );
2084
2396
                /* find the next subkey */
2085
2397
                for (next=pub->next,ll=pub;
2086
 
                     next && pub->pkt->pkttype != PKT_PUBLIC_SUBKEY;
 
2398
                     next && next->pkt->pkttype != PKT_PUBLIC_SUBKEY;
2087
2399
                     ll = next, next = next->next ) 
2088
2400
                    ;
2089
2401
                /* make new link */
2141
2453
    KBNODE k;
2142
2454
    KBNODE foundk = NULL;
2143
2455
    PKT_user_id *foundu = NULL;
2144
 
#define USAGE_MASK  (PUBKEY_USAGE_SIG|PUBKEY_USAGE_ENC)
 
2456
#define USAGE_MASK  (PUBKEY_USAGE_SIG|PUBKEY_USAGE_ENC|PUBKEY_USAGE_CERT)
2145
2457
    unsigned int req_usage = ( ctx->req_usage & USAGE_MASK );
2146
2458
    /* Request the primary if we're certifying another key, and also
2147
2459
       if signing data while --pgp6 or --pgp7 is on since pgp 6 and 7
2303
2615
        
2304
2616
    ctx->found_key = latest_key;
2305
2617
 
2306
 
    if (latest_key != keyblock && opt.verbose) {
2307
 
        log_info(_("using secondary key %08lX "
2308
 
                   "instead of primary key %08lX\n"),
2309
 
                 (ulong)keyid_from_pk( latest_key->pkt->pkt.public_key, NULL),
2310
 
                 (ulong)keyid_from_pk( keyblock->pkt->pkt.public_key, NULL) );
2311
 
    }
 
2618
    if (latest_key != keyblock && opt.verbose)
 
2619
      {
 
2620
        char *tempkeystr=
 
2621
          xstrdup(keystr_from_pk(latest_key->pkt->pkt.public_key));
 
2622
        log_info(_("using subkey %s instead of primary key %s\n"),
 
2623
                 tempkeystr, keystr_from_pk(keyblock->pkt->pkt.public_key));
 
2624
        xfree(tempkeystr);
 
2625
      }
2312
2626
 
2313
2627
    cache_user_id( keyblock );
2314
2628
    
2333
2647
 
2334
2648
        rc = keydb_get_keyblock (ctx->kr_handle, &ctx->keyblock);
2335
2649
        if (rc) {
2336
 
            log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
 
2650
            log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc));
2337
2651
            rc = 0;
2338
2652
            goto skip;
2339
2653
        }
2349
2663
 
2350
2664
            keyid_from_sk (k->pkt->pkt.secret_key, aki);
2351
2665
            k = get_pubkeyblock (aki);
2352
 
            if( !k ) {
 
2666
            if( !k )
 
2667
              {
2353
2668
                if (!opt.quiet)
2354
 
                    log_info(_("key %08lX: secret key without public key "
2355
 
                               "- skipped\n"),  (ulong)aki[1] );
 
2669
                  log_info(_("key %s: secret key without public key"
 
2670
                             " - skipped\n"), keystr(aki));
2356
2671
                goto skip;
2357
 
            }
 
2672
              }
2358
2673
            secblock = ctx->keyblock;
2359
2674
            ctx->keyblock = k;
2360
2675
 
2390
2705
 
2391
2706
  found:
2392
2707
    if( rc && rc != -1 )
2393
 
        log_error("keydb_search failed: %s\n", gpg_strerror (rc));
 
2708
        log_error("keydb_search failed: %s\n", g10_errstr(rc));
2394
2709
 
2395
2710
    if( !rc ) {
2396
2711
        *ret_keyblock = ctx->keyblock; /* return the keyblock */
2397
2712
        ctx->keyblock = NULL;
2398
2713
    }
2399
2714
    else if (rc == -1 && no_suitable_key)
2400
 
         rc = secmode ? GPG_ERR_UNUSABLE_SECKEY : GPG_ERR_UNUSABLE_PUBKEY;
 
2715
        rc = secmode ? G10ERR_UNU_SECKEY : G10ERR_UNU_PUBKEY;
2401
2716
    else if( rc == -1 )
2402
 
        rc = secmode ? GPG_ERR_NO_SECKEY : GPG_ERR_NO_PUBKEY;
 
2717
        rc = secmode ? G10ERR_NO_SECKEY : G10ERR_NO_PUBKEY;
2403
2718
 
2404
2719
    if ( secmode ) {
2405
2720
        release_kbnode( secblock );
2449
2764
 
2450
2765
 
2451
2766
    if( !c ) { /* make a new context */
2452
 
        c = xcalloc (1, sizeof *c );
 
2767
        c = xmalloc_clear( sizeof *c );
2453
2768
        *context = c;
2454
2769
        c->hd = keydb_new (1);
2455
2770
        c->first = 1;
2460
2775
    if( !sk ) { /* free the context */
2461
2776
        keydb_release (c->hd);
2462
2777
        release_kbnode (c->keyblock);
2463
 
        xfree ( c );
 
2778
        xfree( c );
2464
2779
        *context = NULL;
2465
2780
        return 0;
2466
2781
    }
2507
2822
 
2508
2823
/****************
2509
2824
 * Return a string with a printable representation of the user_id.
2510
 
 * this string must be freed by m_free.
 
2825
 * this string must be freed by xfree.
2511
2826
 */
2512
2827
char*
2513
2828
get_user_id_string( u32 *keyid )
2514
2829
{
2515
 
    user_id_db_t r;
2516
 
    char *p;
2517
 
    int pass=0;
2518
 
    /* try it two times; second pass reads from key resources */
2519
 
    do {
2520
 
        for(r=user_id_db; r; r = r->next ) {
2521
 
            keyid_list_t a;
2522
 
            for (a=r->keyids; a; a= a->next ) {
2523
 
                if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] ) {
2524
 
                    p = xmalloc ( r->len + 10 );
2525
 
                    sprintf(p, "%08lX %.*s",
2526
 
                            (ulong)keyid[1], r->len, r->name );
2527
 
                    return p;
2528
 
                }
2529
 
            }
 
2830
  user_id_db_t r;
 
2831
  char *p;
 
2832
  int pass=0;
 
2833
  /* try it two times; second pass reads from key resources */
 
2834
  do
 
2835
    {
 
2836
      for(r=user_id_db; r; r = r->next )
 
2837
        {
 
2838
          keyid_list_t a;
 
2839
          for (a=r->keyids; a; a= a->next )
 
2840
            {
 
2841
              if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] )
 
2842
                {
 
2843
                  p = xmalloc( keystrlen() + 1 + r->len + 1 );
 
2844
                  sprintf(p, "%s %.*s", keystr(keyid), r->len, r->name );
 
2845
                  return p;
 
2846
                }
 
2847
            }
2530
2848
        }
2531
2849
    } while( ++pass < 2 && !get_pubkey( NULL, keyid ) );
2532
 
    p = xmalloc ( 15 );
2533
 
    sprintf(p, "%08lX [?]", (ulong)keyid[1] );
2534
 
    return p;
 
2850
  p = xmalloc( keystrlen() + 5 );
 
2851
  sprintf(p, "%s [?]", keystr(keyid));
 
2852
  return p;
2535
2853
}
2536
2854
 
2537
2855
 
2538
2856
char*
2539
 
get_user_id_string_printable ( u32 *keyid )
 
2857
get_user_id_string_native ( u32 *keyid )
2540
2858
{
2541
 
    char *p = get_user_id_string( keyid );
2542
 
    char *p2 = utf8_to_native( p, strlen(p), 0 );
2543
 
    xfree (p);
2544
 
    p = make_printable_string (p2, strlen (p2), 0);
2545
 
    xfree (p2);
2546
 
    return p;
 
2859
  char *p = get_user_id_string( keyid );
 
2860
  char *p2 = utf8_to_native( p, strlen(p), 0 );
 
2861
  xfree(p);
 
2862
  return p2;
2547
2863
}
2548
2864
 
2549
2865
 
2559
2875
            keyid_list_t a;
2560
2876
            for (a=r->keyids; a; a= a->next ) {
2561
2877
                if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] ) {
2562
 
                    p = xmalloc ( r->len + 20 );
 
2878
                    p = xmalloc( r->len + 20 );
2563
2879
                    sprintf(p, "%08lX%08lX %.*s",
2564
2880
                            (ulong)keyid[0], (ulong)keyid[1],
2565
2881
                            r->len, r->name );
2568
2884
            }
2569
2885
        }
2570
2886
    } while( ++pass < 2 && !get_pubkey( NULL, keyid ) );
2571
 
    p = xmalloc ( 25 );
 
2887
    p = xmalloc( 25 );
2572
2888
    sprintf(p, "%08lX%08lX [?]", (ulong)keyid[0], (ulong)keyid[1] );
2573
2889
    return p;
2574
2890
}
2586
2902
            keyid_list_t a;
2587
2903
            for (a=r->keyids; a; a= a->next ) {
2588
2904
                if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] ) {
2589
 
                    p = xmalloc ( r->len );
 
2905
                    p = xmalloc( r->len );
2590
2906
                    memcpy(p, r->name, r->len );
2591
2907
                    *rn = r->len;
2592
2908
                    return p;
2594
2910
            }
2595
2911
        }
2596
2912
    } while( ++pass < 2 && !get_pubkey( NULL, keyid ) );
2597
 
    p = xstrdup ( _("[User id not found]") );
 
2913
    p = xstrdup( user_id_not_found_utf8 () );
2598
2914
    *rn = strlen(p);
2599
2915
    return p;
2600
2916
}
2601
2917
 
2602
2918
char*
2603
 
get_user_id_printable( u32 *keyid )
 
2919
get_user_id_native( u32 *keyid )
2604
2920
{
2605
 
    size_t rn;
2606
 
    char *p = get_user_id( keyid, &rn );
2607
 
    char *p2 = utf8_to_native( p, rn, 0 );
2608
 
    xfree (p);
2609
 
    p = make_printable_string (p2, strlen (p2), 0);
2610
 
    xfree (p2);
2611
 
    return p;
 
2921
  size_t rn;
 
2922
  char *p = get_user_id( keyid, &rn );
 
2923
  char *p2 = utf8_to_native( p, rn, 0 );
 
2924
  xfree(p);
 
2925
  return p2;
2612
2926
}
2613
2927
 
2614
2928
KEYDB_HANDLE
2616
2930
{
2617
2931
  return ctx->kr_handle;
2618
2932
}
 
2933
 
 
2934
static void
 
2935
free_akl(struct akl *akl)
 
2936
{
 
2937
  if(akl->spec)
 
2938
    free_keyserver_spec(akl->spec);
 
2939
 
 
2940
  xfree(akl);
 
2941
}
 
2942
 
 
2943
void
 
2944
release_akl(void)
 
2945
{
 
2946
  while(opt.auto_key_locate)
 
2947
    {
 
2948
      struct akl *akl2=opt.auto_key_locate;
 
2949
      opt.auto_key_locate=opt.auto_key_locate->next;
 
2950
      free_akl(akl2);
 
2951
    }
 
2952
}
 
2953
 
 
2954
int
 
2955
parse_auto_key_locate(char *options)
 
2956
{
 
2957
  char *tok;
 
2958
 
 
2959
  while((tok=optsep(&options)))
 
2960
    {
 
2961
      struct akl *akl,*check,*last=NULL;
 
2962
      int dupe=0;
 
2963
 
 
2964
      if(tok[0]=='\0')
 
2965
        continue;
 
2966
 
 
2967
      akl=xmalloc_clear(sizeof(*akl));
 
2968
 
 
2969
      if(ascii_strcasecmp(tok,"ldap")==0)
 
2970
        akl->type=AKL_LDAP;
 
2971
      else if(ascii_strcasecmp(tok,"keyserver")==0)
 
2972
        akl->type=AKL_KEYSERVER;
 
2973
#ifdef USE_DNS_CERT
 
2974
      else if(ascii_strcasecmp(tok,"cert")==0)
 
2975
        akl->type=AKL_CERT;
 
2976
#endif
 
2977
#ifdef USE_DNS_PKA
 
2978
      else if(ascii_strcasecmp(tok,"pka")==0)
 
2979
        akl->type=AKL_PKA;
 
2980
#endif
 
2981
      else if((akl->spec=parse_keyserver_uri(tok,1,NULL,0)))
 
2982
        akl->type=AKL_SPEC;
 
2983
      else
 
2984
        {
 
2985
          free_akl(akl);
 
2986
          return 0;
 
2987
        }
 
2988
 
 
2989
      /* We must maintain the order the user gave us */
 
2990
      for(check=opt.auto_key_locate;check;last=check,check=check->next)
 
2991
        {
 
2992
          /* Check for duplicates */
 
2993
          if(check->type==akl->type
 
2994
             && (akl->type!=AKL_SPEC
 
2995
                 || (akl->type==AKL_SPEC
 
2996
                     && strcmp(check->spec->uri,akl->spec->uri)==0)))
 
2997
            {
 
2998
              dupe=1;
 
2999
              free_akl(akl);
 
3000
              break;
 
3001
            }
 
3002
        }
 
3003
 
 
3004
      if(!dupe)
 
3005
        {
 
3006
          if(last)
 
3007
            last->next=akl;
 
3008
          else
 
3009
            opt.auto_key_locate=akl;
 
3010
        }
 
3011
    }
 
3012
 
 
3013
  return 1;
 
3014
}