1 /* getkey.c - Get a key from the database
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2007, 2008 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
37 #include "keyserver-internal.h"
39 #define MAX_PK_CACHE_ENTRIES PK_UID_CACHE_SIZE
40 #define MAX_UID_CACHE_ENTRIES PK_UID_CACHE_SIZE
42 #if MAX_PK_CACHE_ENTRIES < 2
43 #error We need the cache for key creation
50 KBNODE found_key
; /* Pointer into some keyblock. */
51 strlist_t extra_list
; /* Will be freed when releasing the context. */
55 KEYDB_HANDLE kr_handle
;
58 KEYDB_SEARCH_DESC items
[1];
70 typedef struct keyid_list
{
71 struct keyid_list
*next
;
76 #if MAX_PK_CACHE_ENTRIES
77 typedef struct pk_cache_entry
{
78 struct pk_cache_entry
*next
;
82 static pk_cache_entry_t pk_cache
;
83 static int pk_cache_entries
; /* number of entries in pk cache */
84 static int pk_cache_disabled
;
87 #if MAX_UID_CACHE_ENTRIES < 5
88 #error we really need the userid cache
90 typedef struct user_id_db
{
91 struct user_id_db
*next
;
96 static user_id_db_t user_id_db
;
97 static int uid_cache_entries
; /* number of entries in uid cache */
99 static void merge_selfsigs( KBNODE keyblock
);
100 static int lookup( GETKEY_CTX ctx
, KBNODE
*ret_keyblock
, int secmode
);
107 for(i
=0; i
< DIM(lkup_stats
); i
++ ) {
108 if( lkup_stats
[i
].any
)
110 "lookup stats: mode=%-2d ok=%-6d nokey=%-6d err=%-6d\n",
112 lkup_stats
[i
].okay_count
,
113 lkup_stats
[i
].nokey_count
,
114 lkup_stats
[i
].error_count
);
121 cache_public_key( PKT_public_key
*pk
)
123 #if MAX_PK_CACHE_ENTRIES
127 if( pk_cache_disabled
)
133 if( is_ELGAMAL(pk
->pubkey_algo
)
134 || pk
->pubkey_algo
== PUBKEY_ALGO_DSA
135 || is_RSA(pk
->pubkey_algo
) ) {
136 keyid_from_pk( pk
, keyid
);
139 return; /* don't know how to get the keyid */
141 for( ce
= pk_cache
; ce
; ce
= ce
->next
)
142 if( ce
->keyid
[0] == keyid
[0] && ce
->keyid
[1] == keyid
[1] ) {
144 log_debug("cache_public_key: already in cache\n");
148 if( pk_cache_entries
>= MAX_PK_CACHE_ENTRIES
) {
149 /* fixme: use another algorithm to free some cache slots */
151 if( opt
.verbose
> 1 )
152 log_info(_("too many entries in pk cache - disabled\n"));
156 ce
= xmalloc( sizeof *ce
);
159 ce
->pk
= copy_public_key( NULL
, pk
);
160 ce
->keyid
[0] = keyid
[0];
161 ce
->keyid
[1] = keyid
[1];
166 /* Return a const utf-8 string with the text "[User ID not found]".
167 This fucntion is required so that we don't need to switch gettext's
168 encoding temporary. */
170 user_id_not_found_utf8 (void)
175 text
= native_to_utf8 (_("[User ID not found]"));
182 * Return the user ID from the given keyblock.
183 * We use the primary uid flag which has been set by the merge_selfsigs
184 * function. The returned value is only valid as long as then given
185 * keyblock is not changed
188 get_primary_uid ( KBNODE keyblock
, size_t *uidlen
)
193 for (k
=keyblock
; k
; k
=k
->next
) {
194 if ( k
->pkt
->pkttype
== PKT_USER_ID
195 && !k
->pkt
->pkt
.user_id
->attrib_data
196 && k
->pkt
->pkt
.user_id
->is_primary
) {
197 *uidlen
= k
->pkt
->pkt
.user_id
->len
;
198 return k
->pkt
->pkt
.user_id
->name
;
201 s
= user_id_not_found_utf8 ();
202 *uidlen
= strlen (s
);
208 release_keyid_list ( keyid_list_t k
)
211 keyid_list_t k2
= k
->next
;
218 * Store the association of keyid and userid
219 * Feed only public keys to this function.
222 cache_user_id( KBNODE keyblock
)
227 keyid_list_t keyids
= NULL
;
230 for (k
=keyblock
; k
; k
= k
->next
) {
231 if ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
232 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
233 keyid_list_t a
= xmalloc_clear ( sizeof *a
);
234 /* Hmmm: For a long list of keyids it might be an advantage
235 * to append the keys */
236 keyid_from_pk( k
->pkt
->pkt
.public_key
, a
->keyid
);
237 /* first check for duplicates */
238 for(r
=user_id_db
; r
; r
= r
->next
) {
239 keyid_list_t b
= r
->keyids
;
240 for ( b
= r
->keyids
; b
; b
= b
->next
) {
241 if( b
->keyid
[0] == a
->keyid
[0]
242 && b
->keyid
[1] == a
->keyid
[1] ) {
244 log_debug("cache_user_id: already in cache\n");
245 release_keyid_list ( keyids
);
251 /* now put it into the cache */
257 BUG (); /* No key no fun */
260 uid
= get_primary_uid ( keyblock
, &uidlen
);
262 if( uid_cache_entries
>= MAX_UID_CACHE_ENTRIES
) {
263 /* fixme: use another algorithm to free some cache slots */
265 user_id_db
= r
->next
;
266 release_keyid_list ( r
->keyids
);
270 r
= xmalloc( sizeof *r
+ uidlen
-1 );
273 memcpy(r
->name
, uid
, r
->len
);
274 r
->next
= user_id_db
;
281 getkey_disable_caches()
283 #if MAX_PK_CACHE_ENTRIES
285 pk_cache_entry_t ce
, ce2
;
287 for( ce
= pk_cache
; ce
; ce
= ce2
) {
289 free_public_key( ce
->pk
);
293 pk_cache_entries
= 0;
297 /* fixme: disable user id cache ? */
302 pk_from_block ( GETKEY_CTX ctx
, PKT_public_key
*pk
, KBNODE keyblock
)
304 KBNODE a
= ctx
->found_key
? ctx
->found_key
: keyblock
;
306 assert ( a
->pkt
->pkttype
== PKT_PUBLIC_KEY
307 || a
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
);
309 copy_public_key ( pk
, a
->pkt
->pkt
.public_key
);
313 sk_from_block ( GETKEY_CTX ctx
,
314 PKT_secret_key
*sk
, KBNODE keyblock
)
316 KBNODE a
= ctx
->found_key
? ctx
->found_key
: keyblock
;
318 assert ( a
->pkt
->pkttype
== PKT_SECRET_KEY
319 || a
->pkt
->pkttype
== PKT_SECRET_SUBKEY
);
321 copy_secret_key( sk
, a
->pkt
->pkt
.secret_key
);
326 * Get a public key and store it into the allocated pk
327 * can be called with PK set to NULL to just read it into some
328 * internal structures.
331 get_pubkey( PKT_public_key
*pk
, u32
*keyid
)
336 #if MAX_PK_CACHE_ENTRIES
339 /* Try to get it from the cache. We don't do this when pk is
340 NULL as it does not guarantee that the user IDs are
343 for( ce
= pk_cache
; ce
; ce
= ce
->next
)
345 if( ce
->keyid
[0] == keyid
[0] && ce
->keyid
[1] == keyid
[1] )
347 copy_public_key( pk
, ce
->pk
);
353 /* more init stuff */
355 pk
= xmalloc_clear( sizeof *pk
);
361 { struct getkey_ctx_s ctx
;
363 memset( &ctx
, 0, sizeof ctx
);
364 ctx
.exact
= 1; /* use the key ID exactly as given */
365 ctx
.not_allocated
= 1;
366 ctx
.kr_handle
= keydb_new (0);
368 ctx
.items
[0].mode
= KEYDB_SEARCH_MODE_LONG_KID
;
369 ctx
.items
[0].u
.kid
[0] = keyid
[0];
370 ctx
.items
[0].u
.kid
[1] = keyid
[1];
371 ctx
.req_algo
= pk
->req_algo
;
372 ctx
.req_usage
= pk
->req_usage
;
373 rc
= lookup( &ctx
, &kb
, 0 );
375 pk_from_block ( &ctx
, pk
, kb
);
377 get_pubkey_end( &ctx
);
378 release_kbnode ( kb
);
383 rc
= G10ERR_NO_PUBKEY
;
387 cache_public_key( pk
);
394 /* Get a public key and store it into the allocated pk. This function
395 differs from get_pubkey() in that it does not do a check of the key
396 to avoid recursion. It should be used only in very certain cases.
397 It will only retrieve primary keys. */
399 get_pubkey_fast (PKT_public_key
*pk
, u32
*keyid
)
407 #if MAX_PK_CACHE_ENTRIES
408 { /* Try to get it from the cache */
411 for (ce
= pk_cache
; ce
; ce
= ce
->next
)
413 if (ce
->keyid
[0] == keyid
[0] && ce
->keyid
[1] == keyid
[1])
416 copy_public_key (pk
, ce
->pk
);
424 rc
= keydb_search_kid (hd
, keyid
);
428 return G10ERR_NO_PUBKEY
;
430 rc
= keydb_get_keyblock (hd
, &keyblock
);
434 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc
));
435 return G10ERR_NO_PUBKEY
;
438 assert ( keyblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
439 || keyblock
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
);
441 keyid_from_pk(keyblock
->pkt
->pkt
.public_key
,pkid
);
442 if(keyid
[0]==pkid
[0] && keyid
[1]==pkid
[1])
443 copy_public_key (pk
, keyblock
->pkt
->pkt
.public_key
);
447 release_kbnode (keyblock
);
449 /* Not caching key here since it won't have all of the fields
457 get_pubkeyblock( u32
*keyid
)
459 struct getkey_ctx_s ctx
;
461 KBNODE keyblock
= NULL
;
463 memset( &ctx
, 0, sizeof ctx
);
464 /* no need to set exact here because we want the entire block */
465 ctx
.not_allocated
= 1;
466 ctx
.kr_handle
= keydb_new (0);
468 ctx
.items
[0].mode
= KEYDB_SEARCH_MODE_LONG_KID
;
469 ctx
.items
[0].u
.kid
[0] = keyid
[0];
470 ctx
.items
[0].u
.kid
[1] = keyid
[1];
471 rc
= lookup( &ctx
, &keyblock
, 0 );
472 get_pubkey_end( &ctx
);
474 return rc
? NULL
: keyblock
;
481 * Get a secret key and store it into sk
484 get_seckey( PKT_secret_key
*sk
, u32
*keyid
)
487 struct getkey_ctx_s ctx
;
490 memset( &ctx
, 0, sizeof ctx
);
491 ctx
.exact
= 1; /* use the key ID exactly as given */
492 ctx
.not_allocated
= 1;
493 ctx
.kr_handle
= keydb_new (1);
495 ctx
.items
[0].mode
= KEYDB_SEARCH_MODE_LONG_KID
;
496 ctx
.items
[0].u
.kid
[0] = keyid
[0];
497 ctx
.items
[0].u
.kid
[1] = keyid
[1];
498 ctx
.req_algo
= sk
->req_algo
;
499 ctx
.req_usage
= sk
->req_usage
;
500 rc
= lookup( &ctx
, &kb
, 1 );
502 sk_from_block ( &ctx
, sk
, kb
);
504 get_seckey_end( &ctx
);
505 release_kbnode ( kb
);
508 /* check the secret key (this may prompt for a passprase to
509 * unlock the secret key
511 rc
= check_secret_key( sk
, 0 );
519 * Check whether the secret key is available. This is just a fast
520 * check and does not tell us whether the secret key is valid. It
521 * merely tells other whether there is some secret key.
522 * Returns: 0 := key is available
523 * G10ERR_NO_SECKEY := not availabe
526 seckey_available( u32
*keyid
)
529 KEYDB_HANDLE hd
= keydb_new (1);
531 rc
= keydb_search_kid (hd
, keyid
);
533 rc
= G10ERR_NO_SECKEY
;
540 * Return the type of the user id:
542 * Please use the constants KEYDB_SERCH_MODE_xxx
543 * 0 = Invalid user ID
545 * 2 = match a substring
546 * 3 = match an email address
547 * 4 = match a substring of an email address
548 * 5 = match an email address, but compare from end
549 * 6 = word match mode
550 * 10 = it is a short KEYID (don't care about keyid[0])
551 * 11 = it is a long KEYID
552 * 12 = it is a trustdb index (keyid is looked up)
553 * 16 = it is a 16 byte fingerprint
554 * 20 = it is a 20 byte fingerprint
555 * 21 = Unified fingerprint :fpr:pk_algo:
556 * (We don't use pk_algo yet)
559 * - If the username starts with 8,9,16 or 17 hex-digits (the first one
560 * must be in the range 0..9), this is considered a keyid; depending
561 * on the length a short or complete one.
562 * - If the username starts with 32,33,40 or 41 hex-digits (the first one
563 * must be in the range 0..9), this is considered a fingerprint.
564 * - If the username starts with a left angle, we assume it is a complete
565 * email address and look only at this part.
566 * - If the username starts with a colon we assume it is a unified
567 * key specfification.
568 * - If the username starts with a '.', we assume it is the ending
569 * part of an email address
570 * - If the username starts with an '@', we assume it is a part of an
572 * - If the userid start with an '=' an exact compare is done.
573 * - If the userid starts with a '*' a case insensitive substring search is
574 * done (This is the default).
575 * - If the userid starts with a '+' we will compare individual words
576 * and a match requires that all the words are in the userid.
577 * Words are delimited by white space or "()<>[]{}.@-+_,;/&!"
578 * (note that you can't search for these characters). Compare
579 * is not case sensitive.
580 * - If the userid starts with a '&' a 40 hex digits keygrip is expected.
584 classify_user_id( const char *name
, KEYDB_SEARCH_DESC
*desc
)
590 KEYDB_SEARCH_DESC dummy_desc
;
595 /* clear the structure so that the mode field is set to zero unless
596 * we set it to the correct value right at the end of this function */
597 memset (desc
, 0, sizeof *desc
);
599 /* skip leading spaces. Fixme: what is with trailing spaces? */
600 for(s
= name
; *s
&& spacep (s
); s
++ )
604 case 0: /* empty string is an error */
608 case '.': /* an email address, compare from end */
609 mode
= KEYDB_SEARCH_MODE_MAILEND
;
615 case '<': /* an email address */
616 mode
= KEYDB_SEARCH_MODE_MAIL
;
620 case '@': /* part of an email address */
621 mode
= KEYDB_SEARCH_MODE_MAILSUB
;
626 case '=': /* exact compare */
627 mode
= KEYDB_SEARCH_MODE_EXACT
;
632 case '*': /* case insensitive substring search */
633 mode
= KEYDB_SEARCH_MODE_SUBSTR
;
639 case '+': /* compare individual words */
640 mode
= KEYDB_SEARCH_MODE_WORDS
;
646 case '#': /* local user id */
647 return 0; /* This is now obsolete and can't not be used anymore*/
649 case ':': /*Unified fingerprint */
654 se
= strchr( ++s
,':');
657 for (i
=0,si
=s
; si
< se
; si
++, i
++ ) {
658 if ( !strchr("01234567890abcdefABCDEF", *si
) )
659 return 0; /* invalid digit */
661 if (i
!= 32 && i
!= 40)
662 return 0; /* invalid length of fpr*/
663 for (i
=0,si
=s
; si
< se
; i
++, si
+=2)
664 desc
->u
.fpr
[i
] = hextobyte(si
);
668 mode
= KEYDB_SEARCH_MODE_FPR
;
672 case '&': /* keygrip */
673 return 0; /* Not yet implememted. */
676 if (s
[0] == '0' && s
[1] == 'x') {
681 hexlength
= strspn(s
, "0123456789abcdefABCDEF");
682 if (hexlength
>= 8 && s
[hexlength
] =='!') {
684 hexlength
++; /* just for the following check */
687 /* check if a hexadecimal number is terminated by EOS or blank */
688 if (hexlength
&& s
[hexlength
] && !spacep(s
+hexlength
)) {
689 if (hexprefix
) /* a "0x" prefix without correct */
690 return 0; /* termination is an error */
691 else /* The first chars looked like */
692 hexlength
= 0; /* a hex number, but really were not. */
699 || (!hexprefix
&& hexlength
== 9 && *s
== '0')){
704 desc
->u
.kid
[1] = strtoul( s
, NULL
, 16 );
705 mode
= KEYDB_SEARCH_MODE_SHORT_KID
;
707 else if (hexlength
== 16
708 || (!hexprefix
&& hexlength
== 17 && *s
== '0')) {
714 desc
->u
.kid
[0] = strtoul( buf
, NULL
, 16 );
715 desc
->u
.kid
[1] = strtoul( s
+8, NULL
, 16 );
716 mode
= KEYDB_SEARCH_MODE_LONG_KID
;
718 else if (hexlength
== 32 || (!hexprefix
&& hexlength
== 33
720 /* md5 fingerprint */
724 memset(desc
->u
.fpr
+16, 0, 4);
725 for (i
=0; i
< 16; i
++, s
+=2) {
726 int c
= hextobyte(s
);
731 mode
= KEYDB_SEARCH_MODE_FPR16
;
733 else if (hexlength
== 40 || (!hexprefix
&& hexlength
== 41
735 /* sha1/rmd160 fingerprint */
739 for (i
=0; i
< 20; i
++, s
+=2) {
740 int c
= hextobyte(s
);
745 mode
= KEYDB_SEARCH_MODE_FPR20
;
748 if (hexprefix
) /* This was a hex number with a prefix */
749 return 0; /* and a wrong length */
753 mode
= KEYDB_SEARCH_MODE_SUBSTR
; /* default mode */
763 skip_unusable(void *dummy
,u32
*keyid
,PKT_user_id
*uid
)
768 keyblock
=get_pubkeyblock(keyid
);
771 log_error("error checking usability status of %s\n",keystr(keyid
));
775 /* Is the user ID in question revoked/expired? */
780 for(node
=keyblock
;node
;node
=node
->next
)
782 if(node
->pkt
->pkttype
==PKT_USER_ID
)
784 if(cmp_user_ids(uid
,node
->pkt
->pkt
.user_id
)==0
785 && (node
->pkt
->pkt
.user_id
->is_revoked
786 || node
->pkt
->pkt
.user_id
->is_expired
))
796 unusable
=pk_is_disabled(keyblock
->pkt
->pkt
.public_key
);
799 release_kbnode(keyblock
);
804 * Try to get the pubkey by the userid. This function looks for the
805 * first pubkey certificate which has the given name in a user_id. if
806 * pk/sk has the pubkey algo set, the function will only return a
807 * pubkey with that algo. If namelist is NULL, the first key is
808 * returned. The caller should provide storage for either the pk or
809 * the sk. If ret_kb is not NULL the function will return the
814 key_byname( GETKEY_CTX
*retctx
, strlist_t namelist
,
815 PKT_public_key
*pk
, PKT_secret_key
*sk
,
816 int secmode
, int include_unusable
,
817 KBNODE
*ret_kb
, KEYDB_HANDLE
*ret_kdbhd
)
823 KBNODE help_kb
= NULL
;
825 if( retctx
) {/* reset the returned context in case of error */
826 assert (!ret_kdbhd
); /* not allowed because the handle is
827 stored in the context */
835 ctx
= xmalloc_clear (sizeof *ctx
);
837 ctx
->items
[0].mode
=KEYDB_SEARCH_MODE_FIRST
;
838 if(!include_unusable
)
839 ctx
->items
[0].skipfnc
=skip_unusable
;
843 /* build the search context */
844 for(n
=0, r
=namelist
; r
; r
= r
->next
)
847 ctx
= xmalloc_clear (sizeof *ctx
+ (n
-1)*sizeof ctx
->items
);
850 for(n
=0, r
=namelist
; r
; r
= r
->next
, n
++ )
852 classify_user_id (r
->d
, &ctx
->items
[n
]);
854 if (ctx
->items
[n
].exact
)
856 if (!ctx
->items
[n
].mode
)
859 return G10ERR_INV_USER_ID
;
862 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_SHORT_KID
863 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_LONG_KID
864 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_FPR16
865 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_FPR20
866 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_FPR
)
867 ctx
->items
[n
].skipfnc
=skip_unusable
;
871 ctx
->kr_handle
= keydb_new (secmode
);
877 ctx
->req_algo
= sk
->req_algo
;
878 ctx
->req_usage
= sk
->req_usage
;
880 rc
= lookup( ctx
, ret_kb
, 1 );
882 sk_from_block ( ctx
, sk
, *ret_kb
);
887 ctx
->req_algo
= pk
->req_algo
;
888 ctx
->req_usage
= pk
->req_usage
;
890 rc
= lookup( ctx
, ret_kb
, 0 );
892 pk_from_block ( ctx
, pk
, *ret_kb
);
896 release_kbnode ( help_kb
);
898 if (retctx
) /* caller wants the context */
902 *ret_kdbhd
= ctx
->kr_handle
;
903 ctx
->kr_handle
= NULL
;
905 get_pubkey_end (ctx
);
913 /* Find a public key from NAME and return the keyblock or the key. If
914 ret_kdb is not NULL, the KEYDB handle used to locate this keyblock
915 is returned and the caller is responsible for closing it. If a key
916 was not found (or if local search has been disabled) and NAME is a
917 valid RFC822 mailbox and --auto-key-locate has been enabled, we try
918 to import the key via the online mechanisms defined by
919 --auto-key-locate. */
921 get_pubkey_byname (GETKEY_CTX
*retctx
, PKT_public_key
*pk
,
922 const char *name
, KBNODE
*ret_keyblock
,
923 KEYDB_HANDLE
*ret_kdbhd
, int include_unusable
,
927 strlist_t namelist
= NULL
;
931 int anylocalfirst
= 0;
936 is_mbox
= is_valid_mailbox (name
);
938 /* Check whether we the default local search has been disabled.
939 This is the case if either the "nodefault" or the "local" keyword
940 are in the list of auto key locate mechanisms.
942 ANYLOCALFIRST is set if the search order has the local method
943 before any other or if "local" is used first by default. This
944 makes sure that if a RETCTX is used it gets only set if a local
945 search has precedence over the other search methods and only then
946 a followup call to get_pubkey_next shall succeed. */
949 for (akl
=opt
.auto_key_locate
; akl
; akl
=akl
->next
)
950 if (akl
->type
== AKL_NODEFAULT
|| akl
->type
== AKL_LOCAL
)
955 for (akl
=opt
.auto_key_locate
; akl
; akl
=akl
->next
)
956 if (akl
->type
!= AKL_NODEFAULT
)
958 if (akl
->type
== AKL_LOCAL
)
967 if (nodefault
&& is_mbox
)
969 /* Nodefault but a mailbox - let the AKL locate the key. */
970 rc
= G10ERR_NO_PUBKEY
;
974 add_to_strlist (&namelist
, name
);
975 rc
= key_byname (retctx
, namelist
, pk
, NULL
, 0,
976 include_unusable
, ret_keyblock
, ret_kdbhd
);
979 /* If the requested name resembles a valid mailbox and automatic
980 retrieval has been enabled, we try to import the key. */
981 if (gpg_err_code (rc
) == G10ERR_NO_PUBKEY
&& !no_akl
&& is_mbox
)
983 for (akl
=opt
.auto_key_locate
; akl
; akl
=akl
->next
)
985 unsigned char *fpr
= NULL
;
987 int did_key_byname
= 0;
988 int no_fingerprint
= 0;
989 const char *mechanism
= "?";
994 /* This is a dummy mechanism. */
996 rc
= G10ERR_NO_PUBKEY
;
1000 mechanism
= "Local";
1004 get_pubkey_end (*retctx
);
1007 add_to_strlist (&namelist
, name
);
1008 rc
= key_byname (anylocalfirst
? retctx
:NULL
,
1009 namelist
, pk
, NULL
, 0,
1010 include_unusable
, ret_keyblock
, ret_kdbhd
);
1014 mechanism
= "DNS CERT";
1015 glo_ctrl
.in_auto_key_retrieve
++;
1016 rc
=keyserver_import_cert(name
,&fpr
,&fpr_len
);
1017 glo_ctrl
.in_auto_key_retrieve
--;
1022 glo_ctrl
.in_auto_key_retrieve
++;
1023 rc
=keyserver_import_pka(name
,&fpr
,&fpr_len
);
1024 glo_ctrl
.in_auto_key_retrieve
--;
1029 glo_ctrl
.in_auto_key_retrieve
++;
1030 rc
=keyserver_import_ldap(name
,&fpr
,&fpr_len
);
1031 glo_ctrl
.in_auto_key_retrieve
--;
1035 /* Strictly speaking, we don't need to only use a valid
1036 mailbox for the getname search, but it helps cut down
1037 on the problem of searching for something like "john"
1038 and getting a whole lot of keys back. */
1041 mechanism
= opt
.keyserver
->uri
;
1042 glo_ctrl
.in_auto_key_retrieve
++;
1043 rc
=keyserver_import_name(name
,&fpr
,&fpr_len
,opt
.keyserver
);
1044 glo_ctrl
.in_auto_key_retrieve
--;
1048 mechanism
= "Unconfigured keyserver";
1049 rc
= G10ERR_NO_PUBKEY
;
1055 struct keyserver_spec
*keyserver
;
1057 mechanism
= akl
->spec
->uri
;
1058 keyserver
=keyserver_match(akl
->spec
);
1059 glo_ctrl
.in_auto_key_retrieve
++;
1060 rc
=keyserver_import_name(name
,&fpr
,&fpr_len
,keyserver
);
1061 glo_ctrl
.in_auto_key_retrieve
--;
1066 /* Use the fingerprint of the key that we actually fetched.
1067 This helps prevent problems where the key that we fetched
1068 doesn't have the same name that we used to fetch it. In
1069 the case of CERT and PKA, this is an actual security
1070 requirement as the URL might point to a key put in by an
1071 attacker. By forcing the use of the fingerprint, we
1072 won't use the attacker's key here. */
1075 char fpr_string
[MAX_FINGERPRINT_LEN
*2+1];
1077 assert(fpr_len
<=MAX_FINGERPRINT_LEN
);
1079 free_strlist(namelist
);
1082 bin2hex (fpr
, fpr_len
, fpr_string
);
1085 log_info("auto-key-locate found fingerprint %s\n",fpr_string
);
1087 add_to_strlist( &namelist
, fpr_string
);
1089 else if (!rc
&& !fpr
&& !did_key_byname
)
1092 rc
= G10ERR_NO_PUBKEY
;
1097 if (!rc
&& !did_key_byname
)
1101 get_pubkey_end (*retctx
);
1104 rc
= key_byname (anylocalfirst
?retctx
:NULL
,
1105 namelist
, pk
, NULL
, 0,
1106 include_unusable
, ret_keyblock
, ret_kdbhd
);
1111 log_info (_("automatically retrieved `%s' via %s\n"),
1115 if (rc
!= G10ERR_NO_PUBKEY
|| opt
.verbose
|| no_fingerprint
)
1116 log_info (_("error retrieving `%s' via %s: %s\n"),
1118 no_fingerprint
? _("No fingerprint"):g10_errstr(rc
));
1125 get_pubkey_end (*retctx
);
1129 if (retctx
&& *retctx
)
1131 assert (!(*retctx
)->extra_list
);
1132 (*retctx
)->extra_list
= namelist
;
1135 free_strlist (namelist
);
1141 get_pubkey_bynames( GETKEY_CTX
*retctx
, PKT_public_key
*pk
,
1142 strlist_t names
, KBNODE
*ret_keyblock
)
1144 return key_byname( retctx
, names
, pk
, NULL
, 0, 1, ret_keyblock
, NULL
);
1148 get_pubkey_next( GETKEY_CTX ctx
, PKT_public_key
*pk
, KBNODE
*ret_keyblock
)
1152 rc
= lookup( ctx
, ret_keyblock
, 0 );
1153 if ( !rc
&& pk
&& ret_keyblock
)
1154 pk_from_block ( ctx
, pk
, *ret_keyblock
);
1160 get_pubkey_end( GETKEY_CTX ctx
)
1163 memset (&ctx
->kbpos
, 0, sizeof ctx
->kbpos
);
1164 keydb_release (ctx
->kr_handle
);
1165 free_strlist (ctx
->extra_list
);
1166 if( !ctx
->not_allocated
)
1173 * Search for a key with the given fingerprint.
1175 * We should replace this with the _byname function. Thiscsan be done
1176 * by creating a userID conforming to the unified fingerprint style.
1179 get_pubkey_byfprint( PKT_public_key
*pk
,
1180 const byte
*fprint
, size_t fprint_len
)
1184 if( fprint_len
== 20 || fprint_len
== 16 ) {
1185 struct getkey_ctx_s ctx
;
1188 memset( &ctx
, 0, sizeof ctx
);
1190 ctx
.not_allocated
= 1;
1191 ctx
.kr_handle
= keydb_new (0);
1193 ctx
.items
[0].mode
= fprint_len
==16? KEYDB_SEARCH_MODE_FPR16
1194 : KEYDB_SEARCH_MODE_FPR20
;
1195 memcpy( ctx
.items
[0].u
.fpr
, fprint
, fprint_len
);
1196 rc
= lookup( &ctx
, &kb
, 0 );
1198 pk_from_block ( &ctx
, pk
, kb
);
1199 release_kbnode ( kb
);
1200 get_pubkey_end( &ctx
);
1203 rc
= G10ERR_GENERAL
; /* Oops */
1208 /* Get a public key and store it into the allocated pk. This function
1209 differs from get_pubkey_byfprint() in that it does not do a check
1210 of the key to avoid recursion. It should be used only in very
1211 certain cases. PK may be NULL to check just for the existance of
1214 get_pubkey_byfprint_fast (PKT_public_key
*pk
,
1215 const byte
*fprint
, size_t fprint_len
)
1220 byte fprbuf
[MAX_FINGERPRINT_LEN
];
1223 for (i
=0; i
< MAX_FINGERPRINT_LEN
&& i
< fprint_len
; i
++)
1224 fprbuf
[i
] = fprint
[i
];
1225 while (i
< MAX_FINGERPRINT_LEN
)
1229 rc
= keydb_search_fpr (hd
, fprbuf
);
1233 return G10ERR_NO_PUBKEY
;
1235 rc
= keydb_get_keyblock (hd
, &keyblock
);
1239 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc
));
1240 return G10ERR_NO_PUBKEY
;
1243 assert ( keyblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
1244 || keyblock
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
);
1246 copy_public_key (pk
, keyblock
->pkt
->pkt
.public_key
);
1247 release_kbnode (keyblock
);
1249 /* Not caching key here since it won't have all of the fields
1256 * Search for a key with the given fingerprint and return the
1257 * complete keyblock which may have more than only this key.
1260 get_keyblock_byfprint( KBNODE
*ret_keyblock
, const byte
*fprint
,
1265 if( fprint_len
== 20 || fprint_len
== 16 ) {
1266 struct getkey_ctx_s ctx
;
1268 memset( &ctx
, 0, sizeof ctx
);
1269 ctx
.not_allocated
= 1;
1270 ctx
.kr_handle
= keydb_new (0);
1272 ctx
.items
[0].mode
= fprint_len
==16? KEYDB_SEARCH_MODE_FPR16
1273 : KEYDB_SEARCH_MODE_FPR20
;
1274 memcpy( ctx
.items
[0].u
.fpr
, fprint
, fprint_len
);
1275 rc
= lookup( &ctx
, ret_keyblock
, 0 );
1276 get_pubkey_end( &ctx
);
1279 rc
= G10ERR_GENERAL
; /* Oops */
1286 * Get a secret key by name and store it into sk
1287 * If NAME is NULL use the default key
1290 get_seckey_byname2( GETKEY_CTX
*retctx
,
1291 PKT_secret_key
*sk
, const char *name
, int unprotect
,
1294 strlist_t namelist
= NULL
;
1295 int rc
,include_unusable
=1;
1297 /* If we have no name, try to use the default secret key. If we
1298 have no default, we'll use the first usable one. */
1300 if( !name
&& opt
.def_secret_key
&& *opt
.def_secret_key
)
1301 add_to_strlist( &namelist
, opt
.def_secret_key
);
1303 add_to_strlist( &namelist
, name
);
1307 rc
= key_byname( retctx
, namelist
, NULL
, sk
, 1, include_unusable
,
1310 free_strlist( namelist
);
1312 if( !rc
&& unprotect
)
1313 rc
= check_secret_key( sk
, 0 );
1319 get_seckey_byname( PKT_secret_key
*sk
, const char *name
, int unlock
)
1321 return get_seckey_byname2 ( NULL
, sk
, name
, unlock
, NULL
);
1326 get_seckey_bynames( GETKEY_CTX
*retctx
, PKT_secret_key
*sk
,
1327 strlist_t names
, KBNODE
*ret_keyblock
)
1329 return key_byname( retctx
, names
, NULL
, sk
, 1, 1, ret_keyblock
, NULL
);
1334 get_seckey_next( GETKEY_CTX ctx
, PKT_secret_key
*sk
, KBNODE
*ret_keyblock
)
1338 rc
= lookup( ctx
, ret_keyblock
, 1 );
1339 if ( !rc
&& sk
&& ret_keyblock
)
1340 sk_from_block ( ctx
, sk
, *ret_keyblock
);
1347 get_seckey_end( GETKEY_CTX ctx
)
1349 get_pubkey_end( ctx
);
1354 * Search for a key with the given fingerprint.
1356 * We should replace this with the _byname function. Thiscsan be done
1357 * by creating a userID conforming to the unified fingerprint style.
1360 get_seckey_byfprint( PKT_secret_key
*sk
,
1361 const byte
*fprint
, size_t fprint_len
)
1365 if( fprint_len
== 20 || fprint_len
== 16 ) {
1366 struct getkey_ctx_s ctx
;
1369 memset( &ctx
, 0, sizeof ctx
);
1371 ctx
.not_allocated
= 1;
1372 ctx
.kr_handle
= keydb_new (1);
1374 ctx
.items
[0].mode
= fprint_len
==16? KEYDB_SEARCH_MODE_FPR16
1375 : KEYDB_SEARCH_MODE_FPR20
;
1376 memcpy( ctx
.items
[0].u
.fpr
, fprint
, fprint_len
);
1377 rc
= lookup( &ctx
, &kb
, 1 );
1379 sk_from_block ( &ctx
, sk
, kb
);
1380 release_kbnode ( kb
);
1381 get_seckey_end( &ctx
);
1384 rc
= G10ERR_GENERAL
; /* Oops */
1389 /* Search for a secret key with the given fingerprint and return the
1390 complete keyblock which may have more than only this key. */
1392 get_seckeyblock_byfprint (KBNODE
*ret_keyblock
, const byte
*fprint
,
1396 struct getkey_ctx_s ctx
;
1398 if (fprint_len
!= 20 && fprint_len
== 16)
1399 return G10ERR_GENERAL
; /* Oops */
1401 memset (&ctx
, 0, sizeof ctx
);
1402 ctx
.not_allocated
= 1;
1403 ctx
.kr_handle
= keydb_new (1);
1405 ctx
.items
[0].mode
= (fprint_len
==16
1406 ? KEYDB_SEARCH_MODE_FPR16
1407 : KEYDB_SEARCH_MODE_FPR20
);
1408 memcpy (ctx
.items
[0].u
.fpr
, fprint
, fprint_len
);
1409 rc
= lookup (&ctx
, ret_keyblock
, 1);
1410 get_seckey_end (&ctx
);
1417 /************************************************
1418 ************* Merging stuff ********************
1419 ************************************************/
1422 * merge all selfsignatures with the keys.
1423 * FIXME: replace this at least for the public key parts
1424 * by merge_selfsigs.
1425 * It is still used in keyedit.c and
1426 * at 2 or 3 other places - check whether it is really needed.
1427 * It might be needed by the key edit and import stuff because
1428 * the keylock is changed.
1431 merge_keys_and_selfsig( KBNODE keyblock
)
1433 PKT_public_key
*pk
= NULL
;
1434 PKT_secret_key
*sk
= NULL
;
1437 u32 kid
[2] = { 0, 0 };
1440 if (keyblock
&& keyblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
) {
1441 /* divert to our new function */
1442 merge_selfsigs (keyblock
);
1445 /* still need the old one because the new one can't handle secret keys */
1447 for(k
=keyblock
; k
; k
= k
->next
) {
1448 if( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
1449 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
1450 pk
= k
->pkt
->pkt
.public_key
; sk
= NULL
;
1451 if( pk
->version
< 4 )
1452 pk
= NULL
; /* not needed for old keys */
1453 else if( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
)
1454 keyid_from_pk( pk
, kid
);
1455 else if( !pk
->expiredate
) { /* and subkey */
1456 /* insert the expiration date here */
1457 /*FIXME!!! pk->expiredate = subkeys_expiretime( k, kid );*/
1461 else if( k
->pkt
->pkttype
== PKT_SECRET_KEY
1462 || k
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1463 pk
= NULL
; sk
= k
->pkt
->pkt
.secret_key
;
1464 if( sk
->version
< 4 )
1466 else if( k
->pkt
->pkttype
== PKT_SECRET_KEY
)
1467 keyid_from_sk( sk
, kid
);
1470 else if( (pk
|| sk
) && k
->pkt
->pkttype
== PKT_SIGNATURE
1471 && (sig
=k
->pkt
->pkt
.signature
)->sig_class
>= 0x10
1472 && sig
->sig_class
<= 0x30 && sig
->version
> 3
1473 && !(sig
->sig_class
== 0x18 || sig
->sig_class
== 0x28)
1474 && sig
->keyid
[0] == kid
[0] && sig
->keyid
[1] == kid
[1] ) {
1475 /* okay this is a self-signature which can be used.
1476 * This is not used for subkey binding signature, becuase this
1478 * FIXME: We should only use this if the signature is valid
1479 * but this is time consuming - we must provide another
1480 * way to handle this
1485 p
= parse_sig_subpkt( sig
->hashed
, SIGSUBPKT_KEY_EXPIRE
, NULL
);
1487 ed
= p
? pk
->timestamp
+ buffer_to_u32(p
):0;
1488 if( sig
->timestamp
> sigdate
) {
1489 pk
->expiredate
= ed
;
1490 sigdate
= sig
->timestamp
;
1494 ed
= p
? sk
->timestamp
+ buffer_to_u32(p
):0;
1495 if( sig
->timestamp
> sigdate
) {
1496 sk
->expiredate
= ed
;
1497 sigdate
= sig
->timestamp
;
1502 if(pk
&& (pk
->expiredate
==0 ||
1503 (pk
->max_expiredate
&& pk
->expiredate
>pk
->max_expiredate
)))
1504 pk
->expiredate
=pk
->max_expiredate
;
1506 if(sk
&& (sk
->expiredate
==0 ||
1507 (sk
->max_expiredate
&& sk
->expiredate
>sk
->max_expiredate
)))
1508 sk
->expiredate
=sk
->max_expiredate
;
1513 parse_key_usage(PKT_signature
*sig
)
1520 p
=parse_sig_subpkt(sig
->hashed
,SIGSUBPKT_KEY_FLAGS
,&n
);
1523 /* first octet of the keyflags */
1528 key_usage
|= PUBKEY_USAGE_CERT
;
1534 key_usage
|= PUBKEY_USAGE_SIG
;
1538 /* We do not distinguish between encrypting communications and
1539 encrypting storage. */
1540 if(flags
& (0x04|0x08))
1542 key_usage
|= PUBKEY_USAGE_ENC
;
1543 flags
&=~(0x04|0x08);
1548 key_usage
|= PUBKEY_USAGE_AUTH
;
1553 key_usage
|= PUBKEY_USAGE_UNKNOWN
;
1556 /* We set PUBKEY_USAGE_UNKNOWN to indicate that this key has a
1557 capability that we do not handle. This serves to distinguish
1558 between a zero key usage which we handle as the default
1559 capabilities for that algorithm, and a usage that we do not
1566 * Apply information from SIGNODE (which is the valid self-signature
1567 * associated with that UID) to the UIDNODE:
1568 * - wether the UID has been revoked
1569 * - assumed creation date of the UID
1570 * - temporary store the keyflags here
1571 * - temporary store the key expiration time here
1572 * - mark whether the primary user ID flag hat been set.
1573 * - store the preferences
1576 fixup_uidnode ( KBNODE uidnode
, KBNODE signode
, u32 keycreated
)
1578 PKT_user_id
*uid
= uidnode
->pkt
->pkt
.user_id
;
1579 PKT_signature
*sig
= signode
->pkt
->pkt
.signature
;
1580 const byte
*p
, *sym
, *hash
, *zip
;
1581 size_t n
, nsym
, nhash
, nzip
;
1583 sig
->flags
.chosen_selfsig
= 1; /* we chose this one */
1584 uid
->created
= 0; /* not created == invalid */
1585 if ( IS_UID_REV ( sig
) )
1587 uid
->is_revoked
= 1;
1588 return; /* has been revoked */
1591 uid
->is_revoked
= 0;
1593 uid
->expiredate
= sig
->expiredate
;
1595 if (sig
->flags
.expired
)
1597 uid
->is_expired
= 1;
1598 return; /* has expired */
1601 uid
->is_expired
= 0;
1603 uid
->created
= sig
->timestamp
; /* this one is okay */
1604 uid
->selfsigversion
= sig
->version
;
1605 /* If we got this far, it's not expired :) */
1606 uid
->is_expired
= 0;
1608 /* store the key flags in the helper variable for later processing */
1609 uid
->help_key_usage
=parse_key_usage(sig
);
1611 /* ditto for the key expiration */
1612 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_KEY_EXPIRE
, NULL
);
1613 if( p
&& buffer_to_u32(p
) )
1614 uid
->help_key_expire
= keycreated
+ buffer_to_u32(p
);
1616 uid
->help_key_expire
= 0;
1618 /* Set the primary user ID flag - we will later wipe out some
1619 * of them to only have one in our keyblock */
1620 uid
->is_primary
= 0;
1621 p
= parse_sig_subpkt ( sig
->hashed
, SIGSUBPKT_PRIMARY_UID
, NULL
);
1623 uid
->is_primary
= 2;
1624 /* We could also query this from the unhashed area if it is not in
1625 * the hased area and then later try to decide which is the better
1626 * there should be no security problem with this.
1627 * For now we only look at the hashed one.
1630 /* Now build the preferences list. These must come from the
1631 hashed section so nobody can modify the ciphers a key is
1632 willing to accept. */
1633 p
= parse_sig_subpkt ( sig
->hashed
, SIGSUBPKT_PREF_SYM
, &n
);
1634 sym
= p
; nsym
= p
?n
:0;
1635 p
= parse_sig_subpkt ( sig
->hashed
, SIGSUBPKT_PREF_HASH
, &n
);
1636 hash
= p
; nhash
= p
?n
:0;
1637 p
= parse_sig_subpkt ( sig
->hashed
, SIGSUBPKT_PREF_COMPR
, &n
);
1638 zip
= p
; nzip
= p
?n
:0;
1641 n
= nsym
+ nhash
+ nzip
;
1645 uid
->prefs
= xmalloc (sizeof (*uid
->prefs
) * (n
+1));
1647 for (; nsym
; nsym
--, n
++) {
1648 uid
->prefs
[n
].type
= PREFTYPE_SYM
;
1649 uid
->prefs
[n
].value
= *sym
++;
1651 for (; nhash
; nhash
--, n
++) {
1652 uid
->prefs
[n
].type
= PREFTYPE_HASH
;
1653 uid
->prefs
[n
].value
= *hash
++;
1655 for (; nzip
; nzip
--, n
++) {
1656 uid
->prefs
[n
].type
= PREFTYPE_ZIP
;
1657 uid
->prefs
[n
].value
= *zip
++;
1659 uid
->prefs
[n
].type
= PREFTYPE_NONE
; /* end of list marker */
1660 uid
->prefs
[n
].value
= 0;
1663 /* see whether we have the MDC feature */
1665 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_FEATURES
, &n
);
1666 if (p
&& n
&& (p
[0] & 0x01))
1669 /* and the keyserver modify flag */
1670 uid
->flags
.ks_modify
= 1;
1671 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_KS_FLAGS
, &n
);
1672 if (p
&& n
&& (p
[0] & 0x80))
1673 uid
->flags
.ks_modify
= 0;
1677 sig_to_revoke_info(PKT_signature
*sig
,struct revoke_info
*rinfo
)
1679 rinfo
->date
= sig
->timestamp
;
1680 rinfo
->algo
= sig
->pubkey_algo
;
1681 rinfo
->keyid
[0] = sig
->keyid
[0];
1682 rinfo
->keyid
[1] = sig
->keyid
[1];
1686 merge_selfsigs_main(KBNODE keyblock
, int *r_revoked
, struct revoke_info
*rinfo
)
1688 PKT_public_key
*pk
= NULL
;
1691 u32 sigdate
, uiddate
, uiddate2
;
1692 KBNODE signode
, uidnode
, uidnode2
;
1693 u32 curtime
= make_timestamp ();
1694 unsigned int key_usage
= 0;
1695 u32 keytimestamp
= 0;
1697 int key_expire_seen
= 0;
1698 byte sigversion
= 0;
1701 memset(rinfo
,0,sizeof(*rinfo
));
1703 if ( keyblock
->pkt
->pkttype
!= PKT_PUBLIC_KEY
)
1705 pk
= keyblock
->pkt
->pkt
.public_key
;
1706 keytimestamp
= pk
->timestamp
;
1708 keyid_from_pk( pk
, kid
);
1709 pk
->main_keyid
[0] = kid
[0];
1710 pk
->main_keyid
[1] = kid
[1];
1712 if ( pk
->version
< 4 ) {
1713 /* before v4 the key packet itself contains the expiration
1714 * date and there was no way to change it, so we start with
1715 * the one from the key packet */
1716 key_expire
= pk
->max_expiredate
;
1717 key_expire_seen
= 1;
1720 /* first pass: find the latest direct key self-signature.
1721 * We assume that the newest one overrides all others
1724 /* In case this key was already merged */
1730 sigdate
= 0; /* helper to find the latest signature */
1731 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_USER_ID
; k
= k
->next
) {
1732 if ( k
->pkt
->pkttype
== PKT_SIGNATURE
) {
1733 PKT_signature
*sig
= k
->pkt
->pkt
.signature
;
1734 if ( sig
->keyid
[0] == kid
[0] && sig
->keyid
[1]==kid
[1] ) {
1735 if ( check_key_signature( keyblock
, k
, NULL
) )
1736 ; /* signature did not verify */
1737 else if ( IS_KEY_REV (sig
) ){
1738 /* key has been revoked - there is no way to override
1739 * such a revocation, so we theoretically can stop now.
1740 * We should not cope with expiration times for revocations
1741 * here because we have to assume that an attacker can
1742 * generate all kinds of signatures. However due to the
1743 * fact that the key has been revoked it does not harm
1744 * either and by continuing we gather some more info on
1748 sig_to_revoke_info(sig
,rinfo
);
1750 else if ( IS_KEY_SIG (sig
) ) {
1751 /* Add any revocation keys onto the pk. This is
1752 particularly interesting since we normally only
1753 get data from the most recent 1F signature, but
1754 you need multiple 1F sigs to properly handle
1755 revocation keys (PGP does it this way, and a
1756 revocation key could be sensitive and hence in a
1757 different signature). */
1762 xrealloc(pk
->revkey
,sizeof(struct revocation_key
)*
1763 (pk
->numrevkeys
+sig
->numrevkeys
));
1765 for(i
=0;i
<sig
->numrevkeys
;i
++)
1766 memcpy(&pk
->revkey
[pk
->numrevkeys
++],
1768 sizeof(struct revocation_key
));
1771 if( sig
->timestamp
>= sigdate
) {
1772 if(sig
->flags
.expired
)
1773 ; /* signature has expired - ignore it */
1775 sigdate
= sig
->timestamp
;
1777 if( sig
->version
> sigversion
)
1778 sigversion
= sig
->version
;
1787 /* Remove dupes from the revocation keys */
1791 int i
,j
,x
,changed
=0;
1793 for(i
=0;i
<pk
->numrevkeys
;i
++)
1795 for(j
=i
+1;j
<pk
->numrevkeys
;j
++)
1797 if(memcmp(&pk
->revkey
[i
],&pk
->revkey
[j
],
1798 sizeof(struct revocation_key
))==0)
1802 for(x
=j
;x
<pk
->numrevkeys
-1;x
++)
1803 pk
->revkey
[x
]=pk
->revkey
[x
+1];
1813 pk
->revkey
=xrealloc(pk
->revkey
,
1814 pk
->numrevkeys
*sizeof(struct revocation_key
));
1819 /* some information from a direct key signature take precedence
1820 * over the same information given in UID sigs.
1822 PKT_signature
*sig
= signode
->pkt
->pkt
.signature
;
1825 key_usage
=parse_key_usage(sig
);
1827 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_KEY_EXPIRE
, NULL
);
1828 if( p
&& buffer_to_u32(p
) )
1830 key_expire
= keytimestamp
+ buffer_to_u32(p
);
1831 key_expire_seen
= 1;
1834 /* mark that key as valid: one direct key signature should
1835 * render a key as valid */
1839 /* pass 1.5: look for key revocation signatures that were not made
1840 by the key (i.e. did a revocation key issue a revocation for
1841 us?). Only bother to do this if there is a revocation key in
1842 the first place and we're not revoked already. */
1844 if(!*r_revoked
&& pk
->revkey
)
1845 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_USER_ID
; k
= k
->next
)
1847 if ( k
->pkt
->pkttype
== PKT_SIGNATURE
)
1849 PKT_signature
*sig
= k
->pkt
->pkt
.signature
;
1851 if(IS_KEY_REV(sig
) &&
1852 (sig
->keyid
[0]!=kid
[0] || sig
->keyid
[1]!=kid
[1]))
1854 int rc
=check_revocation_keys(pk
,sig
);
1858 sig_to_revoke_info(sig
,rinfo
);
1859 /* don't continue checking since we can't be any
1860 more revoked than this */
1863 else if(rc
==G10ERR_NO_PUBKEY
)
1864 pk
->maybe_revoked
=1;
1866 /* A failure here means the sig did not verify, was
1867 not issued by a revocation key, or a revocation
1868 key loop was broken. If a revocation key isn't
1869 findable, however, the key might be revoked and
1870 we don't know it. */
1872 /* TODO: In the future handle subkey and cert
1873 revocations? PGP doesn't, but it's in 2440. */
1878 /* second pass: look at the self-signature of all user IDs */
1879 signode
= uidnode
= NULL
;
1880 sigdate
= 0; /* helper to find the latest signature in one user ID */
1881 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
; k
= k
->next
) {
1882 if ( k
->pkt
->pkttype
== PKT_USER_ID
) {
1883 if ( uidnode
&& signode
)
1885 fixup_uidnode ( uidnode
, signode
, keytimestamp
);
1892 else if ( k
->pkt
->pkttype
== PKT_SIGNATURE
&& uidnode
) {
1893 PKT_signature
*sig
= k
->pkt
->pkt
.signature
;
1894 if ( sig
->keyid
[0] == kid
[0] && sig
->keyid
[1]==kid
[1] ) {
1895 if ( check_key_signature( keyblock
, k
, NULL
) )
1896 ; /* signature did not verify */
1897 else if ( (IS_UID_SIG (sig
) || IS_UID_REV (sig
))
1898 && sig
->timestamp
>= sigdate
)
1900 /* Note: we allow to invalidate cert revocations
1901 * by a newer signature. An attacker can't use this
1902 * because a key should be revoced with a key revocation.
1903 * The reason why we have to allow for that is that at
1904 * one time an email address may become invalid but later
1905 * the same email address may become valid again (hired,
1906 * fired, hired again).
1909 sigdate
= sig
->timestamp
;
1911 signode
->pkt
->pkt
.signature
->flags
.chosen_selfsig
=0;
1912 if( sig
->version
> sigversion
)
1913 sigversion
= sig
->version
;
1918 if ( uidnode
&& signode
) {
1919 fixup_uidnode ( uidnode
, signode
, keytimestamp
);
1923 /* If the key isn't valid yet, and we have
1924 --allow-non-selfsigned-uid set, then force it valid. */
1925 if(!pk
->is_valid
&& opt
.allow_non_selfsigned_uid
)
1928 log_info(_("Invalid key %s made valid by"
1929 " --allow-non-selfsigned-uid\n"),keystr_from_pk(pk
));
1933 /* The key STILL isn't valid, so try and find an ultimately
1934 trusted signature. */
1939 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
; k
=k
->next
)
1941 if ( k
->pkt
->pkttype
== PKT_USER_ID
)
1943 else if ( k
->pkt
->pkttype
== PKT_SIGNATURE
&& uidnode
)
1945 PKT_signature
*sig
= k
->pkt
->pkt
.signature
;
1947 if(sig
->keyid
[0] != kid
[0] || sig
->keyid
[1]!=kid
[1])
1949 PKT_public_key
*ultimate_pk
;
1951 ultimate_pk
=xmalloc_clear(sizeof(*ultimate_pk
));
1953 /* We don't want to use the full get_pubkey to
1954 avoid infinite recursion in certain cases.
1955 There is no reason to check that an ultimately
1956 trusted key is still valid - if it has been
1957 revoked or the user should also renmove the
1958 ultimate trust flag. */
1959 if(get_pubkey_fast(ultimate_pk
,sig
->keyid
)==0
1960 && check_key_signature2(keyblock
,k
,ultimate_pk
,
1961 NULL
,NULL
,NULL
,NULL
)==0
1962 && get_ownertrust(ultimate_pk
)==TRUST_ULTIMATE
)
1964 free_public_key(ultimate_pk
);
1969 free_public_key(ultimate_pk
);
1975 /* Record the highest selfsig version so we know if this is a v3
1976 key through and through, or a v3 key with a v4 selfsig
1977 somewhere. This is useful in a few places to know if the key
1978 must be treated as PGP2-style or OpenPGP-style. Note that a
1979 selfsig revocation with a higher version number will also raise
1980 this value. This is okay since such a revocation must be
1981 issued by the user (i.e. it cannot be issued by someone else to
1982 modify the key behavior.) */
1984 pk
->selfsigversion
=sigversion
;
1986 /* Now that we had a look at all user IDs we can now get some information
1987 * from those user IDs.
1991 /* find the latest user ID with key flags set */
1992 uiddate
= 0; /* helper to find the latest user ID */
1993 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
1995 if ( k
->pkt
->pkttype
== PKT_USER_ID
) {
1996 PKT_user_id
*uid
= k
->pkt
->pkt
.user_id
;
1997 if ( uid
->help_key_usage
&& uid
->created
> uiddate
) {
1998 key_usage
= uid
->help_key_usage
;
1999 uiddate
= uid
->created
;
2004 if ( !key_usage
) { /* no key flags at all: get it from the algo */
2005 key_usage
= openpgp_pk_algo_usage ( pk
->pubkey_algo
);
2007 else { /* check that the usage matches the usage as given by the algo */
2008 int x
= openpgp_pk_algo_usage ( pk
->pubkey_algo
);
2009 if ( x
) /* mask it down to the actual allowed usage */
2013 /* Whatever happens, it's a primary key, so it can certify. */
2014 pk
->pubkey_usage
= key_usage
|PUBKEY_USAGE_CERT
;
2016 if ( !key_expire_seen
) {
2017 /* find the latest valid user ID with a key expiration set
2018 * Note, that this may be a different one from the above because
2019 * some user IDs may have no expiration date set */
2021 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
2023 if ( k
->pkt
->pkttype
== PKT_USER_ID
) {
2024 PKT_user_id
*uid
= k
->pkt
->pkt
.user_id
;
2025 if ( uid
->help_key_expire
&& uid
->created
> uiddate
) {
2026 key_expire
= uid
->help_key_expire
;
2027 uiddate
= uid
->created
;
2033 /* Currently only v3 keys have a maximum expiration date, but I'll
2034 bet v5 keys get this feature again. */
2035 if(key_expire
==0 || (pk
->max_expiredate
&& key_expire
>pk
->max_expiredate
))
2036 key_expire
=pk
->max_expiredate
;
2038 pk
->has_expired
= key_expire
>= curtime
? 0 : key_expire
;
2039 pk
->expiredate
= key_expire
;
2041 /* Fixme: we should see how to get rid of the expiretime fields but
2042 * this needs changes at other places too. */
2044 /* and now find the real primary user ID and delete all others */
2045 uiddate
= uiddate2
= 0;
2046 uidnode
= uidnode2
= NULL
;
2047 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
; k
= k
->next
) {
2048 if ( k
->pkt
->pkttype
== PKT_USER_ID
&&
2049 !k
->pkt
->pkt
.user_id
->attrib_data
) {
2050 PKT_user_id
*uid
= k
->pkt
->pkt
.user_id
;
2051 if (uid
->is_primary
)
2053 if(uid
->created
> uiddate
)
2055 uiddate
= uid
->created
;
2058 else if(uid
->created
==uiddate
&& uidnode
)
2060 /* The dates are equal, so we need to do a
2061 different (and arbitrary) comparison. This
2062 should rarely, if ever, happen. It's good to
2063 try and guarantee that two different GnuPG
2064 users with two different keyrings at least pick
2065 the same primary. */
2066 if(cmp_user_ids(uid
,uidnode
->pkt
->pkt
.user_id
)>0)
2072 if(uid
->created
> uiddate2
)
2074 uiddate2
= uid
->created
;
2077 else if(uid
->created
==uiddate2
&& uidnode2
)
2079 if(cmp_user_ids(uid
,uidnode2
->pkt
->pkt
.user_id
)>0)
2086 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
2088 if ( k
->pkt
->pkttype
== PKT_USER_ID
&&
2089 !k
->pkt
->pkt
.user_id
->attrib_data
) {
2090 PKT_user_id
*uid
= k
->pkt
->pkt
.user_id
;
2092 uid
->is_primary
= 0;
2096 else if( uidnode2
) {
2097 /* none is flagged primary - use the latest user ID we have,
2098 and disambiguate with the arbitrary packet comparison. */
2099 uidnode2
->pkt
->pkt
.user_id
->is_primary
= 1;
2103 /* None of our uids were self-signed, so pick the one that
2104 sorts first to be the primary. This is the best we can do
2105 here since there are no self sigs to date the uids. */
2109 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
2112 if(k
->pkt
->pkttype
==PKT_USER_ID
2113 && !k
->pkt
->pkt
.user_id
->attrib_data
)
2118 uidnode
->pkt
->pkt
.user_id
->is_primary
=1;
2123 if(cmp_user_ids(k
->pkt
->pkt
.user_id
,
2124 uidnode
->pkt
->pkt
.user_id
)>0)
2126 uidnode
->pkt
->pkt
.user_id
->is_primary
=0;
2128 uidnode
->pkt
->pkt
.user_id
->is_primary
=1;
2131 k
->pkt
->pkt
.user_id
->is_primary
=0; /* just to be
2139 /* Convert a buffer to a signature. Useful for 0x19 embedded sigs.
2140 Caller must free the signature when they are done. */
2141 static PKT_signature
*
2142 buf_to_sig(const byte
*buf
,size_t len
)
2144 PKT_signature
*sig
=xmalloc_clear(sizeof(PKT_signature
));
2145 IOBUF iobuf
=iobuf_temp_with_content(buf
,len
);
2146 int save_mode
=set_packet_list_mode(0);
2148 if(parse_signature(iobuf
,PKT_SIGNATURE
,len
,sig
)!=0)
2154 set_packet_list_mode(save_mode
);
2161 merge_selfsigs_subkey( KBNODE keyblock
, KBNODE subnode
)
2163 PKT_public_key
*mainpk
= NULL
, *subpk
= NULL
;
2169 u32 curtime
= make_timestamp ();
2170 unsigned int key_usage
= 0;
2171 u32 keytimestamp
= 0;
2175 if ( subnode
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
)
2177 mainpk
= keyblock
->pkt
->pkt
.public_key
;
2178 if ( mainpk
->version
< 4 )
2179 return; /* (actually this should never happen) */
2180 keyid_from_pk( mainpk
, mainkid
);
2181 subpk
= subnode
->pkt
->pkt
.public_key
;
2182 keytimestamp
= subpk
->timestamp
;
2184 subpk
->is_valid
= 0;
2185 subpk
->main_keyid
[0] = mainpk
->main_keyid
[0];
2186 subpk
->main_keyid
[1] = mainpk
->main_keyid
[1];
2188 /* find the latest key binding self-signature. */
2190 sigdate
= 0; /* helper to find the latest signature */
2191 for(k
=subnode
->next
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
2193 if ( k
->pkt
->pkttype
== PKT_SIGNATURE
) {
2194 sig
= k
->pkt
->pkt
.signature
;
2195 if ( sig
->keyid
[0] == mainkid
[0] && sig
->keyid
[1]==mainkid
[1] ) {
2196 if ( check_key_signature( keyblock
, k
, NULL
) )
2197 ; /* signature did not verify */
2198 else if ( IS_SUBKEY_REV (sig
) ) {
2199 /* Note that this means that the date on a
2200 revocation sig does not matter - even if the
2201 binding sig is dated after the revocation sig,
2202 the subkey is still marked as revoked. This
2203 seems ok, as it is just as easy to make new
2204 subkeys rather than re-sign old ones as the
2205 problem is in the distribution. Plus, PGP (7)
2206 does this the same way. */
2207 subpk
->is_revoked
= 1;
2208 sig_to_revoke_info(sig
,&subpk
->revoked
);
2209 /* although we could stop now, we continue to
2210 * figure out other information like the old expiration
2213 else if ( IS_SUBKEY_SIG (sig
) && sig
->timestamp
>= sigdate
)
2215 if(sig
->flags
.expired
)
2216 ; /* signature has expired - ignore it */
2219 sigdate
= sig
->timestamp
;
2221 signode
->pkt
->pkt
.signature
->flags
.chosen_selfsig
=0;
2228 /* no valid key binding */
2232 sig
= signode
->pkt
->pkt
.signature
;
2233 sig
->flags
.chosen_selfsig
=1; /* so we know which selfsig we chose later */
2235 key_usage
=parse_key_usage(sig
);
2238 /* no key flags at all: get it from the algo */
2239 key_usage
= openpgp_pk_algo_usage ( subpk
->pubkey_algo
);
2243 /* check that the usage matches the usage as given by the algo */
2244 int x
= openpgp_pk_algo_usage ( subpk
->pubkey_algo
);
2245 if ( x
) /* mask it down to the actual allowed usage */
2249 subpk
->pubkey_usage
= key_usage
;
2251 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_KEY_EXPIRE
, NULL
);
2252 if ( p
&& buffer_to_u32(p
) )
2253 key_expire
= keytimestamp
+ buffer_to_u32(p
);
2256 subpk
->has_expired
= key_expire
>= curtime
? 0 : key_expire
;
2257 subpk
->expiredate
= key_expire
;
2259 /* algo doesn't exist */
2260 if(openpgp_pk_test_algo(subpk
->pubkey_algo
))
2263 subpk
->is_valid
= 1;
2265 /* Find the most recent 0x19 embedded signature on our self-sig. */
2266 if(subpk
->backsig
==0)
2270 PKT_signature
*backsig
=NULL
;
2274 /* We do this while() since there may be other embedded
2275 signatures in the future. We only want 0x19 here. */
2277 while((p
=enum_sig_subpkt(sig
->hashed
,
2278 SIGSUBPKT_SIGNATURE
,&n
,&seq
,NULL
)))
2279 if(n
>3 && ((p
[0]==3 && p
[2]==0x19) || (p
[0]==4 && p
[1]==0x19)))
2281 PKT_signature
*tempsig
=buf_to_sig(p
,n
);
2284 if(tempsig
->timestamp
>sigdate
)
2287 free_seckey_enc(backsig
);
2290 sigdate
=backsig
->timestamp
;
2293 free_seckey_enc(tempsig
);
2299 /* It is safe to have this in the unhashed area since the 0x19
2300 is located on the selfsig for convenience, not security. */
2302 while((p
=enum_sig_subpkt(sig
->unhashed
,SIGSUBPKT_SIGNATURE
,
2304 if(n
>3 && ((p
[0]==3 && p
[2]==0x19) || (p
[0]==4 && p
[1]==0x19)))
2306 PKT_signature
*tempsig
=buf_to_sig(p
,n
);
2309 if(tempsig
->timestamp
>sigdate
)
2312 free_seckey_enc(backsig
);
2315 sigdate
=backsig
->timestamp
;
2318 free_seckey_enc(tempsig
);
2324 /* At ths point, backsig contains the most recent 0x19 sig.
2325 Let's see if it is good. */
2327 /* 2==valid, 1==invalid, 0==didn't check */
2328 if(check_backsig(mainpk
,subpk
,backsig
)==0)
2333 free_seckey_enc(backsig
);
2340 * Merge information from the self-signatures with the key, so that
2341 * we can later use them more easy.
2342 * The function works by first applying the self signatures to the
2343 * primary key and the to each subkey.
2344 * Here are the rules we use to decide which inormation from which
2345 * self-signature is used:
2346 * We check all self signatures or validity and ignore all invalid signatures.
2347 * All signatures are then ordered by their creation date ....
2348 * For the primary key:
2352 merge_selfsigs( KBNODE keyblock
)
2356 struct revoke_info rinfo
;
2357 PKT_public_key
*main_pk
;
2361 if ( keyblock
->pkt
->pkttype
!= PKT_PUBLIC_KEY
) {
2362 if (keyblock
->pkt
->pkttype
== PKT_SECRET_KEY
) {
2363 log_error ("expected public key but found secret key "
2365 /* we better exit here becuase a public key is expected at
2366 other places too. FIXME: Figure this out earlier and
2367 don't get to here at all */
2373 merge_selfsigs_main ( keyblock
, &revoked
, &rinfo
);
2375 /* now merge in the data from each of the subkeys */
2376 for(k
=keyblock
; k
; k
= k
->next
) {
2377 if ( k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2378 merge_selfsigs_subkey ( keyblock
, k
);
2382 main_pk
= keyblock
->pkt
->pkt
.public_key
;
2383 if ( revoked
|| main_pk
->has_expired
|| !main_pk
->is_valid
) {
2384 /* if the primary key is revoked, expired, or invalid we
2385 * better set the appropriate flags on that key and all
2387 for(k
=keyblock
; k
; k
= k
->next
) {
2388 if ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
2389 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2390 PKT_public_key
*pk
= k
->pkt
->pkt
.public_key
;
2391 if(!main_pk
->is_valid
)
2393 if(revoked
&& !pk
->is_revoked
)
2395 pk
->is_revoked
= revoked
;
2396 memcpy(&pk
->revoked
,&rinfo
,sizeof(rinfo
));
2398 if(main_pk
->has_expired
)
2399 pk
->has_expired
= main_pk
->has_expired
;
2405 /* set the preference list of all keys to those of the primary real
2406 * user ID. Note: we use these preferences when we don't know by
2407 * which user ID the key has been selected.
2408 * fixme: we should keep atoms of commonly used preferences or
2409 * use reference counting to optimize the preference lists storage.
2410 * FIXME: it might be better to use the intersection of
2412 * Do a similar thing for the MDC feature flag.
2416 for (k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
; k
= k
->next
) {
2417 if (k
->pkt
->pkttype
== PKT_USER_ID
2418 && !k
->pkt
->pkt
.user_id
->attrib_data
2419 && k
->pkt
->pkt
.user_id
->is_primary
) {
2420 prefs
= k
->pkt
->pkt
.user_id
->prefs
;
2421 mdc_feature
= k
->pkt
->pkt
.user_id
->flags
.mdc
;
2425 for(k
=keyblock
; k
; k
= k
->next
) {
2426 if ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
2427 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2428 PKT_public_key
*pk
= k
->pkt
->pkt
.public_key
;
2431 pk
->prefs
= copy_prefs (prefs
);
2432 pk
->mdc_feature
= mdc_feature
;
2439 * Merge the secret keys from secblock into the pubblock thereby
2440 * replacing the public (sub)keys with their secret counterparts Hmmm:
2441 * It might be better to get away from the concept of entire secret
2442 * keys at all and have a way to store just the real secret parts
2446 merge_public_with_secret ( KBNODE pubblock
, KBNODE secblock
)
2450 assert ( pubblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
);
2451 assert ( secblock
->pkt
->pkttype
== PKT_SECRET_KEY
);
2453 for (pub
=pubblock
; pub
; pub
= pub
->next
) {
2454 if ( pub
->pkt
->pkttype
== PKT_PUBLIC_KEY
) {
2455 PKT_public_key
*pk
= pub
->pkt
->pkt
.public_key
;
2456 PKT_secret_key
*sk
= secblock
->pkt
->pkt
.secret_key
;
2457 assert ( pub
== pubblock
); /* only in the first node */
2458 /* there is nothing to compare in this case, so just replace
2459 * some information */
2460 copy_public_parts_to_secret_key ( pk
, sk
);
2461 free_public_key ( pk
);
2462 pub
->pkt
->pkttype
= PKT_SECRET_KEY
;
2463 pub
->pkt
->pkt
.secret_key
= copy_secret_key (NULL
, sk
);
2465 else if ( pub
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2467 PKT_public_key
*pk
= pub
->pkt
->pkt
.public_key
;
2469 /* this is more complicated: it may happen that the sequence
2470 * of the subkeys dosn't match, so we have to find the
2471 * appropriate secret key */
2472 for (sec
=secblock
->next
; sec
; sec
= sec
->next
) {
2473 if ( sec
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
2474 PKT_secret_key
*sk
= sec
->pkt
->pkt
.secret_key
;
2475 if ( !cmp_public_secret_key ( pk
, sk
) ) {
2476 copy_public_parts_to_secret_key ( pk
, sk
);
2477 free_public_key ( pk
);
2478 pub
->pkt
->pkttype
= PKT_SECRET_SUBKEY
;
2479 pub
->pkt
->pkt
.secret_key
= copy_secret_key (NULL
, sk
);
2485 BUG(); /* already checked in premerge */
2490 /* This function checks that for every public subkey a corresponding
2491 * secret subkey is available and deletes the public subkey otherwise.
2492 * We need this function because we can't delete it later when we
2493 * actually merge the secret parts into the pubring.
2494 * The function also plays some games with the node flags.
2497 premerge_public_with_secret ( KBNODE pubblock
, KBNODE secblock
)
2501 assert ( pubblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
);
2502 assert ( secblock
->pkt
->pkttype
== PKT_SECRET_KEY
);
2504 for (pub
=pubblock
,last
=NULL
; pub
; last
= pub
, pub
= pub
->next
) {
2505 pub
->flag
&= ~3; /* reset bits 0 and 1 */
2506 if ( pub
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2508 PKT_public_key
*pk
= pub
->pkt
->pkt
.public_key
;
2510 for (sec
=secblock
->next
; sec
; sec
= sec
->next
) {
2511 if ( sec
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
2512 PKT_secret_key
*sk
= sec
->pkt
->pkt
.secret_key
;
2513 if ( !cmp_public_secret_key ( pk
, sk
) ) {
2514 if ( sk
->protect
.s2k
.mode
== 1001 ) {
2515 /* The secret parts are not available so
2516 we can't use that key for signing etc.
2517 Fix the pubkey usage */
2518 pk
->pubkey_usage
&= ~(PUBKEY_USAGE_SIG
2519 |PUBKEY_USAGE_AUTH
);
2521 /* transfer flag bits 0 and 1 to the pubblock */
2522 pub
->flag
|= (sec
->flag
&3);
2531 log_info (_("no secret subkey"
2532 " for public subkey %s - ignoring\n"),
2533 keystr_from_pk (pk
));
2534 /* we have to remove the subkey in this case */
2536 /* find the next subkey */
2537 for (next
=pub
->next
,ll
=pub
;
2538 next
&& next
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
2539 ll
= next
, next
= next
->next
)
2543 /* release this public subkey with all sigs */
2545 release_kbnode( pub
);
2546 /* let the loop continue */
2551 /* We need to copy the found bits (0 and 1) from the secret key to
2552 the public key. This has already been done for the subkeys but
2553 got lost on the primary key - fix it here *. */
2554 pubblock
->flag
|= (secblock
->flag
& 3);
2560 /* See see whether the key fits
2561 * our requirements and in case we do not
2562 * request the primary key, we should select
2563 * a suitable subkey.
2564 * FIXME: Check against PGP 7 whether we still need a kludge
2565 * to favor type 16 keys over type 20 keys when type 20
2566 * has not been explitely requested.
2567 * Returns: True when a suitable key has been found.
2569 * We have to distinguish four cases: FIXME!
2570 * 1. No usage and no primary key requested
2571 * Examples for this case are that we have a keyID to be used
2572 * for decrytion or verification.
2573 * 2. No usage but primary key requested
2574 * This is the case for all functions which work on an
2575 * entire keyblock, e.g. for editing or listing
2576 * 3. Usage and primary key requested
2578 * 4. Usage but no primary key requested
2580 * FIXME: Tell what is going to happen here and something about the rationale
2581 * Note: We don't use this function if no specific usage is requested;
2582 * This way the getkey functions can be used for plain key listings.
2584 * CTX ist the keyblock we are investigating, if FOUNDK is not NULL this
2585 * is the key we actually found by looking at the keyid or a fingerprint and
2586 * may eitehr point to the primary or one of the subkeys.
2590 finish_lookup (GETKEY_CTX ctx
)
2592 KBNODE keyblock
= ctx
->keyblock
;
2594 KBNODE foundk
= NULL
;
2595 PKT_user_id
*foundu
= NULL
;
2596 #define USAGE_MASK (PUBKEY_USAGE_SIG|PUBKEY_USAGE_ENC|PUBKEY_USAGE_CERT)
2597 unsigned int req_usage
= ( ctx
->req_usage
& USAGE_MASK
);
2598 /* Request the primary if we're certifying another key, and also
2599 if signing data while --pgp6 or --pgp7 is on since pgp 6 and 7
2600 do not understand signatures made by a signing subkey. PGP 8
2602 int req_prim
= (ctx
->req_usage
& PUBKEY_USAGE_CERT
) ||
2603 ((PGP6
|| PGP7
) && (ctx
->req_usage
& PUBKEY_USAGE_SIG
));
2606 u32 curtime
= make_timestamp ();
2608 assert( keyblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
);
2610 ctx
->found_key
= NULL
;
2613 for (k
=keyblock
; k
; k
= k
->next
) {
2614 if ( (k
->flag
& 1) ) {
2615 assert ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
2616 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
);
2623 for (k
=keyblock
; k
; k
= k
->next
) {
2624 if ( (k
->flag
& 2) ) {
2625 assert (k
->pkt
->pkttype
== PKT_USER_ID
);
2626 foundu
= k
->pkt
->pkt
.user_id
;
2632 log_debug( "finish_lookup: checking key %08lX (%s)(req_usage=%x)\n",
2633 (ulong
)keyid_from_pk( keyblock
->pkt
->pkt
.public_key
, NULL
),
2634 foundk
? "one":"all", req_usage
);
2637 latest_key
= foundk
? foundk
:keyblock
;
2642 PKT_public_key
*pk
= foundk
->pkt
->pkt
.public_key
;
2644 free_user_id (pk
->user_id
);
2645 pk
->user_id
= scopy_user_id (foundu
);
2646 ctx
->found_key
= foundk
;
2647 cache_user_id( keyblock
);
2648 return 1; /* found */
2653 /* do not look at subkeys if a certification key is requested */
2654 if ((!foundk
|| foundk
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) && !req_prim
) {
2656 /* either start a loop or check just this one subkey */
2657 for (k
=foundk
?foundk
:keyblock
; k
; k
= nextk
) {
2660 if ( k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
)
2663 nextk
= NULL
; /* what a hack */
2664 pk
= k
->pkt
->pkt
.public_key
;
2666 log_debug( "\tchecking subkey %08lX\n",
2667 (ulong
)keyid_from_pk( pk
, NULL
));
2668 if ( !pk
->is_valid
) {
2670 log_debug( "\tsubkey not valid\n");
2673 if ( pk
->is_revoked
) {
2675 log_debug( "\tsubkey has been revoked\n");
2678 if ( pk
->has_expired
) {
2680 log_debug( "\tsubkey has expired\n");
2683 if ( pk
->timestamp
> curtime
&& !opt
.ignore_valid_from
) {
2685 log_debug( "\tsubkey not yet valid\n");
2689 if ( !((pk
->pubkey_usage
&USAGE_MASK
) & req_usage
) ) {
2691 log_debug( "\tusage does not match: want=%x have=%x\n",
2692 req_usage
, pk
->pubkey_usage
);
2697 log_debug( "\tsubkey looks fine\n");
2698 if ( pk
->timestamp
> latest_date
) {
2699 latest_date
= pk
->timestamp
;
2705 /* Okay now try the primary key unless we want an exact
2706 * key ID match on a subkey */
2707 if ((!latest_key
&& !(ctx
->exact
&& foundk
!= keyblock
)) || req_prim
) {
2709 if (DBG_CACHE
&& !foundk
&& !req_prim
)
2710 log_debug( "\tno suitable subkeys found - trying primary\n");
2711 pk
= keyblock
->pkt
->pkt
.public_key
;
2712 if ( !pk
->is_valid
) {
2714 log_debug( "\tprimary key not valid\n");
2716 else if ( pk
->is_revoked
) {
2718 log_debug( "\tprimary key has been revoked\n");
2720 else if ( pk
->has_expired
) {
2722 log_debug( "\tprimary key has expired\n");
2724 else if ( !((pk
->pubkey_usage
&USAGE_MASK
) & req_usage
) ) {
2726 log_debug( "\tprimary key usage does not match: "
2727 "want=%x have=%x\n",
2728 req_usage
, pk
->pubkey_usage
);
2732 log_debug( "\tprimary key may be used\n");
2733 latest_key
= keyblock
;
2734 latest_date
= pk
->timestamp
;
2738 if ( !latest_key
) {
2740 log_debug("\tno suitable key found - giving up\n");
2746 log_debug( "\tusing key %08lX\n",
2747 (ulong
)keyid_from_pk( latest_key
->pkt
->pkt
.public_key
, NULL
) );
2750 PKT_public_key
*pk
= latest_key
->pkt
->pkt
.public_key
;
2752 free_user_id (pk
->user_id
);
2753 pk
->user_id
= scopy_user_id (foundu
);
2756 ctx
->found_key
= latest_key
;
2758 if (latest_key
!= keyblock
&& opt
.verbose
)
2761 xstrdup(keystr_from_pk(latest_key
->pkt
->pkt
.public_key
));
2762 log_info(_("using subkey %s instead of primary key %s\n"),
2763 tempkeystr
, keystr_from_pk(keyblock
->pkt
->pkt
.public_key
));
2767 cache_user_id( keyblock
);
2769 return 1; /* found */
2774 lookup( GETKEY_CTX ctx
, KBNODE
*ret_keyblock
, int secmode
)
2777 KBNODE secblock
= NULL
; /* helper */
2778 int no_suitable_key
= 0;
2781 while (!(rc
= keydb_search (ctx
->kr_handle
, ctx
->items
, ctx
->nitems
))) {
2782 /* If we are searching for the first key we have to make sure
2783 that the next iteration does not do an implicit reset.
2784 This can be triggered by an empty key ring. */
2785 if (ctx
->nitems
&& ctx
->items
->mode
== KEYDB_SEARCH_MODE_FIRST
)
2786 ctx
->items
->mode
= KEYDB_SEARCH_MODE_NEXT
;
2788 rc
= keydb_get_keyblock (ctx
->kr_handle
, &ctx
->keyblock
);
2790 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc
));
2796 /* find the correspondig public key and use this
2797 * this one for the selection process */
2799 KBNODE k
= ctx
->keyblock
;
2801 if (k
->pkt
->pkttype
!= PKT_SECRET_KEY
)
2804 keyid_from_sk (k
->pkt
->pkt
.secret_key
, aki
);
2805 k
= get_pubkeyblock (aki
);
2809 log_info(_("key %s: secret key without public key"
2810 " - skipped\n"), keystr(aki
));
2813 secblock
= ctx
->keyblock
;
2816 premerge_public_with_secret ( ctx
->keyblock
, secblock
);
2819 /* warning: node flag bits 0 and 1 should be preserved by
2820 * merge_selfsigs. For secret keys, premerge did tranfer the
2821 * keys to the keyblock */
2822 merge_selfsigs ( ctx
->keyblock
);
2823 if ( finish_lookup (ctx
) ) {
2824 no_suitable_key
= 0;
2826 merge_public_with_secret ( ctx
->keyblock
,
2828 release_kbnode (secblock
);
2834 no_suitable_key
= 1;
2837 /* release resources and continue search */
2839 release_kbnode( secblock
);
2842 release_kbnode( ctx
->keyblock
);
2843 ctx
->keyblock
= NULL
;
2847 if( rc
&& rc
!= -1 )
2848 log_error("keydb_search failed: %s\n", g10_errstr(rc
));
2851 *ret_keyblock
= ctx
->keyblock
; /* return the keyblock */
2852 ctx
->keyblock
= NULL
;
2854 else if (rc
== -1 && no_suitable_key
)
2855 rc
= secmode
? G10ERR_UNU_SECKEY
: G10ERR_UNU_PUBKEY
;
2857 rc
= secmode
? G10ERR_NO_SECKEY
: G10ERR_NO_PUBKEY
;
2860 release_kbnode( secblock
);
2863 release_kbnode( ctx
->keyblock
);
2864 ctx
->keyblock
= NULL
;
2874 * FIXME: Replace by the generic function
2875 * It does not work as it is right now - it is used at
2876 * 2 places: a) to get the key for an anonyous recipient
2877 * b) to get the ultimately trusted keys.
2878 * The a) usage might have some problems.
2880 * set with_subkeys true to include subkeys
2881 * set with_spm true to include secret-parts-missing keys
2883 * Enumerate all primary secret keys. Caller must use these procedure:
2884 * 1) create a void pointer and initialize it to NULL
2885 * 2) pass this void pointer by reference to this function
2886 * and provide space for the secret key (pass a buffer for sk)
2887 * 3) call this function as long as it does not return -1
2889 * 4) Always call this function a last time with SK set to NULL,
2890 * so that can free it's context.
2893 enum_secret_keys( void **context
, PKT_secret_key
*sk
,
2894 int with_subkeys
, int with_spm
)
2906 if( !c
) { /* make a new context */
2907 c
= xmalloc_clear( sizeof *c
);
2909 c
->hd
= keydb_new (1);
2915 if( !sk
) { /* free the context */
2916 keydb_release (c
->hd
);
2917 release_kbnode (c
->keyblock
);
2927 /* get the next secret key from the current keyblock */
2928 for (; c
->node
; c
->node
= c
->node
->next
) {
2929 if ((c
->node
->pkt
->pkttype
== PKT_SECRET_KEY
2931 && c
->node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) )
2932 && !(c
->node
->pkt
->pkt
.secret_key
->protect
.s2k
.mode
==1001
2934 copy_secret_key (sk
, c
->node
->pkt
->pkt
.secret_key
);
2935 c
->node
= c
->node
->next
;
2936 return 0; /* found */
2939 release_kbnode (c
->keyblock
);
2940 c
->keyblock
= c
->node
= NULL
;
2942 rc
= c
->first
? keydb_search_first (c
->hd
) : keydb_search_next (c
->hd
);
2945 keydb_release (c
->hd
); c
->hd
= NULL
;
2947 return -1; /* eof */
2950 rc
= keydb_get_keyblock (c
->hd
, &c
->keyblock
);
2951 c
->node
= c
->keyblock
;
2954 return rc
; /* error */
2959 /*********************************************
2960 *********** user ID printing helpers *******
2961 *********************************************/
2964 * Return a string with a printable representation of the user_id.
2965 * this string must be freed by xfree.
2968 get_user_id_string( u32
*keyid
)
2973 /* try it two times; second pass reads from key resources */
2976 for(r
=user_id_db
; r
; r
= r
->next
)
2979 for (a
=r
->keyids
; a
; a
= a
->next
)
2981 if( a
->keyid
[0] == keyid
[0] && a
->keyid
[1] == keyid
[1] )
2983 p
= xmalloc( keystrlen() + 1 + r
->len
+ 1 );
2984 sprintf(p
, "%s %.*s", keystr(keyid
), r
->len
, r
->name
);
2989 } while( ++pass
< 2 && !get_pubkey( NULL
, keyid
) );
2990 p
= xmalloc( keystrlen() + 5 );
2991 sprintf(p
, "%s [?]", keystr(keyid
));
2997 get_user_id_string_native ( u32
*keyid
)
2999 char *p
= get_user_id_string( keyid
);
3000 char *p2
= utf8_to_native( p
, strlen(p
), 0 );
3007 get_long_user_id_string( u32
*keyid
)
3012 /* try it two times; second pass reads from key resources */
3014 for(r
=user_id_db
; r
; r
= r
->next
) {
3016 for (a
=r
->keyids
; a
; a
= a
->next
) {
3017 if( a
->keyid
[0] == keyid
[0] && a
->keyid
[1] == keyid
[1] ) {
3018 p
= xmalloc( r
->len
+ 20 );
3019 sprintf(p
, "%08lX%08lX %.*s",
3020 (ulong
)keyid
[0], (ulong
)keyid
[1],
3026 } while( ++pass
< 2 && !get_pubkey( NULL
, keyid
) );
3028 sprintf(p
, "%08lX%08lX [?]", (ulong
)keyid
[0], (ulong
)keyid
[1] );
3033 get_user_id( u32
*keyid
, size_t *rn
)
3039 /* try it two times; second pass reads from key resources */
3041 for(r
=user_id_db
; r
; r
= r
->next
) {
3043 for (a
=r
->keyids
; a
; a
= a
->next
) {
3044 if( a
->keyid
[0] == keyid
[0] && a
->keyid
[1] == keyid
[1] ) {
3045 p
= xmalloc( r
->len
);
3046 memcpy(p
, r
->name
, r
->len
);
3052 } while( ++pass
< 2 && !get_pubkey( NULL
, keyid
) );
3053 p
= xstrdup( user_id_not_found_utf8 () );
3059 get_user_id_native( u32
*keyid
)
3062 char *p
= get_user_id( keyid
, &rn
);
3063 char *p2
= utf8_to_native( p
, rn
, 0 );
3069 get_ctx_handle(GETKEY_CTX ctx
)
3071 return ctx
->kr_handle
;
3075 free_akl(struct akl
*akl
)
3078 free_keyserver_spec(akl
->spec
);
3086 while(opt
.auto_key_locate
)
3088 struct akl
*akl2
=opt
.auto_key_locate
;
3089 opt
.auto_key_locate
=opt
.auto_key_locate
->next
;
3094 /* Returns false on error. */
3096 parse_auto_key_locate(char *options
)
3100 while((tok
=optsep(&options
)))
3102 struct akl
*akl
,*check
,*last
=NULL
;
3108 akl
=xmalloc_clear(sizeof(*akl
));
3110 if(ascii_strcasecmp(tok
,"nodefault")==0)
3111 akl
->type
=AKL_NODEFAULT
;
3112 else if(ascii_strcasecmp(tok
,"local")==0)
3113 akl
->type
=AKL_LOCAL
;
3114 else if(ascii_strcasecmp(tok
,"ldap")==0)
3116 else if(ascii_strcasecmp(tok
,"keyserver")==0)
3117 akl
->type
=AKL_KEYSERVER
;
3119 else if(ascii_strcasecmp(tok
,"cert")==0)
3123 else if(ascii_strcasecmp(tok
,"pka")==0)
3126 else if((akl
->spec
=parse_keyserver_uri(tok
,1,NULL
,0)))
3134 /* We must maintain the order the user gave us */
3135 for(check
=opt
.auto_key_locate
;check
;last
=check
,check
=check
->next
)
3137 /* Check for duplicates */
3138 if(check
->type
==akl
->type
3139 && (akl
->type
!=AKL_SPEC
3140 || (akl
->type
==AKL_SPEC
3141 && strcmp(check
->spec
->uri
,akl
->spec
->uri
)==0)))
3154 opt
.auto_key_locate
=akl
;