1 /* getkey.c - Get a key from the database
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 * 2006 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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
39 #include "keyserver-internal.h"
41 #define MAX_PK_CACHE_ENTRIES PK_UID_CACHE_SIZE
42 #define MAX_UID_CACHE_ENTRIES PK_UID_CACHE_SIZE
44 #if MAX_PK_CACHE_ENTRIES < 2
45 #error We need the cache for key creation
52 KBNODE found_key
; /* pointer into some keyblock */
56 KEYDB_HANDLE kr_handle
;
59 KEYDB_SEARCH_DESC items
[1];
71 typedef struct keyid_list
{
72 struct keyid_list
*next
;
77 #if MAX_PK_CACHE_ENTRIES
78 typedef struct pk_cache_entry
{
79 struct pk_cache_entry
*next
;
83 static pk_cache_entry_t pk_cache
;
84 static int pk_cache_entries
; /* number of entries in pk cache */
85 static int pk_cache_disabled
;
88 #if MAX_UID_CACHE_ENTRIES < 5
89 #error we really need the userid cache
91 typedef struct user_id_db
{
92 struct user_id_db
*next
;
97 static user_id_db_t user_id_db
;
98 static int uid_cache_entries
; /* number of entries in uid cache */
100 static void merge_selfsigs( KBNODE keyblock
);
101 static int lookup( GETKEY_CTX ctx
, KBNODE
*ret_keyblock
, int secmode
);
108 for(i
=0; i
< DIM(lkup_stats
); i
++ ) {
109 if( lkup_stats
[i
].any
)
111 "lookup stats: mode=%-2d ok=%-6d nokey=%-6d err=%-6d\n",
113 lkup_stats
[i
].okay_count
,
114 lkup_stats
[i
].nokey_count
,
115 lkup_stats
[i
].error_count
);
122 cache_public_key( PKT_public_key
*pk
)
124 #if MAX_PK_CACHE_ENTRIES
128 if( pk_cache_disabled
)
134 if( is_ELGAMAL(pk
->pubkey_algo
)
135 || pk
->pubkey_algo
== PUBKEY_ALGO_DSA
136 || is_RSA(pk
->pubkey_algo
) ) {
137 keyid_from_pk( pk
, keyid
);
140 return; /* don't know how to get the keyid */
142 for( ce
= pk_cache
; ce
; ce
= ce
->next
)
143 if( ce
->keyid
[0] == keyid
[0] && ce
->keyid
[1] == keyid
[1] ) {
145 log_debug("cache_public_key: already in cache\n");
149 if( pk_cache_entries
>= MAX_PK_CACHE_ENTRIES
) {
150 /* fixme: use another algorithm to free some cache slots */
152 if( opt
.verbose
> 1 )
153 log_info(_("too many entries in pk cache - disabled\n"));
157 ce
= xmalloc( sizeof *ce
);
160 ce
->pk
= copy_public_key( NULL
, pk
);
161 ce
->keyid
[0] = keyid
[0];
162 ce
->keyid
[1] = keyid
[1];
167 /* Return a const utf-8 string with the text "[User ID not found]".
168 This fucntion is required so that we don't need to switch gettext's
169 encoding temporary. */
171 user_id_not_found_utf8 (void)
176 text
= native_to_utf8 (_("[User ID not found]"));
183 * Return the user ID from the given keyblock.
184 * We use the primary uid flag which has been set by the merge_selfsigs
185 * function. The returned value is only valid as long as then given
186 * keyblock is not changed
189 get_primary_uid ( KBNODE keyblock
, size_t *uidlen
)
194 for (k
=keyblock
; k
; k
=k
->next
) {
195 if ( k
->pkt
->pkttype
== PKT_USER_ID
196 && !k
->pkt
->pkt
.user_id
->attrib_data
197 && k
->pkt
->pkt
.user_id
->is_primary
) {
198 *uidlen
= k
->pkt
->pkt
.user_id
->len
;
199 return k
->pkt
->pkt
.user_id
->name
;
202 s
= user_id_not_found_utf8 ();
203 *uidlen
= strlen (s
);
209 release_keyid_list ( keyid_list_t k
)
212 keyid_list_t k2
= k
->next
;
219 * Store the association of keyid and userid
220 * Feed only public keys to this function.
223 cache_user_id( KBNODE keyblock
)
228 keyid_list_t keyids
= NULL
;
231 for (k
=keyblock
; k
; k
= k
->next
) {
232 if ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
233 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
234 keyid_list_t a
= xmalloc_clear ( sizeof *a
);
235 /* Hmmm: For a long list of keyids it might be an advantage
236 * to append the keys */
237 keyid_from_pk( k
->pkt
->pkt
.public_key
, a
->keyid
);
238 /* first check for duplicates */
239 for(r
=user_id_db
; r
; r
= r
->next
) {
240 keyid_list_t b
= r
->keyids
;
241 for ( b
= r
->keyids
; b
; b
= b
->next
) {
242 if( b
->keyid
[0] == a
->keyid
[0]
243 && b
->keyid
[1] == a
->keyid
[1] ) {
245 log_debug("cache_user_id: already in cache\n");
246 release_keyid_list ( keyids
);
252 /* now put it into the cache */
258 BUG (); /* No key no fun */
261 uid
= get_primary_uid ( keyblock
, &uidlen
);
263 if( uid_cache_entries
>= MAX_UID_CACHE_ENTRIES
) {
264 /* fixme: use another algorithm to free some cache slots */
266 user_id_db
= r
->next
;
267 release_keyid_list ( r
->keyids
);
271 r
= xmalloc( sizeof *r
+ uidlen
-1 );
274 memcpy(r
->name
, uid
, r
->len
);
275 r
->next
= user_id_db
;
282 getkey_disable_caches()
284 #if MAX_PK_CACHE_ENTRIES
286 pk_cache_entry_t ce
, ce2
;
288 for( ce
= pk_cache
; ce
; ce
= ce2
) {
290 free_public_key( ce
->pk
);
294 pk_cache_entries
= 0;
298 /* fixme: disable user id cache ? */
303 pk_from_block ( GETKEY_CTX ctx
, PKT_public_key
*pk
, KBNODE keyblock
)
305 KBNODE a
= ctx
->found_key
? ctx
->found_key
: keyblock
;
307 assert ( a
->pkt
->pkttype
== PKT_PUBLIC_KEY
308 || a
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
);
310 copy_public_key ( pk
, a
->pkt
->pkt
.public_key
);
314 sk_from_block ( GETKEY_CTX ctx
,
315 PKT_secret_key
*sk
, KBNODE keyblock
)
317 KBNODE a
= ctx
->found_key
? ctx
->found_key
: keyblock
;
319 assert ( a
->pkt
->pkttype
== PKT_SECRET_KEY
320 || a
->pkt
->pkttype
== PKT_SECRET_SUBKEY
);
322 copy_secret_key( sk
, a
->pkt
->pkt
.secret_key
);
327 * Get a public key and store it into the allocated pk
328 * can be called with PK set to NULL to just read it into some
329 * internal structures.
332 get_pubkey( PKT_public_key
*pk
, u32
*keyid
)
337 #if MAX_PK_CACHE_ENTRIES
340 /* Try to get it from the cache. We don't do this when pk is
341 NULL as it does not guarantee that the user IDs are
344 for( ce
= pk_cache
; ce
; ce
= ce
->next
)
346 if( ce
->keyid
[0] == keyid
[0] && ce
->keyid
[1] == keyid
[1] )
348 copy_public_key( pk
, ce
->pk
);
354 /* more init stuff */
356 pk
= xmalloc_clear( sizeof *pk
);
362 { struct getkey_ctx_s ctx
;
364 memset( &ctx
, 0, sizeof ctx
);
365 ctx
.exact
= 1; /* use the key ID exactly as given */
366 ctx
.not_allocated
= 1;
367 ctx
.kr_handle
= keydb_new (0);
369 ctx
.items
[0].mode
= KEYDB_SEARCH_MODE_LONG_KID
;
370 ctx
.items
[0].u
.kid
[0] = keyid
[0];
371 ctx
.items
[0].u
.kid
[1] = keyid
[1];
372 ctx
.req_algo
= pk
->req_algo
;
373 ctx
.req_usage
= pk
->req_usage
;
374 rc
= lookup( &ctx
, &kb
, 0 );
376 pk_from_block ( &ctx
, pk
, kb
);
378 get_pubkey_end( &ctx
);
379 release_kbnode ( kb
);
384 rc
= G10ERR_NO_PUBKEY
;
388 cache_public_key( pk
);
395 /* Get a public key and store it into the allocated pk. This function
396 differs from get_pubkey() in that it does not do a check of the key
397 to avoid recursion. It should be used only in very certain cases.
398 It will only retrieve primary keys. */
400 get_pubkey_fast (PKT_public_key
*pk
, u32
*keyid
)
408 #if MAX_PK_CACHE_ENTRIES
409 { /* Try to get it from the cache */
412 for (ce
= pk_cache
; ce
; ce
= ce
->next
)
414 if (ce
->keyid
[0] == keyid
[0] && ce
->keyid
[1] == keyid
[1])
417 copy_public_key (pk
, ce
->pk
);
425 rc
= keydb_search_kid (hd
, keyid
);
429 return G10ERR_NO_PUBKEY
;
431 rc
= keydb_get_keyblock (hd
, &keyblock
);
435 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc
));
436 return G10ERR_NO_PUBKEY
;
439 assert ( keyblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
440 || keyblock
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
);
442 keyid_from_pk(keyblock
->pkt
->pkt
.public_key
,pkid
);
443 if(keyid
[0]==pkid
[0] && keyid
[1]==pkid
[1])
444 copy_public_key (pk
, keyblock
->pkt
->pkt
.public_key
);
448 release_kbnode (keyblock
);
450 /* Not caching key here since it won't have all of the fields
458 get_pubkeyblock( u32
*keyid
)
460 struct getkey_ctx_s ctx
;
462 KBNODE keyblock
= NULL
;
464 memset( &ctx
, 0, sizeof ctx
);
465 /* no need to set exact here because we want the entire block */
466 ctx
.not_allocated
= 1;
467 ctx
.kr_handle
= keydb_new (0);
469 ctx
.items
[0].mode
= KEYDB_SEARCH_MODE_LONG_KID
;
470 ctx
.items
[0].u
.kid
[0] = keyid
[0];
471 ctx
.items
[0].u
.kid
[1] = keyid
[1];
472 rc
= lookup( &ctx
, &keyblock
, 0 );
473 get_pubkey_end( &ctx
);
475 return rc
? NULL
: keyblock
;
482 * Get a secret key and store it into sk
485 get_seckey( PKT_secret_key
*sk
, u32
*keyid
)
488 struct getkey_ctx_s ctx
;
491 memset( &ctx
, 0, sizeof ctx
);
492 ctx
.exact
= 1; /* use the key ID exactly as given */
493 ctx
.not_allocated
= 1;
494 ctx
.kr_handle
= keydb_new (1);
496 ctx
.items
[0].mode
= KEYDB_SEARCH_MODE_LONG_KID
;
497 ctx
.items
[0].u
.kid
[0] = keyid
[0];
498 ctx
.items
[0].u
.kid
[1] = keyid
[1];
499 ctx
.req_algo
= sk
->req_algo
;
500 ctx
.req_usage
= sk
->req_usage
;
501 rc
= lookup( &ctx
, &kb
, 1 );
503 sk_from_block ( &ctx
, sk
, kb
);
505 get_seckey_end( &ctx
);
506 release_kbnode ( kb
);
509 /* check the secret key (this may prompt for a passprase to
510 * unlock the secret key
512 rc
= check_secret_key( sk
, 0 );
520 * Check whether the secret key is available. This is just a fast
521 * check and does not tell us whether the secret key is valid. It
522 * merely tells other whether there is some secret key.
523 * Returns: 0 := key is available
524 * G10ERR_NO_SECKEY := not availabe
527 seckey_available( u32
*keyid
)
530 KEYDB_HANDLE hd
= keydb_new (1);
532 rc
= keydb_search_kid (hd
, keyid
);
534 rc
= G10ERR_NO_SECKEY
;
541 * Return the type of the user id:
543 * Please use the constants KEYDB_SERCH_MODE_xxx
544 * 0 = Invalid user ID
546 * 2 = match a substring
547 * 3 = match an email address
548 * 4 = match a substring of an email address
549 * 5 = match an email address, but compare from end
550 * 6 = word match mode
551 * 10 = it is a short KEYID (don't care about keyid[0])
552 * 11 = it is a long KEYID
553 * 12 = it is a trustdb index (keyid is looked up)
554 * 16 = it is a 16 byte fingerprint
555 * 20 = it is a 20 byte fingerprint
556 * 21 = Unified fingerprint :fpr:pk_algo:
557 * (We don't use pk_algo yet)
560 * - If the username starts with 8,9,16 or 17 hex-digits (the first one
561 * must be in the range 0..9), this is considered a keyid; depending
562 * on the length a short or complete one.
563 * - If the username starts with 32,33,40 or 41 hex-digits (the first one
564 * must be in the range 0..9), this is considered a fingerprint.
565 * - If the username starts with a left angle, we assume it is a complete
566 * email address and look only at this part.
567 * - If the username starts with a colon we assume it is a unified
568 * key specfification.
569 * - If the username starts with a '.', we assume it is the ending
570 * part of an email address
571 * - If the username starts with an '@', we assume it is a part of an
573 * - If the userid start with an '=' an exact compare is done.
574 * - If the userid starts with a '*' a case insensitive substring search is
575 * done (This is the default).
576 * - If the userid starts with a '+' we will compare individual words
577 * and a match requires that all the words are in the userid.
578 * Words are delimited by white space or "()<>[]{}.@-+_,;/&!"
579 * (note that you can't search for these characters). Compare
580 * is not case sensitive.
581 * - If the userid starts with a '&' a 40 hex digits keygrip is expected.
585 classify_user_id( const char *name
, KEYDB_SEARCH_DESC
*desc
)
591 KEYDB_SEARCH_DESC dummy_desc
;
596 /* clear the structure so that the mode field is set to zero unless
597 * we set it to the correct value right at the end of this function */
598 memset (desc
, 0, sizeof *desc
);
600 /* skip leading spaces. Fixme: what is with trailing spaces? */
601 for(s
= name
; *s
&& spacep (s
); s
++ )
605 case 0: /* empty string is an error */
609 case '.': /* an email address, compare from end */
610 mode
= KEYDB_SEARCH_MODE_MAILEND
;
616 case '<': /* an email address */
617 mode
= KEYDB_SEARCH_MODE_MAIL
;
621 case '@': /* part of an email address */
622 mode
= KEYDB_SEARCH_MODE_MAILSUB
;
627 case '=': /* exact compare */
628 mode
= KEYDB_SEARCH_MODE_EXACT
;
633 case '*': /* case insensitive substring search */
634 mode
= KEYDB_SEARCH_MODE_SUBSTR
;
640 case '+': /* compare individual words */
641 mode
= KEYDB_SEARCH_MODE_WORDS
;
647 case '#': /* local user id */
648 return 0; /* This is now obsolete and can't not be used anymore*/
650 case ':': /*Unified fingerprint */
655 se
= strchr( ++s
,':');
658 for (i
=0,si
=s
; si
< se
; si
++, i
++ ) {
659 if ( !strchr("01234567890abcdefABCDEF", *si
) )
660 return 0; /* invalid digit */
662 if (i
!= 32 && i
!= 40)
663 return 0; /* invalid length of fpr*/
664 for (i
=0,si
=s
; si
< se
; i
++, si
+=2)
665 desc
->u
.fpr
[i
] = hextobyte(si
);
669 mode
= KEYDB_SEARCH_MODE_FPR
;
673 case '&': /* keygrip */
674 return 0; /* Not yet implememted. */
677 if (s
[0] == '0' && s
[1] == 'x') {
682 hexlength
= strspn(s
, "0123456789abcdefABCDEF");
683 if (hexlength
>= 8 && s
[hexlength
] =='!') {
685 hexlength
++; /* just for the following check */
688 /* check if a hexadecimal number is terminated by EOS or blank */
689 if (hexlength
&& s
[hexlength
] && !spacep(s
+hexlength
)) {
690 if (hexprefix
) /* a "0x" prefix without correct */
691 return 0; /* termination is an error */
692 else /* The first chars looked like */
693 hexlength
= 0; /* a hex number, but really were not. */
700 || (!hexprefix
&& hexlength
== 9 && *s
== '0')){
705 desc
->u
.kid
[1] = strtoul( s
, NULL
, 16 );
706 mode
= KEYDB_SEARCH_MODE_SHORT_KID
;
708 else if (hexlength
== 16
709 || (!hexprefix
&& hexlength
== 17 && *s
== '0')) {
715 desc
->u
.kid
[0] = strtoul( buf
, NULL
, 16 );
716 desc
->u
.kid
[1] = strtoul( s
+8, NULL
, 16 );
717 mode
= KEYDB_SEARCH_MODE_LONG_KID
;
719 else if (hexlength
== 32 || (!hexprefix
&& hexlength
== 33
721 /* md5 fingerprint */
725 memset(desc
->u
.fpr
+16, 0, 4);
726 for (i
=0; i
< 16; i
++, s
+=2) {
727 int c
= hextobyte(s
);
732 mode
= KEYDB_SEARCH_MODE_FPR16
;
734 else if (hexlength
== 40 || (!hexprefix
&& hexlength
== 41
736 /* sha1/rmd160 fingerprint */
740 for (i
=0; i
< 20; i
++, s
+=2) {
741 int c
= hextobyte(s
);
746 mode
= KEYDB_SEARCH_MODE_FPR20
;
749 if (hexprefix
) /* This was a hex number with a prefix */
750 return 0; /* and a wrong length */
754 mode
= KEYDB_SEARCH_MODE_SUBSTR
; /* default mode */
764 skip_unusable(void *dummy
,u32
*keyid
,PKT_user_id
*uid
)
769 keyblock
=get_pubkeyblock(keyid
);
772 log_error("error checking usability status of %s\n",keystr(keyid
));
776 /* Is the user ID in question revoked/expired? */
781 for(node
=keyblock
;node
;node
=node
->next
)
783 if(node
->pkt
->pkttype
==PKT_USER_ID
)
785 if(cmp_user_ids(uid
,node
->pkt
->pkt
.user_id
)==0
786 && (node
->pkt
->pkt
.user_id
->is_revoked
787 || node
->pkt
->pkt
.user_id
->is_expired
))
797 unusable
=pk_is_disabled(keyblock
->pkt
->pkt
.public_key
);
800 release_kbnode(keyblock
);
805 * Try to get the pubkey by the userid. This function looks for the
806 * first pubkey certificate which has the given name in a user_id. if
807 * pk/sk has the pubkey algo set, the function will only return a
808 * pubkey with that algo. If namelist is NULL, the first key is
809 * returned. The caller should provide storage for either the pk or
810 * the sk. If ret_kb is not NULL the function will return the
815 key_byname( GETKEY_CTX
*retctx
, strlist_t namelist
,
816 PKT_public_key
*pk
, PKT_secret_key
*sk
,
817 int secmode
, int include_unusable
,
818 KBNODE
*ret_kb
, KEYDB_HANDLE
*ret_kdbhd
)
824 KBNODE help_kb
= NULL
;
826 if( retctx
) {/* reset the returned context in case of error */
827 assert (!ret_kdbhd
); /* not allowed because the handle is
828 stored in the context */
836 ctx
= xmalloc_clear (sizeof *ctx
);
838 ctx
->items
[0].mode
=KEYDB_SEARCH_MODE_FIRST
;
839 if(!include_unusable
)
840 ctx
->items
[0].skipfnc
=skip_unusable
;
844 /* build the search context */
845 for(n
=0, r
=namelist
; r
; r
= r
->next
)
848 ctx
= xmalloc_clear (sizeof *ctx
+ (n
-1)*sizeof ctx
->items
);
851 for(n
=0, r
=namelist
; r
; r
= r
->next
, n
++ )
853 classify_user_id (r
->d
, &ctx
->items
[n
]);
855 if (ctx
->items
[n
].exact
)
857 if (!ctx
->items
[n
].mode
)
860 return G10ERR_INV_USER_ID
;
863 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_SHORT_KID
864 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_LONG_KID
865 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_FPR16
866 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_FPR20
867 && ctx
->items
[n
].mode
!=KEYDB_SEARCH_MODE_FPR
)
868 ctx
->items
[n
].skipfnc
=skip_unusable
;
872 ctx
->kr_handle
= keydb_new (secmode
);
878 ctx
->req_algo
= sk
->req_algo
;
879 ctx
->req_usage
= sk
->req_usage
;
881 rc
= lookup( ctx
, ret_kb
, 1 );
883 sk_from_block ( ctx
, sk
, *ret_kb
);
888 ctx
->req_algo
= pk
->req_algo
;
889 ctx
->req_usage
= pk
->req_usage
;
891 rc
= lookup( ctx
, ret_kb
, 0 );
893 pk_from_block ( ctx
, pk
, *ret_kb
);
897 release_kbnode ( help_kb
);
899 if (retctx
) /* caller wants the context */
903 *ret_kdbhd
= ctx
->kr_handle
;
904 ctx
->kr_handle
= NULL
;
906 get_pubkey_end (ctx
);
914 /* Find a public key from NAME and return the keyblock or the key. If
915 ret_kdb is not NULL, the KEYDB handle used to locate this keyblock
916 is returned and the caller is responsible for closing it. If a key
917 was not found and NAME is a valid RFC822 mailbox and PKA retrieval
918 has been enabled, we try to import the pkea via the PKA
921 get_pubkey_byname (PKT_public_key
*pk
,
922 const char *name
, KBNODE
*ret_keyblock
,
923 KEYDB_HANDLE
*ret_kdbhd
, int include_unusable
)
926 strlist_t namelist
= NULL
;
928 add_to_strlist( &namelist
, name
);
930 rc
= key_byname( NULL
, namelist
, pk
, NULL
, 0,
931 include_unusable
, ret_keyblock
, ret_kdbhd
);
933 /* If the requested name resembles a valid mailbox and automatic
934 retrieval has been enabled, we try to import the key. */
936 if (rc
== G10ERR_NO_PUBKEY
&& is_valid_mailbox(name
))
940 for(akl
=opt
.auto_key_locate
;akl
;akl
=akl
->next
)
942 unsigned char *fpr
=NULL
;
948 glo_ctrl
.in_auto_key_retrieve
++;
949 rc
=keyserver_import_cert(name
,&fpr
,&fpr_len
);
950 glo_ctrl
.in_auto_key_retrieve
--;
953 log_info(_("automatically retrieved `%s' via %s\n"),
958 glo_ctrl
.in_auto_key_retrieve
++;
959 rc
=keyserver_import_pka(name
,&fpr
,&fpr_len
);
960 glo_ctrl
.in_auto_key_retrieve
--;
963 log_info(_("automatically retrieved `%s' via %s\n"),
968 glo_ctrl
.in_auto_key_retrieve
++;
969 rc
=keyserver_import_ldap(name
,&fpr
,&fpr_len
);
970 glo_ctrl
.in_auto_key_retrieve
--;
973 log_info(_("automatically retrieved `%s' via %s\n"),
978 /* Strictly speaking, we don't need to only use a valid
979 mailbox for the getname search, but it helps cut down
980 on the problem of searching for something like "john"
981 and getting a whole lot of keys back. */
984 glo_ctrl
.in_auto_key_retrieve
++;
985 rc
=keyserver_import_name(name
,&fpr
,&fpr_len
,opt
.keyserver
);
986 glo_ctrl
.in_auto_key_retrieve
--;
989 log_info(_("automatically retrieved `%s' via %s\n"),
990 name
,opt
.keyserver
->uri
);
996 struct keyserver_spec
*keyserver
;
998 keyserver
=keyserver_match(akl
->spec
);
999 glo_ctrl
.in_auto_key_retrieve
++;
1000 rc
=keyserver_import_name(name
,&fpr
,&fpr_len
,keyserver
);
1001 glo_ctrl
.in_auto_key_retrieve
--;
1004 log_info(_("automatically retrieved `%s' via %s\n"),
1005 name
,akl
->spec
->uri
);
1010 /* Use the fingerprint of the key that we actually fetched.
1011 This helps prevent problems where the key that we fetched
1012 doesn't have the same name that we used to fetch it. In
1013 the case of CERT and PKA, this is an actual security
1014 requirement as the URL might point to a key put in by an
1015 attacker. By forcing the use of the fingerprint, we
1016 won't use the attacker's key here. */
1020 char fpr_string
[MAX_FINGERPRINT_LEN
*2+1];
1022 assert(fpr_len
<=MAX_FINGERPRINT_LEN
);
1024 free_strlist(namelist
);
1027 for(i
=0;i
<fpr_len
;i
++)
1028 sprintf(fpr_string
+2*i
,"%02X",fpr
[i
]);
1031 log_info("auto-key-locate found fingerprint %s\n",fpr_string
);
1033 add_to_strlist( &namelist
, fpr_string
);
1038 rc
= key_byname( NULL
, namelist
, pk
, NULL
, 0,
1039 include_unusable
, ret_keyblock
, ret_kdbhd
);
1040 if(rc
!=G10ERR_NO_PUBKEY
)
1045 free_strlist( namelist
);
1050 get_pubkey_bynames( GETKEY_CTX
*retctx
, PKT_public_key
*pk
,
1051 strlist_t names
, KBNODE
*ret_keyblock
)
1053 return key_byname( retctx
, names
, pk
, NULL
, 0, 1, ret_keyblock
, NULL
);
1057 get_pubkey_next( GETKEY_CTX ctx
, PKT_public_key
*pk
, KBNODE
*ret_keyblock
)
1061 rc
= lookup( ctx
, ret_keyblock
, 0 );
1062 if ( !rc
&& pk
&& ret_keyblock
)
1063 pk_from_block ( ctx
, pk
, *ret_keyblock
);
1069 get_pubkey_end( GETKEY_CTX ctx
)
1072 memset (&ctx
->kbpos
, 0, sizeof ctx
->kbpos
);
1073 keydb_release (ctx
->kr_handle
);
1074 if( !ctx
->not_allocated
)
1081 * Search for a key with the given fingerprint.
1083 * We should replace this with the _byname function. Thiscsan be done
1084 * by creating a userID conforming to the unified fingerprint style.
1087 get_pubkey_byfprint( PKT_public_key
*pk
,
1088 const byte
*fprint
, size_t fprint_len
)
1092 if( fprint_len
== 20 || fprint_len
== 16 ) {
1093 struct getkey_ctx_s ctx
;
1096 memset( &ctx
, 0, sizeof ctx
);
1098 ctx
.not_allocated
= 1;
1099 ctx
.kr_handle
= keydb_new (0);
1101 ctx
.items
[0].mode
= fprint_len
==16? KEYDB_SEARCH_MODE_FPR16
1102 : KEYDB_SEARCH_MODE_FPR20
;
1103 memcpy( ctx
.items
[0].u
.fpr
, fprint
, fprint_len
);
1104 rc
= lookup( &ctx
, &kb
, 0 );
1106 pk_from_block ( &ctx
, pk
, kb
);
1107 release_kbnode ( kb
);
1108 get_pubkey_end( &ctx
);
1111 rc
= G10ERR_GENERAL
; /* Oops */
1116 /* Get a public key and store it into the allocated pk. This function
1117 differs from get_pubkey_byfprint() in that it does not do a check
1118 of the key to avoid recursion. It should be used only in very
1119 certain cases. PK may be NULL to check just for the existance of
1122 get_pubkey_byfprint_fast (PKT_public_key
*pk
,
1123 const byte
*fprint
, size_t fprint_len
)
1128 byte fprbuf
[MAX_FINGERPRINT_LEN
];
1131 for (i
=0; i
< MAX_FINGERPRINT_LEN
&& i
< fprint_len
; i
++)
1132 fprbuf
[i
] = fprint
[i
];
1133 while (i
< MAX_FINGERPRINT_LEN
)
1137 rc
= keydb_search_fpr (hd
, fprbuf
);
1141 return G10ERR_NO_PUBKEY
;
1143 rc
= keydb_get_keyblock (hd
, &keyblock
);
1147 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc
));
1148 return G10ERR_NO_PUBKEY
;
1151 assert ( keyblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
1152 || keyblock
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
);
1154 copy_public_key (pk
, keyblock
->pkt
->pkt
.public_key
);
1155 release_kbnode (keyblock
);
1157 /* Not caching key here since it won't have all of the fields
1164 * Search for a key with the given fingerprint and return the
1165 * complete keyblock which may have more than only this key.
1168 get_keyblock_byfprint( KBNODE
*ret_keyblock
, const byte
*fprint
,
1173 if( fprint_len
== 20 || fprint_len
== 16 ) {
1174 struct getkey_ctx_s ctx
;
1176 memset( &ctx
, 0, sizeof ctx
);
1177 ctx
.not_allocated
= 1;
1178 ctx
.kr_handle
= keydb_new (0);
1180 ctx
.items
[0].mode
= fprint_len
==16? KEYDB_SEARCH_MODE_FPR16
1181 : KEYDB_SEARCH_MODE_FPR20
;
1182 memcpy( ctx
.items
[0].u
.fpr
, fprint
, fprint_len
);
1183 rc
= lookup( &ctx
, ret_keyblock
, 0 );
1184 get_pubkey_end( &ctx
);
1187 rc
= G10ERR_GENERAL
; /* Oops */
1194 * Get a secret key by name and store it into sk
1195 * If NAME is NULL use the default key
1198 get_seckey_byname2( GETKEY_CTX
*retctx
,
1199 PKT_secret_key
*sk
, const char *name
, int unprotect
,
1202 strlist_t namelist
= NULL
;
1203 int rc
,include_unusable
=1;
1205 /* If we have no name, try to use the default secret key. If we
1206 have no default, we'll use the first usable one. */
1208 if( !name
&& opt
.def_secret_key
&& *opt
.def_secret_key
)
1209 add_to_strlist( &namelist
, opt
.def_secret_key
);
1211 add_to_strlist( &namelist
, name
);
1215 rc
= key_byname( retctx
, namelist
, NULL
, sk
, 1, include_unusable
,
1218 free_strlist( namelist
);
1220 if( !rc
&& unprotect
)
1221 rc
= check_secret_key( sk
, 0 );
1227 get_seckey_byname( PKT_secret_key
*sk
, const char *name
, int unlock
)
1229 return get_seckey_byname2 ( NULL
, sk
, name
, unlock
, NULL
);
1234 get_seckey_bynames( GETKEY_CTX
*retctx
, PKT_secret_key
*sk
,
1235 strlist_t names
, KBNODE
*ret_keyblock
)
1237 return key_byname( retctx
, names
, NULL
, sk
, 1, 1, ret_keyblock
, NULL
);
1242 get_seckey_next( GETKEY_CTX ctx
, PKT_secret_key
*sk
, KBNODE
*ret_keyblock
)
1246 rc
= lookup( ctx
, ret_keyblock
, 1 );
1247 if ( !rc
&& sk
&& ret_keyblock
)
1248 sk_from_block ( ctx
, sk
, *ret_keyblock
);
1255 get_seckey_end( GETKEY_CTX ctx
)
1257 get_pubkey_end( ctx
);
1262 * Search for a key with the given fingerprint.
1264 * We should replace this with the _byname function. Thiscsan be done
1265 * by creating a userID conforming to the unified fingerprint style.
1268 get_seckey_byfprint( PKT_secret_key
*sk
,
1269 const byte
*fprint
, size_t fprint_len
)
1273 if( fprint_len
== 20 || fprint_len
== 16 ) {
1274 struct getkey_ctx_s ctx
;
1277 memset( &ctx
, 0, sizeof ctx
);
1279 ctx
.not_allocated
= 1;
1280 ctx
.kr_handle
= keydb_new (1);
1282 ctx
.items
[0].mode
= fprint_len
==16? KEYDB_SEARCH_MODE_FPR16
1283 : KEYDB_SEARCH_MODE_FPR20
;
1284 memcpy( ctx
.items
[0].u
.fpr
, fprint
, fprint_len
);
1285 rc
= lookup( &ctx
, &kb
, 1 );
1287 sk_from_block ( &ctx
, sk
, kb
);
1288 release_kbnode ( kb
);
1289 get_seckey_end( &ctx
);
1292 rc
= G10ERR_GENERAL
; /* Oops */
1297 /* Search for a secret key with the given fingerprint and return the
1298 complete keyblock which may have more than only this key. */
1300 get_seckeyblock_byfprint (KBNODE
*ret_keyblock
, const byte
*fprint
,
1304 struct getkey_ctx_s ctx
;
1306 if (fprint_len
!= 20 && fprint_len
== 16)
1307 return G10ERR_GENERAL
; /* Oops */
1309 memset (&ctx
, 0, sizeof ctx
);
1310 ctx
.not_allocated
= 1;
1311 ctx
.kr_handle
= keydb_new (1);
1313 ctx
.items
[0].mode
= (fprint_len
==16
1314 ? KEYDB_SEARCH_MODE_FPR16
1315 : KEYDB_SEARCH_MODE_FPR20
);
1316 memcpy (ctx
.items
[0].u
.fpr
, fprint
, fprint_len
);
1317 rc
= lookup (&ctx
, ret_keyblock
, 1);
1318 get_seckey_end (&ctx
);
1325 /************************************************
1326 ************* Merging stuff ********************
1327 ************************************************/
1330 * merge all selfsignatures with the keys.
1331 * FIXME: replace this at least for the public key parts
1332 * by merge_selfsigs.
1333 * It is still used in keyedit.c and
1334 * at 2 or 3 other places - check whether it is really needed.
1335 * It might be needed by the key edit and import stuff because
1336 * the keylock is changed.
1339 merge_keys_and_selfsig( KBNODE keyblock
)
1341 PKT_public_key
*pk
= NULL
;
1342 PKT_secret_key
*sk
= NULL
;
1345 u32 kid
[2] = { 0, 0 };
1348 if (keyblock
&& keyblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
) {
1349 /* divert to our new function */
1350 merge_selfsigs (keyblock
);
1353 /* still need the old one because the new one can't handle secret keys */
1355 for(k
=keyblock
; k
; k
= k
->next
) {
1356 if( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
1357 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
1358 pk
= k
->pkt
->pkt
.public_key
; sk
= NULL
;
1359 if( pk
->version
< 4 )
1360 pk
= NULL
; /* not needed for old keys */
1361 else if( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
)
1362 keyid_from_pk( pk
, kid
);
1363 else if( !pk
->expiredate
) { /* and subkey */
1364 /* insert the expiration date here */
1365 /*FIXME!!! pk->expiredate = subkeys_expiretime( k, kid );*/
1369 else if( k
->pkt
->pkttype
== PKT_SECRET_KEY
1370 || k
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
1371 pk
= NULL
; sk
= k
->pkt
->pkt
.secret_key
;
1372 if( sk
->version
< 4 )
1374 else if( k
->pkt
->pkttype
== PKT_SECRET_KEY
)
1375 keyid_from_sk( sk
, kid
);
1378 else if( (pk
|| sk
) && k
->pkt
->pkttype
== PKT_SIGNATURE
1379 && (sig
=k
->pkt
->pkt
.signature
)->sig_class
>= 0x10
1380 && sig
->sig_class
<= 0x30 && sig
->version
> 3
1381 && !(sig
->sig_class
== 0x18 || sig
->sig_class
== 0x28)
1382 && sig
->keyid
[0] == kid
[0] && sig
->keyid
[1] == kid
[1] ) {
1383 /* okay this is a self-signature which can be used.
1384 * This is not used for subkey binding signature, becuase this
1386 * FIXME: We should only use this if the signature is valid
1387 * but this is time consuming - we must provide another
1388 * way to handle this
1393 p
= parse_sig_subpkt( sig
->hashed
, SIGSUBPKT_KEY_EXPIRE
, NULL
);
1395 ed
= p
? pk
->timestamp
+ buffer_to_u32(p
):0;
1396 if( sig
->timestamp
> sigdate
) {
1397 pk
->expiredate
= ed
;
1398 sigdate
= sig
->timestamp
;
1402 ed
= p
? sk
->timestamp
+ buffer_to_u32(p
):0;
1403 if( sig
->timestamp
> sigdate
) {
1404 sk
->expiredate
= ed
;
1405 sigdate
= sig
->timestamp
;
1410 if(pk
&& (pk
->expiredate
==0 ||
1411 (pk
->max_expiredate
&& pk
->expiredate
>pk
->max_expiredate
)))
1412 pk
->expiredate
=pk
->max_expiredate
;
1414 if(sk
&& (sk
->expiredate
==0 ||
1415 (sk
->max_expiredate
&& sk
->expiredate
>sk
->max_expiredate
)))
1416 sk
->expiredate
=sk
->max_expiredate
;
1421 parse_key_usage(PKT_signature
*sig
)
1428 p
=parse_sig_subpkt(sig
->hashed
,SIGSUBPKT_KEY_FLAGS
,&n
);
1431 /* first octet of the keyflags */
1436 key_usage
|= PUBKEY_USAGE_CERT
;
1442 key_usage
|= PUBKEY_USAGE_SIG
;
1446 /* We do not distinguish between encrypting communications and
1447 encrypting storage. */
1448 if(flags
& (0x04|0x08))
1450 key_usage
|= PUBKEY_USAGE_ENC
;
1451 flags
&=~(0x04|0x08);
1456 key_usage
|= PUBKEY_USAGE_AUTH
;
1461 key_usage
|= PUBKEY_USAGE_UNKNOWN
;
1464 /* We set PUBKEY_USAGE_UNKNOWN to indicate that this key has a
1465 capability that we do not handle. This serves to distinguish
1466 between a zero key usage which we handle as the default
1467 capabilities for that algorithm, and a usage that we do not
1474 * Apply information from SIGNODE (which is the valid self-signature
1475 * associated with that UID) to the UIDNODE:
1476 * - wether the UID has been revoked
1477 * - assumed creation date of the UID
1478 * - temporary store the keyflags here
1479 * - temporary store the key expiration time here
1480 * - mark whether the primary user ID flag hat been set.
1481 * - store the preferences
1484 fixup_uidnode ( KBNODE uidnode
, KBNODE signode
, u32 keycreated
)
1486 PKT_user_id
*uid
= uidnode
->pkt
->pkt
.user_id
;
1487 PKT_signature
*sig
= signode
->pkt
->pkt
.signature
;
1488 const byte
*p
, *sym
, *hash
, *zip
;
1489 size_t n
, nsym
, nhash
, nzip
;
1491 sig
->flags
.chosen_selfsig
= 1; /* we chose this one */
1492 uid
->created
= 0; /* not created == invalid */
1493 if ( IS_UID_REV ( sig
) ) {
1494 uid
->is_revoked
= 1;
1495 return; /* has been revoked */
1498 uid
->expiredate
= sig
->expiredate
;
1500 if(sig
->flags
.expired
)
1502 uid
->is_expired
= 1;
1503 return; /* has expired */
1506 uid
->created
= sig
->timestamp
; /* this one is okay */
1507 uid
->selfsigversion
= sig
->version
;
1508 /* If we got this far, it's not expired :) */
1509 uid
->is_expired
= 0;
1511 /* store the key flags in the helper variable for later processing */
1512 uid
->help_key_usage
=parse_key_usage(sig
);
1514 /* ditto for the key expiration */
1515 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_KEY_EXPIRE
, NULL
);
1516 if( p
&& buffer_to_u32(p
) )
1517 uid
->help_key_expire
= keycreated
+ buffer_to_u32(p
);
1519 uid
->help_key_expire
= 0;
1521 /* Set the primary user ID flag - we will later wipe out some
1522 * of them to only have one in our keyblock */
1523 uid
->is_primary
= 0;
1524 p
= parse_sig_subpkt ( sig
->hashed
, SIGSUBPKT_PRIMARY_UID
, NULL
);
1526 uid
->is_primary
= 2;
1527 /* We could also query this from the unhashed area if it is not in
1528 * the hased area and then later try to decide which is the better
1529 * there should be no security problem with this.
1530 * For now we only look at the hashed one.
1533 /* Now build the preferences list. These must come from the
1534 hashed section so nobody can modify the ciphers a key is
1535 willing to accept. */
1536 p
= parse_sig_subpkt ( sig
->hashed
, SIGSUBPKT_PREF_SYM
, &n
);
1537 sym
= p
; nsym
= p
?n
:0;
1538 p
= parse_sig_subpkt ( sig
->hashed
, SIGSUBPKT_PREF_HASH
, &n
);
1539 hash
= p
; nhash
= p
?n
:0;
1540 p
= parse_sig_subpkt ( sig
->hashed
, SIGSUBPKT_PREF_COMPR
, &n
);
1541 zip
= p
; nzip
= p
?n
:0;
1544 n
= nsym
+ nhash
+ nzip
;
1548 uid
->prefs
= xmalloc (sizeof (*uid
->prefs
) * (n
+1));
1550 for (; nsym
; nsym
--, n
++) {
1551 uid
->prefs
[n
].type
= PREFTYPE_SYM
;
1552 uid
->prefs
[n
].value
= *sym
++;
1554 for (; nhash
; nhash
--, n
++) {
1555 uid
->prefs
[n
].type
= PREFTYPE_HASH
;
1556 uid
->prefs
[n
].value
= *hash
++;
1558 for (; nzip
; nzip
--, n
++) {
1559 uid
->prefs
[n
].type
= PREFTYPE_ZIP
;
1560 uid
->prefs
[n
].value
= *zip
++;
1562 uid
->prefs
[n
].type
= PREFTYPE_NONE
; /* end of list marker */
1563 uid
->prefs
[n
].value
= 0;
1566 /* see whether we have the MDC feature */
1568 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_FEATURES
, &n
);
1569 if (p
&& n
&& (p
[0] & 0x01))
1572 /* and the keyserver modify flag */
1573 uid
->flags
.ks_modify
= 1;
1574 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_KS_FLAGS
, &n
);
1575 if (p
&& n
&& (p
[0] & 0x80))
1576 uid
->flags
.ks_modify
= 0;
1580 sig_to_revoke_info(PKT_signature
*sig
,struct revoke_info
*rinfo
)
1582 rinfo
->date
= sig
->timestamp
;
1583 rinfo
->algo
= sig
->pubkey_algo
;
1584 rinfo
->keyid
[0] = sig
->keyid
[0];
1585 rinfo
->keyid
[1] = sig
->keyid
[1];
1589 merge_selfsigs_main(KBNODE keyblock
, int *r_revoked
, struct revoke_info
*rinfo
)
1591 PKT_public_key
*pk
= NULL
;
1594 u32 sigdate
, uiddate
, uiddate2
;
1595 KBNODE signode
, uidnode
, uidnode2
;
1596 u32 curtime
= make_timestamp ();
1597 unsigned int key_usage
= 0;
1598 u32 keytimestamp
= 0;
1600 int key_expire_seen
= 0;
1601 byte sigversion
= 0;
1604 memset(rinfo
,0,sizeof(*rinfo
));
1606 if ( keyblock
->pkt
->pkttype
!= PKT_PUBLIC_KEY
)
1608 pk
= keyblock
->pkt
->pkt
.public_key
;
1609 keytimestamp
= pk
->timestamp
;
1611 keyid_from_pk( pk
, kid
);
1612 pk
->main_keyid
[0] = kid
[0];
1613 pk
->main_keyid
[1] = kid
[1];
1615 if ( pk
->version
< 4 ) {
1616 /* before v4 the key packet itself contains the expiration
1617 * date and there was no way to change it, so we start with
1618 * the one from the key packet */
1619 key_expire
= pk
->max_expiredate
;
1620 key_expire_seen
= 1;
1623 /* first pass: find the latest direct key self-signature.
1624 * We assume that the newest one overrides all others
1627 /* In case this key was already merged */
1633 sigdate
= 0; /* helper to find the latest signature */
1634 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_USER_ID
; k
= k
->next
) {
1635 if ( k
->pkt
->pkttype
== PKT_SIGNATURE
) {
1636 PKT_signature
*sig
= k
->pkt
->pkt
.signature
;
1637 if ( sig
->keyid
[0] == kid
[0] && sig
->keyid
[1]==kid
[1] ) {
1638 if ( check_key_signature( keyblock
, k
, NULL
) )
1639 ; /* signature did not verify */
1640 else if ( IS_KEY_REV (sig
) ){
1641 /* key has been revoked - there is no way to override
1642 * such a revocation, so we theoretically can stop now.
1643 * We should not cope with expiration times for revocations
1644 * here because we have to assume that an attacker can
1645 * generate all kinds of signatures. However due to the
1646 * fact that the key has been revoked it does not harm
1647 * either and by continuing we gather some more info on
1651 sig_to_revoke_info(sig
,rinfo
);
1653 else if ( IS_KEY_SIG (sig
) ) {
1654 /* Add any revocation keys onto the pk. This is
1655 particularly interesting since we normally only
1656 get data from the most recent 1F signature, but
1657 you need multiple 1F sigs to properly handle
1658 revocation keys (PGP does it this way, and a
1659 revocation key could be sensitive and hence in a
1660 different signature). */
1665 xrealloc(pk
->revkey
,sizeof(struct revocation_key
)*
1666 (pk
->numrevkeys
+sig
->numrevkeys
));
1668 for(i
=0;i
<sig
->numrevkeys
;i
++)
1669 memcpy(&pk
->revkey
[pk
->numrevkeys
++],
1671 sizeof(struct revocation_key
));
1674 if( sig
->timestamp
>= sigdate
) {
1675 if(sig
->flags
.expired
)
1676 ; /* signature has expired - ignore it */
1678 sigdate
= sig
->timestamp
;
1680 if( sig
->version
> sigversion
)
1681 sigversion
= sig
->version
;
1690 /* Remove dupes from the revocation keys */
1694 int i
,j
,x
,changed
=0;
1696 for(i
=0;i
<pk
->numrevkeys
;i
++)
1698 for(j
=i
+1;j
<pk
->numrevkeys
;j
++)
1700 if(memcmp(&pk
->revkey
[i
],&pk
->revkey
[j
],
1701 sizeof(struct revocation_key
))==0)
1705 for(x
=j
;x
<pk
->numrevkeys
-1;x
++)
1706 pk
->revkey
[x
]=pk
->revkey
[x
+1];
1716 pk
->revkey
=xrealloc(pk
->revkey
,
1717 pk
->numrevkeys
*sizeof(struct revocation_key
));
1722 /* some information from a direct key signature take precedence
1723 * over the same information given in UID sigs.
1725 PKT_signature
*sig
= signode
->pkt
->pkt
.signature
;
1728 key_usage
=parse_key_usage(sig
);
1730 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_KEY_EXPIRE
, NULL
);
1731 if( p
&& buffer_to_u32(p
) )
1733 key_expire
= keytimestamp
+ buffer_to_u32(p
);
1734 key_expire_seen
= 1;
1737 /* mark that key as valid: one direct key signature should
1738 * render a key as valid */
1742 /* pass 1.5: look for key revocation signatures that were not made
1743 by the key (i.e. did a revocation key issue a revocation for
1744 us?). Only bother to do this if there is a revocation key in
1745 the first place and we're not revoked already. */
1747 if(!*r_revoked
&& pk
->revkey
)
1748 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_USER_ID
; k
= k
->next
)
1750 if ( k
->pkt
->pkttype
== PKT_SIGNATURE
)
1752 PKT_signature
*sig
= k
->pkt
->pkt
.signature
;
1754 if(IS_KEY_REV(sig
) &&
1755 (sig
->keyid
[0]!=kid
[0] || sig
->keyid
[1]!=kid
[1]))
1757 int rc
=check_revocation_keys(pk
,sig
);
1761 sig_to_revoke_info(sig
,rinfo
);
1762 /* don't continue checking since we can't be any
1763 more revoked than this */
1766 else if(rc
==G10ERR_NO_PUBKEY
)
1767 pk
->maybe_revoked
=1;
1769 /* A failure here means the sig did not verify, was
1770 not issued by a revocation key, or a revocation
1771 key loop was broken. If a revocation key isn't
1772 findable, however, the key might be revoked and
1773 we don't know it. */
1775 /* TODO: In the future handle subkey and cert
1776 revocations? PGP doesn't, but it's in 2440. */
1781 /* second pass: look at the self-signature of all user IDs */
1782 signode
= uidnode
= NULL
;
1783 sigdate
= 0; /* helper to find the latest signature in one user ID */
1784 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
; k
= k
->next
) {
1785 if ( k
->pkt
->pkttype
== PKT_USER_ID
) {
1786 if ( uidnode
&& signode
)
1788 fixup_uidnode ( uidnode
, signode
, keytimestamp
);
1795 else if ( k
->pkt
->pkttype
== PKT_SIGNATURE
&& uidnode
) {
1796 PKT_signature
*sig
= k
->pkt
->pkt
.signature
;
1797 if ( sig
->keyid
[0] == kid
[0] && sig
->keyid
[1]==kid
[1] ) {
1798 if ( check_key_signature( keyblock
, k
, NULL
) )
1799 ; /* signature did not verify */
1800 else if ( (IS_UID_SIG (sig
) || IS_UID_REV (sig
))
1801 && sig
->timestamp
>= sigdate
)
1803 /* Note: we allow to invalidate cert revocations
1804 * by a newer signature. An attacker can't use this
1805 * because a key should be revoced with a key revocation.
1806 * The reason why we have to allow for that is that at
1807 * one time an email address may become invalid but later
1808 * the same email address may become valid again (hired,
1809 * fired, hired again).
1812 sigdate
= sig
->timestamp
;
1814 signode
->pkt
->pkt
.signature
->flags
.chosen_selfsig
=0;
1815 if( sig
->version
> sigversion
)
1816 sigversion
= sig
->version
;
1821 if ( uidnode
&& signode
) {
1822 fixup_uidnode ( uidnode
, signode
, keytimestamp
);
1826 /* If the key isn't valid yet, and we have
1827 --allow-non-selfsigned-uid set, then force it valid. */
1828 if(!pk
->is_valid
&& opt
.allow_non_selfsigned_uid
)
1831 log_info(_("Invalid key %s made valid by"
1832 " --allow-non-selfsigned-uid\n"),keystr_from_pk(pk
));
1836 /* The key STILL isn't valid, so try and find an ultimately
1837 trusted signature. */
1842 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
; k
=k
->next
)
1844 if ( k
->pkt
->pkttype
== PKT_USER_ID
)
1846 else if ( k
->pkt
->pkttype
== PKT_SIGNATURE
&& uidnode
)
1848 PKT_signature
*sig
= k
->pkt
->pkt
.signature
;
1850 if(sig
->keyid
[0] != kid
[0] || sig
->keyid
[1]!=kid
[1])
1852 PKT_public_key
*ultimate_pk
;
1854 ultimate_pk
=xmalloc_clear(sizeof(*ultimate_pk
));
1856 /* We don't want to use the full get_pubkey to
1857 avoid infinite recursion in certain cases.
1858 There is no reason to check that an ultimately
1859 trusted key is still valid - if it has been
1860 revoked or the user should also renmove the
1861 ultimate trust flag. */
1862 if(get_pubkey_fast(ultimate_pk
,sig
->keyid
)==0
1863 && check_key_signature2(keyblock
,k
,ultimate_pk
,
1864 NULL
,NULL
,NULL
,NULL
)==0
1865 && get_ownertrust(ultimate_pk
)==TRUST_ULTIMATE
)
1867 free_public_key(ultimate_pk
);
1872 free_public_key(ultimate_pk
);
1878 /* Record the highest selfsig version so we know if this is a v3
1879 key through and through, or a v3 key with a v4 selfsig
1880 somewhere. This is useful in a few places to know if the key
1881 must be treated as PGP2-style or OpenPGP-style. Note that a
1882 selfsig revocation with a higher version number will also raise
1883 this value. This is okay since such a revocation must be
1884 issued by the user (i.e. it cannot be issued by someone else to
1885 modify the key behavior.) */
1887 pk
->selfsigversion
=sigversion
;
1889 /* Now that we had a look at all user IDs we can now get some information
1890 * from those user IDs.
1894 /* find the latest user ID with key flags set */
1895 uiddate
= 0; /* helper to find the latest user ID */
1896 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
1898 if ( k
->pkt
->pkttype
== PKT_USER_ID
) {
1899 PKT_user_id
*uid
= k
->pkt
->pkt
.user_id
;
1900 if ( uid
->help_key_usage
&& uid
->created
> uiddate
) {
1901 key_usage
= uid
->help_key_usage
;
1902 uiddate
= uid
->created
;
1907 if ( !key_usage
) { /* no key flags at all: get it from the algo */
1908 key_usage
= openpgp_pk_algo_usage ( pk
->pubkey_algo
);
1910 else { /* check that the usage matches the usage as given by the algo */
1911 int x
= openpgp_pk_algo_usage ( pk
->pubkey_algo
);
1912 if ( x
) /* mask it down to the actual allowed usage */
1916 /* Whatever happens, it's a primary key, so it can certify. */
1917 pk
->pubkey_usage
= key_usage
|PUBKEY_USAGE_CERT
;
1919 if ( !key_expire_seen
) {
1920 /* find the latest valid user ID with a key expiration set
1921 * Note, that this may be a different one from the above because
1922 * some user IDs may have no expiration date set */
1924 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
1926 if ( k
->pkt
->pkttype
== PKT_USER_ID
) {
1927 PKT_user_id
*uid
= k
->pkt
->pkt
.user_id
;
1928 if ( uid
->help_key_expire
&& uid
->created
> uiddate
) {
1929 key_expire
= uid
->help_key_expire
;
1930 uiddate
= uid
->created
;
1936 /* Currently only v3 keys have a maximum expiration date, but I'll
1937 bet v5 keys get this feature again. */
1938 if(key_expire
==0 || (pk
->max_expiredate
&& key_expire
>pk
->max_expiredate
))
1939 key_expire
=pk
->max_expiredate
;
1941 pk
->has_expired
= key_expire
>= curtime
? 0 : key_expire
;
1942 pk
->expiredate
= key_expire
;
1944 /* Fixme: we should see how to get rid of the expiretime fields but
1945 * this needs changes at other places too. */
1947 /* and now find the real primary user ID and delete all others */
1948 uiddate
= uiddate2
= 0;
1949 uidnode
= uidnode2
= NULL
;
1950 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
; k
= k
->next
) {
1951 if ( k
->pkt
->pkttype
== PKT_USER_ID
&&
1952 !k
->pkt
->pkt
.user_id
->attrib_data
) {
1953 PKT_user_id
*uid
= k
->pkt
->pkt
.user_id
;
1954 if (uid
->is_primary
)
1956 if(uid
->created
> uiddate
)
1958 uiddate
= uid
->created
;
1961 else if(uid
->created
==uiddate
&& uidnode
)
1963 /* The dates are equal, so we need to do a
1964 different (and arbitrary) comparison. This
1965 should rarely, if ever, happen. It's good to
1966 try and guarantee that two different GnuPG
1967 users with two different keyrings at least pick
1968 the same primary. */
1969 if(cmp_user_ids(uid
,uidnode
->pkt
->pkt
.user_id
)>0)
1975 if(uid
->created
> uiddate2
)
1977 uiddate2
= uid
->created
;
1980 else if(uid
->created
==uiddate2
&& uidnode2
)
1982 if(cmp_user_ids(uid
,uidnode2
->pkt
->pkt
.user_id
)>0)
1989 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
1991 if ( k
->pkt
->pkttype
== PKT_USER_ID
&&
1992 !k
->pkt
->pkt
.user_id
->attrib_data
) {
1993 PKT_user_id
*uid
= k
->pkt
->pkt
.user_id
;
1995 uid
->is_primary
= 0;
1999 else if( uidnode2
) {
2000 /* none is flagged primary - use the latest user ID we have,
2001 and disambiguate with the arbitrary packet comparison. */
2002 uidnode2
->pkt
->pkt
.user_id
->is_primary
= 1;
2006 /* None of our uids were self-signed, so pick the one that
2007 sorts first to be the primary. This is the best we can do
2008 here since there are no self sigs to date the uids. */
2012 for(k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
2015 if(k
->pkt
->pkttype
==PKT_USER_ID
2016 && !k
->pkt
->pkt
.user_id
->attrib_data
)
2021 uidnode
->pkt
->pkt
.user_id
->is_primary
=1;
2026 if(cmp_user_ids(k
->pkt
->pkt
.user_id
,
2027 uidnode
->pkt
->pkt
.user_id
)>0)
2029 uidnode
->pkt
->pkt
.user_id
->is_primary
=0;
2031 uidnode
->pkt
->pkt
.user_id
->is_primary
=1;
2034 k
->pkt
->pkt
.user_id
->is_primary
=0; /* just to be
2044 merge_selfsigs_subkey( KBNODE keyblock
, KBNODE subnode
)
2046 PKT_public_key
*mainpk
= NULL
, *subpk
= NULL
;
2052 u32 curtime
= make_timestamp ();
2053 unsigned int key_usage
= 0;
2054 u32 keytimestamp
= 0;
2058 if ( subnode
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
)
2060 mainpk
= keyblock
->pkt
->pkt
.public_key
;
2061 if ( mainpk
->version
< 4 )
2062 return; /* (actually this should never happen) */
2063 keyid_from_pk( mainpk
, mainkid
);
2064 subpk
= subnode
->pkt
->pkt
.public_key
;
2065 keytimestamp
= subpk
->timestamp
;
2067 subpk
->is_valid
= 0;
2068 subpk
->main_keyid
[0] = mainpk
->main_keyid
[0];
2069 subpk
->main_keyid
[1] = mainpk
->main_keyid
[1];
2071 /* find the latest key binding self-signature. */
2073 sigdate
= 0; /* helper to find the latest signature */
2074 for(k
=subnode
->next
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
2076 if ( k
->pkt
->pkttype
== PKT_SIGNATURE
) {
2077 sig
= k
->pkt
->pkt
.signature
;
2078 if ( sig
->keyid
[0] == mainkid
[0] && sig
->keyid
[1]==mainkid
[1] ) {
2079 if ( check_key_signature( keyblock
, k
, NULL
) )
2080 ; /* signature did not verify */
2081 else if ( IS_SUBKEY_REV (sig
) ) {
2082 /* Note that this means that the date on a
2083 revocation sig does not matter - even if the
2084 binding sig is dated after the revocation sig,
2085 the subkey is still marked as revoked. This
2086 seems ok, as it is just as easy to make new
2087 subkeys rather than re-sign old ones as the
2088 problem is in the distribution. Plus, PGP (7)
2089 does this the same way. */
2090 subpk
->is_revoked
= 1;
2091 sig_to_revoke_info(sig
,&subpk
->revoked
);
2092 /* although we could stop now, we continue to
2093 * figure out other information like the old expiration
2096 else if ( IS_SUBKEY_SIG (sig
) && sig
->timestamp
>= sigdate
)
2098 if(sig
->flags
.expired
)
2099 ; /* signature has expired - ignore it */
2102 sigdate
= sig
->timestamp
;
2104 signode
->pkt
->pkt
.signature
->flags
.chosen_selfsig
=0;
2111 /* no valid key binding */
2115 sig
= signode
->pkt
->pkt
.signature
;
2116 sig
->flags
.chosen_selfsig
=1; /* so we know which selfsig we chose later */
2118 key_usage
=parse_key_usage(sig
);
2121 /* no key flags at all: get it from the algo */
2122 key_usage
= openpgp_pk_algo_usage ( subpk
->pubkey_algo
);
2126 /* check that the usage matches the usage as given by the algo */
2127 int x
= openpgp_pk_algo_usage ( subpk
->pubkey_algo
);
2128 if ( x
) /* mask it down to the actual allowed usage */
2132 subpk
->pubkey_usage
= key_usage
;
2134 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_KEY_EXPIRE
, NULL
);
2135 if ( p
&& buffer_to_u32(p
) )
2136 key_expire
= keytimestamp
+ buffer_to_u32(p
);
2139 subpk
->has_expired
= key_expire
>= curtime
? 0 : key_expire
;
2140 subpk
->expiredate
= key_expire
;
2142 /* algo doesn't exist */
2143 if(openpgp_pk_test_algo(subpk
->pubkey_algo
))
2146 subpk
->is_valid
= 1;
2148 /* Find the first 0x19 embedded signature on our self-sig. */
2149 if(subpk
->backsig
==0)
2154 /* We do this while() since there may be other embedded
2155 signatures in the future. We only want 0x19 here. */
2156 while((p
=enum_sig_subpkt(sig
->hashed
,
2157 SIGSUBPKT_SIGNATURE
,&n
,&seq
,NULL
)))
2158 if(n
>3 && ((p
[0]==3 && p
[2]==0x19) || (p
[0]==4 && p
[1]==0x19)))
2164 /* It is safe to have this in the unhashed area since the
2165 0x19 is located on the selfsig for convenience, not
2167 while((p
=enum_sig_subpkt(sig
->unhashed
,SIGSUBPKT_SIGNATURE
,
2169 if(n
>3 && ((p
[0]==3 && p
[2]==0x19) || (p
[0]==4 && p
[1]==0x19)))
2175 PKT_signature
*backsig
=xmalloc_clear(sizeof(PKT_signature
));
2176 IOBUF backsig_buf
=iobuf_temp_with_content(p
,n
);
2178 if(parse_signature(backsig_buf
,PKT_SIGNATURE
,n
,backsig
)==0)
2180 if(check_backsig(mainpk
,subpk
,backsig
)==0)
2186 iobuf_close(backsig_buf
);
2187 free_seckey_enc(backsig
);
2194 * Merge information from the self-signatures with the key, so that
2195 * we can later use them more easy.
2196 * The function works by first applying the self signatures to the
2197 * primary key and the to each subkey.
2198 * Here are the rules we use to decide which inormation from which
2199 * self-signature is used:
2200 * We check all self signatures or validity and ignore all invalid signatures.
2201 * All signatures are then ordered by their creation date ....
2202 * For the primary key:
2206 merge_selfsigs( KBNODE keyblock
)
2210 struct revoke_info rinfo
;
2211 PKT_public_key
*main_pk
;
2215 if ( keyblock
->pkt
->pkttype
!= PKT_PUBLIC_KEY
) {
2216 if (keyblock
->pkt
->pkttype
== PKT_SECRET_KEY
) {
2217 log_error ("expected public key but found secret key "
2219 /* we better exit here becuase a public key is expected at
2220 other places too. FIXME: Figure this out earlier and
2221 don't get to here at all */
2227 merge_selfsigs_main ( keyblock
, &revoked
, &rinfo
);
2229 /* now merge in the data from each of the subkeys */
2230 for(k
=keyblock
; k
; k
= k
->next
) {
2231 if ( k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2232 merge_selfsigs_subkey ( keyblock
, k
);
2236 main_pk
= keyblock
->pkt
->pkt
.public_key
;
2237 if ( revoked
|| main_pk
->has_expired
|| !main_pk
->is_valid
) {
2238 /* if the primary key is revoked, expired, or invalid we
2239 * better set the appropriate flags on that key and all
2241 for(k
=keyblock
; k
; k
= k
->next
) {
2242 if ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
2243 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2244 PKT_public_key
*pk
= k
->pkt
->pkt
.public_key
;
2245 if(!main_pk
->is_valid
)
2247 if(revoked
&& !pk
->is_revoked
)
2249 pk
->is_revoked
= revoked
;
2250 memcpy(&pk
->revoked
,&rinfo
,sizeof(rinfo
));
2252 if(main_pk
->has_expired
)
2253 pk
->has_expired
= main_pk
->has_expired
;
2259 /* set the preference list of all keys to those of the primary real
2260 * user ID. Note: we use these preferences when we don't know by
2261 * which user ID the key has been selected.
2262 * fixme: we should keep atoms of commonly used preferences or
2263 * use reference counting to optimize the preference lists storage.
2264 * FIXME: it might be better to use the intersection of
2266 * Do a similar thing for the MDC feature flag.
2270 for (k
=keyblock
; k
&& k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
; k
= k
->next
) {
2271 if (k
->pkt
->pkttype
== PKT_USER_ID
2272 && !k
->pkt
->pkt
.user_id
->attrib_data
2273 && k
->pkt
->pkt
.user_id
->is_primary
) {
2274 prefs
= k
->pkt
->pkt
.user_id
->prefs
;
2275 mdc_feature
= k
->pkt
->pkt
.user_id
->flags
.mdc
;
2279 for(k
=keyblock
; k
; k
= k
->next
) {
2280 if ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
2281 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2282 PKT_public_key
*pk
= k
->pkt
->pkt
.public_key
;
2285 pk
->prefs
= copy_prefs (prefs
);
2286 pk
->mdc_feature
= mdc_feature
;
2293 * Merge the secret keys from secblock into the pubblock thereby
2294 * replacing the public (sub)keys with their secret counterparts Hmmm:
2295 * It might be better to get away from the concept of entire secret
2296 * keys at all and have a way to store just the real secret parts
2300 merge_public_with_secret ( KBNODE pubblock
, KBNODE secblock
)
2304 assert ( pubblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
);
2305 assert ( secblock
->pkt
->pkttype
== PKT_SECRET_KEY
);
2307 for (pub
=pubblock
; pub
; pub
= pub
->next
) {
2308 if ( pub
->pkt
->pkttype
== PKT_PUBLIC_KEY
) {
2309 PKT_public_key
*pk
= pub
->pkt
->pkt
.public_key
;
2310 PKT_secret_key
*sk
= secblock
->pkt
->pkt
.secret_key
;
2311 assert ( pub
== pubblock
); /* only in the first node */
2312 /* there is nothing to compare in this case, so just replace
2313 * some information */
2314 copy_public_parts_to_secret_key ( pk
, sk
);
2315 free_public_key ( pk
);
2316 pub
->pkt
->pkttype
= PKT_SECRET_KEY
;
2317 pub
->pkt
->pkt
.secret_key
= copy_secret_key (NULL
, sk
);
2319 else if ( pub
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2321 PKT_public_key
*pk
= pub
->pkt
->pkt
.public_key
;
2323 /* this is more complicated: it may happen that the sequence
2324 * of the subkeys dosn't match, so we have to find the
2325 * appropriate secret key */
2326 for (sec
=secblock
->next
; sec
; sec
= sec
->next
) {
2327 if ( sec
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
2328 PKT_secret_key
*sk
= sec
->pkt
->pkt
.secret_key
;
2329 if ( !cmp_public_secret_key ( pk
, sk
) ) {
2330 copy_public_parts_to_secret_key ( pk
, sk
);
2331 free_public_key ( pk
);
2332 pub
->pkt
->pkttype
= PKT_SECRET_SUBKEY
;
2333 pub
->pkt
->pkt
.secret_key
= copy_secret_key (NULL
, sk
);
2339 BUG(); /* already checked in premerge */
2344 /* This function checks that for every public subkey a corresponding
2345 * secret subkey is available and deletes the public subkey otherwise.
2346 * We need this function because we can't delete it later when we
2347 * actually merge the secret parts into the pubring.
2348 * The function also plays some games with the node flags.
2351 premerge_public_with_secret ( KBNODE pubblock
, KBNODE secblock
)
2355 assert ( pubblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
);
2356 assert ( secblock
->pkt
->pkttype
== PKT_SECRET_KEY
);
2358 for (pub
=pubblock
,last
=NULL
; pub
; last
= pub
, pub
= pub
->next
) {
2359 pub
->flag
&= ~3; /* reset bits 0 and 1 */
2360 if ( pub
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) {
2362 PKT_public_key
*pk
= pub
->pkt
->pkt
.public_key
;
2364 for (sec
=secblock
->next
; sec
; sec
= sec
->next
) {
2365 if ( sec
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) {
2366 PKT_secret_key
*sk
= sec
->pkt
->pkt
.secret_key
;
2367 if ( !cmp_public_secret_key ( pk
, sk
) ) {
2368 if ( sk
->protect
.s2k
.mode
== 1001 ) {
2369 /* The secret parts are not available so
2370 we can't use that key for signing etc.
2371 Fix the pubkey usage */
2372 pk
->pubkey_usage
&= ~(PUBKEY_USAGE_SIG
2373 |PUBKEY_USAGE_AUTH
);
2375 /* transfer flag bits 0 and 1 to the pubblock */
2376 pub
->flag
|= (sec
->flag
&3);
2385 log_info (_("no secret subkey"
2386 " for public subkey %s - ignoring\n"),
2387 keystr_from_pk (pk
));
2388 /* we have to remove the subkey in this case */
2390 /* find the next subkey */
2391 for (next
=pub
->next
,ll
=pub
;
2392 next
&& next
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
;
2393 ll
= next
, next
= next
->next
)
2397 /* release this public subkey with all sigs */
2399 release_kbnode( pub
);
2400 /* let the loop continue */
2405 /* We need to copy the found bits (0 and 1) from the secret key to
2406 the public key. This has already been done for the subkeys but
2407 got lost on the primary key - fix it here *. */
2408 pubblock
->flag
|= (secblock
->flag
& 3);
2414 /* See see whether the key fits
2415 * our requirements and in case we do not
2416 * request the primary key, we should select
2417 * a suitable subkey.
2418 * FIXME: Check against PGP 7 whether we still need a kludge
2419 * to favor type 16 keys over type 20 keys when type 20
2420 * has not been explitely requested.
2421 * Returns: True when a suitable key has been found.
2423 * We have to distinguish four cases: FIXME!
2424 * 1. No usage and no primary key requested
2425 * Examples for this case are that we have a keyID to be used
2426 * for decrytion or verification.
2427 * 2. No usage but primary key requested
2428 * This is the case for all functions which work on an
2429 * entire keyblock, e.g. for editing or listing
2430 * 3. Usage and primary key requested
2432 * 4. Usage but no primary key requested
2434 * FIXME: Tell what is going to happen here and something about the rationale
2435 * Note: We don't use this function if no specific usage is requested;
2436 * This way the getkey functions can be used for plain key listings.
2438 * CTX ist the keyblock we are investigating, if FOUNDK is not NULL this
2439 * is the key we actually found by looking at the keyid or a fingerprint and
2440 * may eitehr point to the primary or one of the subkeys.
2444 finish_lookup (GETKEY_CTX ctx
)
2446 KBNODE keyblock
= ctx
->keyblock
;
2448 KBNODE foundk
= NULL
;
2449 PKT_user_id
*foundu
= NULL
;
2450 #define USAGE_MASK (PUBKEY_USAGE_SIG|PUBKEY_USAGE_ENC|PUBKEY_USAGE_CERT)
2451 unsigned int req_usage
= ( ctx
->req_usage
& USAGE_MASK
);
2452 /* Request the primary if we're certifying another key, and also
2453 if signing data while --pgp6 or --pgp7 is on since pgp 6 and 7
2454 do not understand signatures made by a signing subkey. PGP 8
2456 int req_prim
= (ctx
->req_usage
& PUBKEY_USAGE_CERT
) ||
2457 ((PGP6
|| PGP7
) && (ctx
->req_usage
& PUBKEY_USAGE_SIG
));
2460 u32 curtime
= make_timestamp ();
2462 assert( keyblock
->pkt
->pkttype
== PKT_PUBLIC_KEY
);
2464 ctx
->found_key
= NULL
;
2467 for (k
=keyblock
; k
; k
= k
->next
) {
2468 if ( (k
->flag
& 1) ) {
2469 assert ( k
->pkt
->pkttype
== PKT_PUBLIC_KEY
2470 || k
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
);
2477 for (k
=keyblock
; k
; k
= k
->next
) {
2478 if ( (k
->flag
& 2) ) {
2479 assert (k
->pkt
->pkttype
== PKT_USER_ID
);
2480 foundu
= k
->pkt
->pkt
.user_id
;
2486 log_debug( "finish_lookup: checking key %08lX (%s)(req_usage=%x)\n",
2487 (ulong
)keyid_from_pk( keyblock
->pkt
->pkt
.public_key
, NULL
),
2488 foundk
? "one":"all", req_usage
);
2491 latest_key
= foundk
? foundk
:keyblock
;
2496 PKT_public_key
*pk
= foundk
->pkt
->pkt
.public_key
;
2498 free_user_id (pk
->user_id
);
2499 pk
->user_id
= scopy_user_id (foundu
);
2500 ctx
->found_key
= foundk
;
2501 cache_user_id( keyblock
);
2502 return 1; /* found */
2507 /* do not look at subkeys if a certification key is requested */
2508 if ((!foundk
|| foundk
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
) && !req_prim
) {
2510 /* either start a loop or check just this one subkey */
2511 for (k
=foundk
?foundk
:keyblock
; k
; k
= nextk
) {
2514 if ( k
->pkt
->pkttype
!= PKT_PUBLIC_SUBKEY
)
2517 nextk
= NULL
; /* what a hack */
2518 pk
= k
->pkt
->pkt
.public_key
;
2520 log_debug( "\tchecking subkey %08lX\n",
2521 (ulong
)keyid_from_pk( pk
, NULL
));
2522 if ( !pk
->is_valid
) {
2524 log_debug( "\tsubkey not valid\n");
2527 if ( pk
->is_revoked
) {
2529 log_debug( "\tsubkey has been revoked\n");
2532 if ( pk
->has_expired
) {
2534 log_debug( "\tsubkey has expired\n");
2537 if ( pk
->timestamp
> curtime
&& !opt
.ignore_valid_from
) {
2539 log_debug( "\tsubkey not yet valid\n");
2543 if ( !((pk
->pubkey_usage
&USAGE_MASK
) & req_usage
) ) {
2545 log_debug( "\tusage does not match: want=%x have=%x\n",
2546 req_usage
, pk
->pubkey_usage
);
2551 log_debug( "\tsubkey looks fine\n");
2552 if ( pk
->timestamp
> latest_date
) {
2553 latest_date
= pk
->timestamp
;
2559 /* Okay now try the primary key unless we want an exact
2560 * key ID match on a subkey */
2561 if ((!latest_key
&& !(ctx
->exact
&& foundk
!= keyblock
)) || req_prim
) {
2563 if (DBG_CACHE
&& !foundk
&& !req_prim
)
2564 log_debug( "\tno suitable subkeys found - trying primary\n");
2565 pk
= keyblock
->pkt
->pkt
.public_key
;
2566 if ( !pk
->is_valid
) {
2568 log_debug( "\tprimary key not valid\n");
2570 else if ( pk
->is_revoked
) {
2572 log_debug( "\tprimary key has been revoked\n");
2574 else if ( pk
->has_expired
) {
2576 log_debug( "\tprimary key has expired\n");
2578 else if ( !((pk
->pubkey_usage
&USAGE_MASK
) & req_usage
) ) {
2580 log_debug( "\tprimary key usage does not match: "
2581 "want=%x have=%x\n",
2582 req_usage
, pk
->pubkey_usage
);
2586 log_debug( "\tprimary key may be used\n");
2587 latest_key
= keyblock
;
2588 latest_date
= pk
->timestamp
;
2592 if ( !latest_key
) {
2594 log_debug("\tno suitable key found - giving up\n");
2600 log_debug( "\tusing key %08lX\n",
2601 (ulong
)keyid_from_pk( latest_key
->pkt
->pkt
.public_key
, NULL
) );
2604 PKT_public_key
*pk
= latest_key
->pkt
->pkt
.public_key
;
2606 free_user_id (pk
->user_id
);
2607 pk
->user_id
= scopy_user_id (foundu
);
2610 ctx
->found_key
= latest_key
;
2612 if (latest_key
!= keyblock
&& opt
.verbose
)
2615 xstrdup(keystr_from_pk(latest_key
->pkt
->pkt
.public_key
));
2616 log_info(_("using subkey %s instead of primary key %s\n"),
2617 tempkeystr
, keystr_from_pk(keyblock
->pkt
->pkt
.public_key
));
2621 cache_user_id( keyblock
);
2623 return 1; /* found */
2628 lookup( GETKEY_CTX ctx
, KBNODE
*ret_keyblock
, int secmode
)
2631 KBNODE secblock
= NULL
; /* helper */
2632 int no_suitable_key
= 0;
2635 while (!(rc
= keydb_search (ctx
->kr_handle
, ctx
->items
, ctx
->nitems
))) {
2636 /* If we are searching for the first key we have to make sure
2637 that the next interation does not no an implicit reset.
2638 This can be triggered by an empty key ring. */
2639 if (ctx
->nitems
&& ctx
->items
->mode
== KEYDB_SEARCH_MODE_FIRST
)
2640 ctx
->items
->mode
= KEYDB_SEARCH_MODE_NEXT
;
2642 rc
= keydb_get_keyblock (ctx
->kr_handle
, &ctx
->keyblock
);
2644 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc
));
2650 /* find the correspondig public key and use this
2651 * this one for the selection process */
2653 KBNODE k
= ctx
->keyblock
;
2655 if (k
->pkt
->pkttype
!= PKT_SECRET_KEY
)
2658 keyid_from_sk (k
->pkt
->pkt
.secret_key
, aki
);
2659 k
= get_pubkeyblock (aki
);
2663 log_info(_("key %s: secret key without public key"
2664 " - skipped\n"), keystr(aki
));
2667 secblock
= ctx
->keyblock
;
2670 premerge_public_with_secret ( ctx
->keyblock
, secblock
);
2673 /* warning: node flag bits 0 and 1 should be preserved by
2674 * merge_selfsigs. For secret keys, premerge did tranfer the
2675 * keys to the keyblock */
2676 merge_selfsigs ( ctx
->keyblock
);
2677 if ( finish_lookup (ctx
) ) {
2678 no_suitable_key
= 0;
2680 merge_public_with_secret ( ctx
->keyblock
,
2682 release_kbnode (secblock
);
2688 no_suitable_key
= 1;
2691 /* release resources and continue search */
2693 release_kbnode( secblock
);
2696 release_kbnode( ctx
->keyblock
);
2697 ctx
->keyblock
= NULL
;
2701 if( rc
&& rc
!= -1 )
2702 log_error("keydb_search failed: %s\n", g10_errstr(rc
));
2705 *ret_keyblock
= ctx
->keyblock
; /* return the keyblock */
2706 ctx
->keyblock
= NULL
;
2708 else if (rc
== -1 && no_suitable_key
)
2709 rc
= secmode
? G10ERR_UNU_SECKEY
: G10ERR_UNU_PUBKEY
;
2711 rc
= secmode
? G10ERR_NO_SECKEY
: G10ERR_NO_PUBKEY
;
2714 release_kbnode( secblock
);
2717 release_kbnode( ctx
->keyblock
);
2718 ctx
->keyblock
= NULL
;
2728 * FIXME: Replace by the generic function
2729 * It does not work as it is right now - it is used at
2730 * 2 places: a) to get the key for an anonyous recipient
2731 * b) to get the ultimately trusted keys.
2732 * The a) usage might have some problems.
2734 * set with_subkeys true to include subkeys
2735 * set with_spm true to include secret-parts-missing keys
2737 * Enumerate all primary secret keys. Caller must use these procedure:
2738 * 1) create a void pointer and initialize it to NULL
2739 * 2) pass this void pointer by reference to this function
2740 * and provide space for the secret key (pass a buffer for sk)
2741 * 3) call this function as long as it does not return -1
2743 * 4) Always call this function a last time with SK set to NULL,
2744 * so that can free it's context.
2747 enum_secret_keys( void **context
, PKT_secret_key
*sk
,
2748 int with_subkeys
, int with_spm
)
2760 if( !c
) { /* make a new context */
2761 c
= xmalloc_clear( sizeof *c
);
2763 c
->hd
= keydb_new (1);
2769 if( !sk
) { /* free the context */
2770 keydb_release (c
->hd
);
2771 release_kbnode (c
->keyblock
);
2781 /* get the next secret key from the current keyblock */
2782 for (; c
->node
; c
->node
= c
->node
->next
) {
2783 if ((c
->node
->pkt
->pkttype
== PKT_SECRET_KEY
2785 && c
->node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
) )
2786 && !(c
->node
->pkt
->pkt
.secret_key
->protect
.s2k
.mode
==1001
2788 copy_secret_key (sk
, c
->node
->pkt
->pkt
.secret_key
);
2789 c
->node
= c
->node
->next
;
2790 return 0; /* found */
2793 release_kbnode (c
->keyblock
);
2794 c
->keyblock
= c
->node
= NULL
;
2796 rc
= c
->first
? keydb_search_first (c
->hd
) : keydb_search_next (c
->hd
);
2799 keydb_release (c
->hd
); c
->hd
= NULL
;
2801 return -1; /* eof */
2804 rc
= keydb_get_keyblock (c
->hd
, &c
->keyblock
);
2805 c
->node
= c
->keyblock
;
2808 return rc
; /* error */
2813 /*********************************************
2814 *********** user ID printing helpers *******
2815 *********************************************/
2818 * Return a string with a printable representation of the user_id.
2819 * this string must be freed by xfree.
2822 get_user_id_string( u32
*keyid
)
2827 /* try it two times; second pass reads from key resources */
2830 for(r
=user_id_db
; r
; r
= r
->next
)
2833 for (a
=r
->keyids
; a
; a
= a
->next
)
2835 if( a
->keyid
[0] == keyid
[0] && a
->keyid
[1] == keyid
[1] )
2837 p
= xmalloc( keystrlen() + 1 + r
->len
+ 1 );
2838 sprintf(p
, "%s %.*s", keystr(keyid
), r
->len
, r
->name
);
2843 } while( ++pass
< 2 && !get_pubkey( NULL
, keyid
) );
2844 p
= xmalloc( keystrlen() + 5 );
2845 sprintf(p
, "%s [?]", keystr(keyid
));
2851 get_user_id_string_native ( u32
*keyid
)
2853 char *p
= get_user_id_string( keyid
);
2854 char *p2
= utf8_to_native( p
, strlen(p
), 0 );
2861 get_long_user_id_string( u32
*keyid
)
2866 /* try it two times; second pass reads from key resources */
2868 for(r
=user_id_db
; r
; r
= r
->next
) {
2870 for (a
=r
->keyids
; a
; a
= a
->next
) {
2871 if( a
->keyid
[0] == keyid
[0] && a
->keyid
[1] == keyid
[1] ) {
2872 p
= xmalloc( r
->len
+ 20 );
2873 sprintf(p
, "%08lX%08lX %.*s",
2874 (ulong
)keyid
[0], (ulong
)keyid
[1],
2880 } while( ++pass
< 2 && !get_pubkey( NULL
, keyid
) );
2882 sprintf(p
, "%08lX%08lX [?]", (ulong
)keyid
[0], (ulong
)keyid
[1] );
2887 get_user_id( u32
*keyid
, size_t *rn
)
2893 /* try it two times; second pass reads from key resources */
2895 for(r
=user_id_db
; r
; r
= r
->next
) {
2897 for (a
=r
->keyids
; a
; a
= a
->next
) {
2898 if( a
->keyid
[0] == keyid
[0] && a
->keyid
[1] == keyid
[1] ) {
2899 p
= xmalloc( r
->len
);
2900 memcpy(p
, r
->name
, r
->len
);
2906 } while( ++pass
< 2 && !get_pubkey( NULL
, keyid
) );
2907 p
= xstrdup( user_id_not_found_utf8 () );
2913 get_user_id_native( u32
*keyid
)
2916 char *p
= get_user_id( keyid
, &rn
);
2917 char *p2
= utf8_to_native( p
, rn
, 0 );
2923 get_ctx_handle(GETKEY_CTX ctx
)
2925 return ctx
->kr_handle
;
2929 free_akl(struct akl
*akl
)
2932 free_keyserver_spec(akl
->spec
);
2940 while(opt
.auto_key_locate
)
2942 struct akl
*akl2
=opt
.auto_key_locate
;
2943 opt
.auto_key_locate
=opt
.auto_key_locate
->next
;
2949 parse_auto_key_locate(char *options
)
2953 while((tok
=optsep(&options
)))
2955 struct akl
*akl
,*check
,*last
=NULL
;
2961 akl
=xmalloc_clear(sizeof(*akl
));
2963 if(ascii_strcasecmp(tok
,"ldap")==0)
2965 else if(ascii_strcasecmp(tok
,"keyserver")==0)
2966 akl
->type
=AKL_KEYSERVER
;
2968 else if(ascii_strcasecmp(tok
,"cert")==0)
2972 else if(ascii_strcasecmp(tok
,"pka")==0)
2975 else if((akl
->spec
=parse_keyserver_uri(tok
,1,NULL
,0)))
2983 /* We must maintain the order the user gave us */
2984 for(check
=opt
.auto_key_locate
;check
;last
=check
,check
=check
->next
)
2986 /* Check for duplicates */
2987 if(check
->type
==akl
->type
2988 && (akl
->type
!=AKL_SPEC
2989 || (akl
->type
==AKL_SPEC
2990 && strcmp(check
->spec
->uri
,akl
->spec
->uri
)==0)))
3003 opt
.auto_key_locate
=akl
;