* gpg.texi (GPG Configuration Options): Make http_proxy option
[gnupg.git] / g10 / getkey.c
blobd526398c68a99ac0eb059138a59ba2d946adaa6d
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,
20 * USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
30 #include "gpg.h"
31 #include "util.h"
32 #include "packet.h"
33 #include "iobuf.h"
34 #include "keydb.h"
35 #include "options.h"
36 #include "main.h"
37 #include "trustdb.h"
38 #include "i18n.h"
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
46 #endif
48 struct getkey_ctx_s {
49 int exact;
50 KBNODE keyblock;
51 KBPOS kbpos;
52 KBNODE found_key; /* pointer into some keyblock */
53 int last_rc;
54 int req_usage;
55 int req_algo;
56 KEYDB_HANDLE kr_handle;
57 int not_allocated;
58 int nitems;
59 KEYDB_SEARCH_DESC items[1];
62 #if 0
63 static struct {
64 int any;
65 int okay_count;
66 int nokey_count;
67 int error_count;
68 } lkup_stats[21];
69 #endif
71 typedef struct keyid_list {
72 struct keyid_list *next;
73 u32 keyid[2];
74 } *keyid_list_t;
77 #if MAX_PK_CACHE_ENTRIES
78 typedef struct pk_cache_entry {
79 struct pk_cache_entry *next;
80 u32 keyid[2];
81 PKT_public_key *pk;
82 } *pk_cache_entry_t;
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;
86 #endif
88 #if MAX_UID_CACHE_ENTRIES < 5
89 #error we really need the userid cache
90 #endif
91 typedef struct user_id_db {
92 struct user_id_db *next;
93 keyid_list_t keyids;
94 int len;
95 char name[1];
96 } *user_id_db_t;
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 );
103 #if 0
104 static void
105 print_stats()
107 int i;
108 for(i=0; i < DIM(lkup_stats); i++ ) {
109 if( lkup_stats[i].any )
110 fprintf(stderr,
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 );
118 #endif
121 void
122 cache_public_key( PKT_public_key *pk )
124 #if MAX_PK_CACHE_ENTRIES
125 pk_cache_entry_t ce;
126 u32 keyid[2];
128 if( pk_cache_disabled )
129 return;
131 if( pk->dont_cache )
132 return;
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 );
139 else
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] ) {
144 if( DBG_CACHE )
145 log_debug("cache_public_key: already in cache\n");
146 return;
149 if( pk_cache_entries >= MAX_PK_CACHE_ENTRIES ) {
150 /* fixme: use another algorithm to free some cache slots */
151 pk_cache_disabled=1;
152 if( opt.verbose > 1 )
153 log_info(_("too many entries in pk cache - disabled\n"));
154 return;
156 pk_cache_entries++;
157 ce = xmalloc( sizeof *ce );
158 ce->next = pk_cache;
159 pk_cache = ce;
160 ce->pk = copy_public_key( NULL, pk );
161 ce->keyid[0] = keyid[0];
162 ce->keyid[1] = keyid[1];
163 #endif
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. */
170 static const char *
171 user_id_not_found_utf8 (void)
173 static char *text;
175 if (!text)
176 text = native_to_utf8 (_("[User ID not found]"));
177 return text;
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
188 static const char *
189 get_primary_uid ( KBNODE keyblock, size_t *uidlen )
191 KBNODE k;
192 const char *s;
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);
204 return s;
208 static void
209 release_keyid_list ( keyid_list_t k )
211 while ( k ) {
212 keyid_list_t k2 = k->next;
213 xfree (k);
214 k = k2;
218 /****************
219 * Store the association of keyid and userid
220 * Feed only public keys to this function.
222 static void
223 cache_user_id( KBNODE keyblock )
225 user_id_db_t r;
226 const char *uid;
227 size_t uidlen;
228 keyid_list_t keyids = NULL;
229 KBNODE k;
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] ) {
244 if( DBG_CACHE )
245 log_debug("cache_user_id: already in cache\n");
246 release_keyid_list ( keyids );
247 xfree ( a );
248 return;
252 /* now put it into the cache */
253 a->next = keyids;
254 keyids = a;
257 if ( !keyids )
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 */
265 r = user_id_db;
266 user_id_db = r->next;
267 release_keyid_list ( r->keyids );
268 xfree(r);
269 uid_cache_entries--;
271 r = xmalloc( sizeof *r + uidlen-1 );
272 r->keyids = keyids;
273 r->len = uidlen;
274 memcpy(r->name, uid, r->len);
275 r->next = user_id_db;
276 user_id_db = r;
277 uid_cache_entries++;
281 void
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 ) {
289 ce2 = ce->next;
290 free_public_key( ce->pk );
291 xfree( ce );
293 pk_cache_disabled=1;
294 pk_cache_entries = 0;
295 pk_cache = NULL;
297 #endif
298 /* fixme: disable user id cache ? */
302 static void
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 );
313 static void
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);
326 /****************
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 )
334 int internal = 0;
335 int rc = 0;
337 #if MAX_PK_CACHE_ENTRIES
338 if(pk)
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
342 cached. */
343 pk_cache_entry_t ce;
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 );
349 return 0;
353 #endif
354 /* more init stuff */
355 if( !pk ) {
356 pk = xmalloc_clear( sizeof *pk );
357 internal++;
361 /* do a lookup */
362 { struct getkey_ctx_s ctx;
363 KBNODE kb = NULL;
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);
368 ctx.nitems = 1;
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 );
375 if ( !rc ) {
376 pk_from_block ( &ctx, pk, kb );
378 get_pubkey_end( &ctx );
379 release_kbnode ( kb );
381 if( !rc )
382 goto leave;
384 rc = G10ERR_NO_PUBKEY;
386 leave:
387 if( !rc )
388 cache_public_key( pk );
389 if( internal )
390 free_public_key(pk);
391 return rc;
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)
402 int rc = 0;
403 KEYDB_HANDLE hd;
404 KBNODE keyblock;
405 u32 pkid[2];
407 assert (pk);
408 #if MAX_PK_CACHE_ENTRIES
409 { /* Try to get it from the cache */
410 pk_cache_entry_t ce;
412 for (ce = pk_cache; ce; ce = ce->next)
414 if (ce->keyid[0] == keyid[0] && ce->keyid[1] == keyid[1])
416 if (pk)
417 copy_public_key (pk, ce->pk);
418 return 0;
422 #endif
424 hd = keydb_new (0);
425 rc = keydb_search_kid (hd, keyid);
426 if (rc == -1)
428 keydb_release (hd);
429 return G10ERR_NO_PUBKEY;
431 rc = keydb_get_keyblock (hd, &keyblock);
432 keydb_release (hd);
433 if (rc)
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 );
445 else
446 rc=G10ERR_NO_PUBKEY;
448 release_kbnode (keyblock);
450 /* Not caching key here since it won't have all of the fields
451 properly set. */
453 return rc;
457 KBNODE
458 get_pubkeyblock( u32 *keyid )
460 struct getkey_ctx_s ctx;
461 int rc = 0;
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);
468 ctx.nitems = 1;
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;
481 /****************
482 * Get a secret key and store it into sk
485 get_seckey( PKT_secret_key *sk, u32 *keyid )
487 int rc;
488 struct getkey_ctx_s ctx;
489 KBNODE kb = NULL;
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);
495 ctx.nitems = 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 );
502 if ( !rc ) {
503 sk_from_block ( &ctx, sk, kb );
505 get_seckey_end( &ctx );
506 release_kbnode ( kb );
508 if( !rc ) {
509 /* check the secret key (this may prompt for a passprase to
510 * unlock the secret key
512 rc = check_secret_key( sk, 0 );
515 return rc;
519 /****************
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 )
529 int rc;
530 KEYDB_HANDLE hd = keydb_new (1);
532 rc = keydb_search_kid (hd, keyid);
533 if ( rc == -1 )
534 rc = G10ERR_NO_SECKEY;
535 keydb_release (hd);
536 return rc;
540 /****************
541 * Return the type of the user id:
543 * Please use the constants KEYDB_SERCH_MODE_xxx
544 * 0 = Invalid user ID
545 * 1 = exact match
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)
559 * Rules used:
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
572 * email address
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 )
587 const char *s;
588 int hexprefix = 0;
589 int hexlength;
590 int mode = 0;
591 KEYDB_SEARCH_DESC dummy_desc;
593 if (!desc)
594 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++ )
604 switch (*s) {
605 case 0: /* empty string is an error */
606 return 0;
608 #if 0
609 case '.': /* an email address, compare from end */
610 mode = KEYDB_SEARCH_MODE_MAILEND;
611 s++;
612 desc->u.name = s;
613 break;
614 #endif
616 case '<': /* an email address */
617 mode = KEYDB_SEARCH_MODE_MAIL;
618 desc->u.name = s;
619 break;
621 case '@': /* part of an email address */
622 mode = KEYDB_SEARCH_MODE_MAILSUB;
623 s++;
624 desc->u.name = s;
625 break;
627 case '=': /* exact compare */
628 mode = KEYDB_SEARCH_MODE_EXACT;
629 s++;
630 desc->u.name = s;
631 break;
633 case '*': /* case insensitive substring search */
634 mode = KEYDB_SEARCH_MODE_SUBSTR;
635 s++;
636 desc->u.name = s;
637 break;
639 #if 0
640 case '+': /* compare individual words */
641 mode = KEYDB_SEARCH_MODE_WORDS;
642 s++;
643 desc->u.name = s;
644 break;
645 #endif
647 case '#': /* local user id */
648 return 0; /* This is now obsolete and can't not be used anymore*/
650 case ':': /*Unified fingerprint */
652 const char *se, *si;
653 int i;
655 se = strchr( ++s,':');
656 if ( !se )
657 return 0;
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);
666 for ( ; i < 20; i++)
667 desc->u.fpr[i]= 0;
668 s = se + 1;
669 mode = KEYDB_SEARCH_MODE_FPR;
671 break;
673 case '&': /* keygrip */
674 return 0; /* Not yet implememted. */
676 default:
677 if (s[0] == '0' && s[1] == 'x') {
678 hexprefix = 1;
679 s += 2;
682 hexlength = strspn(s, "0123456789abcdefABCDEF");
683 if (hexlength >= 8 && s[hexlength] =='!') {
684 desc->exact = 1;
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. */
696 if (desc->exact)
697 hexlength--;
699 if (hexlength == 8
700 || (!hexprefix && hexlength == 9 && *s == '0')){
701 /* short keyid */
702 if (hexlength == 9)
703 s++;
704 desc->u.kid[0] = 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')) {
710 /* complete keyid */
711 char buf[9];
712 if (hexlength == 17)
713 s++;
714 mem2str(buf, s, 9 );
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
720 && *s == '0')) {
721 /* md5 fingerprint */
722 int i;
723 if (hexlength == 33)
724 s++;
725 memset(desc->u.fpr+16, 0, 4);
726 for (i=0; i < 16; i++, s+=2) {
727 int c = hextobyte(s);
728 if (c == -1)
729 return 0;
730 desc->u.fpr[i] = c;
732 mode = KEYDB_SEARCH_MODE_FPR16;
734 else if (hexlength == 40 || (!hexprefix && hexlength == 41
735 && *s == '0')) {
736 /* sha1/rmd160 fingerprint */
737 int i;
738 if (hexlength == 41)
739 s++;
740 for (i=0; i < 20; i++, s+=2) {
741 int c = hextobyte(s);
742 if (c == -1)
743 return 0;
744 desc->u.fpr[i] = c;
746 mode = KEYDB_SEARCH_MODE_FPR20;
748 else {
749 if (hexprefix) /* This was a hex number with a prefix */
750 return 0; /* and a wrong length */
752 desc->exact = 0;
753 desc->u.name = s;
754 mode = KEYDB_SEARCH_MODE_SUBSTR; /* default mode */
758 desc->mode = mode;
759 return mode;
763 static int
764 skip_unusable(void *dummy,u32 *keyid,PKT_user_id *uid)
766 int unusable=0;
767 KBNODE keyblock;
769 keyblock=get_pubkeyblock(keyid);
770 if(!keyblock)
772 log_error("error checking usability status of %s\n",keystr(keyid));
773 goto leave;
776 /* Is the user ID in question revoked/expired? */
777 if(uid)
779 KBNODE node;
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))
789 unusable=1;
790 break;
796 if(!unusable)
797 unusable=pk_is_disabled(keyblock->pkt->pkt.public_key);
799 leave:
800 release_kbnode(keyblock);
801 return unusable;
804 /****************
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
811 * keyblock there.
814 static int
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 )
820 int rc = 0;
821 int n;
822 strlist_t r;
823 GETKEY_CTX ctx;
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 */
829 *retctx = NULL;
831 if (ret_kdbhd)
832 *ret_kdbhd = NULL;
834 if(!namelist)
836 ctx = xmalloc_clear (sizeof *ctx);
837 ctx->nitems = 1;
838 ctx->items[0].mode=KEYDB_SEARCH_MODE_FIRST;
839 if(!include_unusable)
840 ctx->items[0].skipfnc=skip_unusable;
842 else
844 /* build the search context */
845 for(n=0, r=namelist; r; r = r->next )
846 n++;
848 ctx = xmalloc_clear (sizeof *ctx + (n-1)*sizeof ctx->items );
849 ctx->nitems = n;
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)
856 ctx->exact = 1;
857 if (!ctx->items[n].mode)
859 xfree (ctx);
860 return G10ERR_INV_USER_ID;
862 if(!include_unusable
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);
873 if ( !ret_kb )
874 ret_kb = &help_kb;
876 if( secmode ) {
877 if (sk) {
878 ctx->req_algo = sk->req_algo;
879 ctx->req_usage = sk->req_usage;
881 rc = lookup( ctx, ret_kb, 1 );
882 if ( !rc && sk ) {
883 sk_from_block ( ctx, sk, *ret_kb );
886 else {
887 if (pk) {
888 ctx->req_algo = pk->req_algo;
889 ctx->req_usage = pk->req_usage;
891 rc = lookup( ctx, ret_kb, 0 );
892 if ( !rc && pk ) {
893 pk_from_block ( ctx, pk, *ret_kb );
897 release_kbnode ( help_kb );
899 if (retctx) /* caller wants the context */
900 *retctx = ctx;
901 else {
902 if (ret_kdbhd) {
903 *ret_kdbhd = ctx->kr_handle;
904 ctx->kr_handle = NULL;
906 get_pubkey_end (ctx);
909 return rc;
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
919 mechanism. */
921 get_pubkey_byname (PKT_public_key *pk,
922 const char *name, KBNODE *ret_keyblock,
923 KEYDB_HANDLE *ret_kdbhd, int include_unusable )
925 int rc;
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))
938 struct akl *akl;
940 for(akl=opt.auto_key_locate;akl;akl=akl->next)
942 unsigned char *fpr=NULL;
943 size_t fpr_len;
945 switch(akl->type)
947 case AKL_CERT:
948 glo_ctrl.in_auto_key_retrieve++;
949 rc=keyserver_import_cert(name,&fpr,&fpr_len);
950 glo_ctrl.in_auto_key_retrieve--;
952 if(rc==0)
953 log_info(_("automatically retrieved `%s' via %s\n"),
954 name,"DNS CERT");
955 break;
957 case AKL_PKA:
958 glo_ctrl.in_auto_key_retrieve++;
959 rc=keyserver_import_pka(name,&fpr,&fpr_len);
960 glo_ctrl.in_auto_key_retrieve--;
962 if(rc==0)
963 log_info(_("automatically retrieved `%s' via %s\n"),
964 name,"PKA");
965 break;
967 case AKL_LDAP:
968 glo_ctrl.in_auto_key_retrieve++;
969 rc=keyserver_import_ldap(name,&fpr,&fpr_len);
970 glo_ctrl.in_auto_key_retrieve--;
972 if(rc==0)
973 log_info(_("automatically retrieved `%s' via %s\n"),
974 name,"LDAP");
975 break;
977 case AKL_KEYSERVER:
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. */
982 if(opt.keyserver)
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--;
988 if(rc==0)
989 log_info(_("automatically retrieved `%s' via %s\n"),
990 name,opt.keyserver->uri);
992 break;
994 case AKL_SPEC:
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--;
1003 if(rc==0)
1004 log_info(_("automatically retrieved `%s' via %s\n"),
1005 name,akl->spec->uri);
1007 break;
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. */
1017 if(rc==0 && fpr)
1019 int i;
1020 char fpr_string[MAX_FINGERPRINT_LEN*2+1];
1022 assert(fpr_len<=MAX_FINGERPRINT_LEN);
1024 free_strlist(namelist);
1025 namelist=NULL;
1027 for(i=0;i<fpr_len;i++)
1028 sprintf(fpr_string+2*i,"%02X",fpr[i]);
1030 if(opt.verbose)
1031 log_info("auto-key-locate found fingerprint %s\n",fpr_string);
1033 add_to_strlist( &namelist, fpr_string );
1035 xfree(fpr);
1038 rc = key_byname( NULL, namelist, pk, NULL, 0,
1039 include_unusable, ret_keyblock, ret_kdbhd);
1040 if(rc!=G10ERR_NO_PUBKEY)
1041 break;
1045 free_strlist( namelist );
1046 return rc;
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 )
1059 int rc;
1061 rc = lookup( ctx, ret_keyblock, 0 );
1062 if ( !rc && pk && ret_keyblock )
1063 pk_from_block ( ctx, pk, *ret_keyblock );
1065 return rc;
1068 void
1069 get_pubkey_end( GETKEY_CTX ctx )
1071 if( ctx ) {
1072 memset (&ctx->kbpos, 0, sizeof ctx->kbpos);
1073 keydb_release (ctx->kr_handle);
1074 if( !ctx->not_allocated )
1075 xfree( ctx );
1080 /****************
1081 * Search for a key with the given fingerprint.
1082 * FIXME:
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)
1090 int rc;
1092 if( fprint_len == 20 || fprint_len == 16 ) {
1093 struct getkey_ctx_s ctx;
1094 KBNODE kb = NULL;
1096 memset( &ctx, 0, sizeof ctx );
1097 ctx.exact = 1 ;
1098 ctx.not_allocated = 1;
1099 ctx.kr_handle = keydb_new (0);
1100 ctx.nitems = 1;
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 );
1105 if (!rc && pk )
1106 pk_from_block ( &ctx, pk, kb );
1107 release_kbnode ( kb );
1108 get_pubkey_end( &ctx );
1110 else
1111 rc = G10ERR_GENERAL; /* Oops */
1112 return rc;
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
1120 the key. */
1122 get_pubkey_byfprint_fast (PKT_public_key *pk,
1123 const byte *fprint, size_t fprint_len)
1125 int rc = 0;
1126 KEYDB_HANDLE hd;
1127 KBNODE keyblock;
1128 byte fprbuf[MAX_FINGERPRINT_LEN];
1129 int i;
1131 for (i=0; i < MAX_FINGERPRINT_LEN && i < fprint_len; i++)
1132 fprbuf[i] = fprint[i];
1133 while (i < MAX_FINGERPRINT_LEN)
1134 fprbuf[i++] = 0;
1136 hd = keydb_new (0);
1137 rc = keydb_search_fpr (hd, fprbuf);
1138 if (rc == -1)
1140 keydb_release (hd);
1141 return G10ERR_NO_PUBKEY;
1143 rc = keydb_get_keyblock (hd, &keyblock);
1144 keydb_release (hd);
1145 if (rc)
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 );
1153 if (pk)
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
1158 properly set. */
1160 return 0;
1163 /****************
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,
1169 size_t fprint_len )
1171 int rc;
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);
1179 ctx.nitems = 1;
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 );
1186 else
1187 rc = G10ERR_GENERAL; /* Oops */
1189 return rc;
1193 /****************
1194 * Get a secret key by name and store it into sk
1195 * If NAME is NULL use the default key
1197 static int
1198 get_seckey_byname2( GETKEY_CTX *retctx,
1199 PKT_secret_key *sk, const char *name, int unprotect,
1200 KBNODE *retblock )
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 );
1210 else if(name)
1211 add_to_strlist( &namelist, name );
1212 else
1213 include_unusable=0;
1215 rc = key_byname( retctx, namelist, NULL, sk, 1, include_unusable,
1216 retblock, NULL );
1218 free_strlist( namelist );
1220 if( !rc && unprotect )
1221 rc = check_secret_key( sk, 0 );
1223 return rc;
1226 int
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 )
1244 int rc;
1246 rc = lookup( ctx, ret_keyblock, 1 );
1247 if ( !rc && sk && ret_keyblock )
1248 sk_from_block ( ctx, sk, *ret_keyblock );
1250 return rc;
1254 void
1255 get_seckey_end( GETKEY_CTX ctx )
1257 get_pubkey_end( ctx );
1261 /****************
1262 * Search for a key with the given fingerprint.
1263 * FIXME:
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)
1271 int rc;
1273 if( fprint_len == 20 || fprint_len == 16 ) {
1274 struct getkey_ctx_s ctx;
1275 KBNODE kb = NULL;
1277 memset( &ctx, 0, sizeof ctx );
1278 ctx.exact = 1 ;
1279 ctx.not_allocated = 1;
1280 ctx.kr_handle = keydb_new (1);
1281 ctx.nitems = 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 );
1286 if (!rc && sk )
1287 sk_from_block ( &ctx, sk, kb );
1288 release_kbnode ( kb );
1289 get_seckey_end( &ctx );
1291 else
1292 rc = G10ERR_GENERAL; /* Oops */
1293 return rc;
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,
1301 size_t fprint_len )
1303 int rc;
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);
1312 ctx.nitems = 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);
1320 return rc;
1325 /************************************************
1326 ************* Merging stuff ********************
1327 ************************************************/
1329 /****************
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.
1338 void
1339 merge_keys_and_selfsig( KBNODE keyblock )
1341 PKT_public_key *pk = NULL;
1342 PKT_secret_key *sk = NULL;
1343 PKT_signature *sig;
1344 KBNODE k;
1345 u32 kid[2] = { 0, 0 };
1346 u32 sigdate = 0;
1348 if (keyblock && keyblock->pkt->pkttype == PKT_PUBLIC_KEY ) {
1349 /* divert to our new function */
1350 merge_selfsigs (keyblock);
1351 return;
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 );*/
1367 sigdate = 0;
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 )
1373 sk = NULL;
1374 else if( k->pkt->pkttype == PKT_SECRET_KEY )
1375 keyid_from_sk( sk, kid );
1376 sigdate = 0;
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
1385 * is done above.
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
1390 const byte *p;
1391 u32 ed;
1393 p = parse_sig_subpkt( sig->hashed, SIGSUBPKT_KEY_EXPIRE, NULL );
1394 if( pk ) {
1395 ed = p? pk->timestamp + buffer_to_u32(p):0;
1396 if( sig->timestamp > sigdate ) {
1397 pk->expiredate = ed;
1398 sigdate = sig->timestamp;
1401 else {
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;
1420 static int
1421 parse_key_usage(PKT_signature *sig)
1423 int key_usage=0;
1424 const byte *p;
1425 size_t n;
1426 byte flags;
1428 p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_KEY_FLAGS,&n);
1429 if(p && n)
1431 /* first octet of the keyflags */
1432 flags=*p;
1434 if(flags & 1)
1436 key_usage |= PUBKEY_USAGE_CERT;
1437 flags&=~1;
1440 if(flags & 2)
1442 key_usage |= PUBKEY_USAGE_SIG;
1443 flags&=~2;
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);
1454 if(flags & 0x20)
1456 key_usage |= PUBKEY_USAGE_AUTH;
1457 flags&=~0x20;
1460 if(flags)
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
1468 handle. */
1470 return key_usage;
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
1483 static void
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);
1518 else
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 );
1525 if ( p && *p )
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;
1542 if (uid->prefs)
1543 xfree (uid->prefs);
1544 n = nsym + nhash + nzip;
1545 if (!n)
1546 uid->prefs = NULL;
1547 else {
1548 uid->prefs = xmalloc (sizeof (*uid->prefs) * (n+1));
1549 n = 0;
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 */
1567 uid->flags.mdc = 0;
1568 p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_FEATURES, &n);
1569 if (p && n && (p[0] & 0x01))
1570 uid->flags.mdc = 1;
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;
1579 static void
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];
1588 static void
1589 merge_selfsigs_main(KBNODE keyblock, int *r_revoked, struct revoke_info *rinfo)
1591 PKT_public_key *pk = NULL;
1592 KBNODE k;
1593 u32 kid[2];
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;
1599 u32 key_expire = 0;
1600 int key_expire_seen = 0;
1601 byte sigversion = 0;
1603 *r_revoked = 0;
1604 memset(rinfo,0,sizeof(*rinfo));
1606 if ( keyblock->pkt->pkttype != PKT_PUBLIC_KEY )
1607 BUG ();
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 */
1628 xfree(pk->revkey);
1629 pk->revkey=NULL;
1630 pk->numrevkeys=0;
1632 signode = NULL;
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
1648 * that key.
1650 *r_revoked = 1;
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). */
1661 if(sig->revkey) {
1662 int i;
1664 pk->revkey=
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++],
1670 sig->revkey[i],
1671 sizeof(struct revocation_key));
1674 if( sig->timestamp >= sigdate ) {
1675 if(sig->flags.expired)
1676 ; /* signature has expired - ignore it */
1677 else {
1678 sigdate = sig->timestamp;
1679 signode = k;
1680 if( sig->version > sigversion )
1681 sigversion = sig->version;
1690 /* Remove dupes from the revocation keys */
1692 if(pk->revkey)
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)
1703 /* remove j */
1705 for(x=j;x<pk->numrevkeys-1;x++)
1706 pk->revkey[x]=pk->revkey[x+1];
1708 pk->numrevkeys--;
1709 j--;
1710 changed=1;
1715 if(changed)
1716 pk->revkey=xrealloc(pk->revkey,
1717 pk->numrevkeys*sizeof(struct revocation_key));
1720 if ( signode )
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;
1726 const byte *p;
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 */
1739 pk->is_valid = 1;
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);
1758 if(rc==0)
1760 *r_revoked=2;
1761 sig_to_revoke_info(sig,rinfo);
1762 /* don't continue checking since we can't be any
1763 more revoked than this */
1764 break;
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 );
1789 pk->is_valid=1;
1791 uidnode = k;
1792 signode = NULL;
1793 sigdate = 0;
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;
1813 signode = k;
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 );
1823 pk->is_valid = 1;
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)
1830 if(opt.verbose)
1831 log_info(_("Invalid key %s made valid by"
1832 " --allow-non-selfsigned-uid\n"),keystr_from_pk(pk));
1833 pk->is_valid = 1;
1836 /* The key STILL isn't valid, so try and find an ultimately
1837 trusted signature. */
1838 if(!pk->is_valid)
1840 uidnode=NULL;
1842 for(k=keyblock; k && k->pkt->pkttype != PKT_PUBLIC_SUBKEY; k=k->next)
1844 if ( k->pkt->pkttype == PKT_USER_ID )
1845 uidnode = k;
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);
1868 pk->is_valid=1;
1869 break;
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.
1893 if ( !key_usage ) {
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;
1897 k = k->next ) {
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 */
1913 key_usage &= x;
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 */
1923 uiddate = 0;
1924 for(k=keyblock; k && k->pkt->pkttype != PKT_PUBLIC_SUBKEY;
1925 k = k->next ) {
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;
1959 uidnode = k;
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)
1970 uidnode=k;
1973 else
1975 if(uid->created > uiddate2)
1977 uiddate2 = uid->created;
1978 uidnode2 = k;
1980 else if(uid->created==uiddate2 && uidnode2)
1982 if(cmp_user_ids(uid,uidnode2->pkt->pkt.user_id)>0)
1983 uidnode2=k;
1988 if ( uidnode ) {
1989 for(k=keyblock; k && k->pkt->pkttype != PKT_PUBLIC_SUBKEY;
1990 k = k->next ) {
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;
1994 if ( k != uidnode )
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;
2004 else
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. */
2010 uidnode = NULL;
2012 for(k=keyblock; k && k->pkt->pkttype != PKT_PUBLIC_SUBKEY;
2013 k = k->next )
2015 if(k->pkt->pkttype==PKT_USER_ID
2016 && !k->pkt->pkt.user_id->attrib_data)
2018 if(!uidnode)
2020 uidnode=k;
2021 uidnode->pkt->pkt.user_id->is_primary=1;
2022 continue;
2024 else
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;
2030 uidnode=k;
2031 uidnode->pkt->pkt.user_id->is_primary=1;
2033 else
2034 k->pkt->pkt.user_id->is_primary=0; /* just to be
2035 safe */
2043 static void
2044 merge_selfsigs_subkey( KBNODE keyblock, KBNODE subnode )
2046 PKT_public_key *mainpk = NULL, *subpk = NULL;
2047 PKT_signature *sig;
2048 KBNODE k;
2049 u32 mainkid[2];
2050 u32 sigdate = 0;
2051 KBNODE signode;
2052 u32 curtime = make_timestamp ();
2053 unsigned int key_usage = 0;
2054 u32 keytimestamp = 0;
2055 u32 key_expire = 0;
2056 const byte *p;
2058 if ( subnode->pkt->pkttype != PKT_PUBLIC_SUBKEY )
2059 BUG ();
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. */
2072 signode = NULL;
2073 sigdate = 0; /* helper to find the latest signature */
2074 for(k=subnode->next; k && k->pkt->pkttype != PKT_PUBLIC_SUBKEY;
2075 k = k->next ) {
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
2094 * time */
2096 else if ( IS_SUBKEY_SIG (sig) && sig->timestamp >= sigdate )
2098 if(sig->flags.expired)
2099 ; /* signature has expired - ignore it */
2100 else
2102 sigdate = sig->timestamp;
2103 signode = k;
2104 signode->pkt->pkt.signature->flags.chosen_selfsig=0;
2111 /* no valid key binding */
2112 if ( !signode )
2113 return;
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);
2119 if ( !key_usage )
2121 /* no key flags at all: get it from the algo */
2122 key_usage = openpgp_pk_algo_usage ( subpk->pubkey_algo );
2124 else
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 */
2129 key_usage &= x;
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);
2137 else
2138 key_expire = 0;
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))
2144 return;
2146 subpk->is_valid = 1;
2148 /* Find the first 0x19 embedded signature on our self-sig. */
2149 if(subpk->backsig==0)
2151 int seq=0;
2152 size_t n;
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)))
2159 break;
2161 if(p==NULL)
2163 seq=0;
2164 /* It is safe to have this in the unhashed area since the
2165 0x19 is located on the selfsig for convenience, not
2166 security. */
2167 while((p=enum_sig_subpkt(sig->unhashed,SIGSUBPKT_SIGNATURE,
2168 &n,&seq,NULL)))
2169 if(n>3 && ((p[0]==3 && p[2]==0x19) || (p[0]==4 && p[1]==0x19)))
2170 break;
2173 if(p)
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)
2181 subpk->backsig=2;
2182 else
2183 subpk->backsig=1;
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:
2203 * FIXME the docs
2205 static void
2206 merge_selfsigs( KBNODE keyblock )
2208 KBNODE k;
2209 int revoked;
2210 struct revoke_info rinfo;
2211 PKT_public_key *main_pk;
2212 prefitem_t *prefs;
2213 int mdc_feature;
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 "
2218 "- must stop\n");
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 */
2222 g10_exit (1);
2224 BUG ();
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
2240 * subkeys */
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)
2246 pk->is_valid = 0;
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;
2256 return;
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
2265 * all preferences.
2266 * Do a similar thing for the MDC feature flag.
2268 prefs = NULL;
2269 mdc_feature = 0;
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;
2276 break;
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;
2283 if (pk->prefs)
2284 xfree (pk->prefs);
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
2297 * from the key.
2299 static void
2300 merge_public_with_secret ( KBNODE pubblock, KBNODE secblock )
2302 KBNODE pub;
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 ) {
2320 KBNODE sec;
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);
2334 break;
2338 if ( !sec )
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.
2350 static void
2351 premerge_public_with_secret ( KBNODE pubblock, KBNODE secblock )
2353 KBNODE last, pub;
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 ) {
2361 KBNODE sec;
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);
2377 break;
2381 if ( !sec ) {
2382 KBNODE next, ll;
2384 if (opt.verbose)
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 */
2389 assert ( last );
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 )
2395 /* make new link */
2396 last->next = next;
2397 /* release this public subkey with all sigs */
2398 ll->next = NULL;
2399 release_kbnode( pub );
2400 /* let the loop continue */
2401 pub = last;
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
2431 * FXME
2432 * 4. Usage but no primary key requested
2433 * FIXME
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.
2443 static int
2444 finish_lookup (GETKEY_CTX ctx)
2446 KBNODE keyblock = ctx->keyblock;
2447 KBNODE k;
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
2455 does. */
2456 int req_prim = (ctx->req_usage & PUBKEY_USAGE_CERT) ||
2457 ((PGP6 || PGP7) && (ctx->req_usage & PUBKEY_USAGE_SIG));
2458 u32 latest_date;
2459 KBNODE latest_key;
2460 u32 curtime = make_timestamp ();
2462 assert( keyblock->pkt->pkttype == PKT_PUBLIC_KEY );
2464 ctx->found_key = NULL;
2466 if (ctx->exact) {
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 );
2471 foundk = k;
2472 break;
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;
2481 break;
2485 if ( DBG_CACHE )
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);
2490 if (!req_usage) {
2491 latest_key = foundk? foundk:keyblock;
2492 goto found;
2495 if (!req_usage) {
2496 PKT_public_key *pk = foundk->pkt->pkt.public_key;
2497 if (pk->user_id)
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 */
2505 latest_date = 0;
2506 latest_key = NULL;
2507 /* do not look at subkeys if a certification key is requested */
2508 if ((!foundk || foundk->pkt->pkttype == PKT_PUBLIC_SUBKEY) && !req_prim) {
2509 KBNODE nextk;
2510 /* either start a loop or check just this one subkey */
2511 for (k=foundk?foundk:keyblock; k; k = nextk ) {
2512 PKT_public_key *pk;
2513 nextk = k->next;
2514 if ( k->pkt->pkttype != PKT_PUBLIC_SUBKEY )
2515 continue;
2516 if ( foundk )
2517 nextk = NULL; /* what a hack */
2518 pk = k->pkt->pkt.public_key;
2519 if (DBG_CACHE)
2520 log_debug( "\tchecking subkey %08lX\n",
2521 (ulong)keyid_from_pk( pk, NULL));
2522 if ( !pk->is_valid ) {
2523 if (DBG_CACHE)
2524 log_debug( "\tsubkey not valid\n");
2525 continue;
2527 if ( pk->is_revoked ) {
2528 if (DBG_CACHE)
2529 log_debug( "\tsubkey has been revoked\n");
2530 continue;
2532 if ( pk->has_expired ) {
2533 if (DBG_CACHE)
2534 log_debug( "\tsubkey has expired\n");
2535 continue;
2537 if ( pk->timestamp > curtime && !opt.ignore_valid_from ) {
2538 if (DBG_CACHE)
2539 log_debug( "\tsubkey not yet valid\n");
2540 continue;
2543 if ( !((pk->pubkey_usage&USAGE_MASK) & req_usage) ) {
2544 if (DBG_CACHE)
2545 log_debug( "\tusage does not match: want=%x have=%x\n",
2546 req_usage, pk->pubkey_usage );
2547 continue;
2550 if (DBG_CACHE)
2551 log_debug( "\tsubkey looks fine\n");
2552 if ( pk->timestamp > latest_date ) {
2553 latest_date = pk->timestamp;
2554 latest_key = k;
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) {
2562 PKT_public_key *pk;
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 ) {
2567 if (DBG_CACHE)
2568 log_debug( "\tprimary key not valid\n");
2570 else if ( pk->is_revoked ) {
2571 if (DBG_CACHE)
2572 log_debug( "\tprimary key has been revoked\n");
2574 else if ( pk->has_expired ) {
2575 if (DBG_CACHE)
2576 log_debug( "\tprimary key has expired\n");
2578 else if ( !((pk->pubkey_usage&USAGE_MASK) & req_usage) ) {
2579 if (DBG_CACHE)
2580 log_debug( "\tprimary key usage does not match: "
2581 "want=%x have=%x\n",
2582 req_usage, pk->pubkey_usage );
2584 else { /* okay */
2585 if (DBG_CACHE)
2586 log_debug( "\tprimary key may be used\n");
2587 latest_key = keyblock;
2588 latest_date = pk->timestamp;
2592 if ( !latest_key ) {
2593 if (DBG_CACHE)
2594 log_debug("\tno suitable key found - giving up\n");
2595 return 0;
2598 found:
2599 if (DBG_CACHE)
2600 log_debug( "\tusing key %08lX\n",
2601 (ulong)keyid_from_pk( latest_key->pkt->pkt.public_key, NULL) );
2603 if (latest_key) {
2604 PKT_public_key *pk = latest_key->pkt->pkt.public_key;
2605 if (pk->user_id)
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)
2614 char *tempkeystr=
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));
2618 xfree(tempkeystr);
2621 cache_user_id( keyblock );
2623 return 1; /* found */
2627 static int
2628 lookup( GETKEY_CTX ctx, KBNODE *ret_keyblock, int secmode )
2630 int rc;
2631 KBNODE secblock = NULL; /* helper */
2632 int no_suitable_key = 0;
2634 rc = 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);
2643 if (rc) {
2644 log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc));
2645 rc = 0;
2646 goto skip;
2649 if ( secmode ) {
2650 /* find the correspondig public key and use this
2651 * this one for the selection process */
2652 u32 aki[2];
2653 KBNODE k = ctx->keyblock;
2655 if (k->pkt->pkttype != PKT_SECRET_KEY)
2656 BUG();
2658 keyid_from_sk (k->pkt->pkt.secret_key, aki);
2659 k = get_pubkeyblock (aki);
2660 if( !k )
2662 if (!opt.quiet)
2663 log_info(_("key %s: secret key without public key"
2664 " - skipped\n"), keystr(aki));
2665 goto skip;
2667 secblock = ctx->keyblock;
2668 ctx->keyblock = k;
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;
2679 if ( secmode ) {
2680 merge_public_with_secret ( ctx->keyblock,
2681 secblock);
2682 release_kbnode (secblock);
2683 secblock = NULL;
2685 goto found;
2687 else
2688 no_suitable_key = 1;
2690 skip:
2691 /* release resources and continue search */
2692 if ( secmode ) {
2693 release_kbnode( secblock );
2694 secblock = NULL;
2696 release_kbnode( ctx->keyblock );
2697 ctx->keyblock = NULL;
2700 found:
2701 if( rc && rc != -1 )
2702 log_error("keydb_search failed: %s\n", g10_errstr(rc));
2704 if( !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;
2710 else if( rc == -1 )
2711 rc = secmode ? G10ERR_NO_SECKEY : G10ERR_NO_PUBKEY;
2713 if ( secmode ) {
2714 release_kbnode( secblock );
2715 secblock = NULL;
2717 release_kbnode( ctx->keyblock );
2718 ctx->keyblock = NULL;
2720 ctx->last_rc = rc;
2721 return rc;
2727 /****************
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
2742 * to indicate EOF.
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 )
2750 int rc=0;
2751 struct {
2752 int eof;
2753 int first;
2754 KEYDB_HANDLE hd;
2755 KBNODE keyblock;
2756 KBNODE node;
2757 } *c = *context;
2760 if( !c ) { /* make a new context */
2761 c = xmalloc_clear( sizeof *c );
2762 *context = c;
2763 c->hd = keydb_new (1);
2764 c->first = 1;
2765 c->keyblock = NULL;
2766 c->node = NULL;
2769 if( !sk ) { /* free the context */
2770 keydb_release (c->hd);
2771 release_kbnode (c->keyblock);
2772 xfree( c );
2773 *context = NULL;
2774 return 0;
2777 if( c->eof )
2778 return -1;
2780 do {
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
2784 || (with_subkeys
2785 && c->node->pkt->pkttype == PKT_SECRET_SUBKEY) )
2786 && !(c->node->pkt->pkt.secret_key->protect.s2k.mode==1001
2787 && !with_spm)) {
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);
2797 c->first = 0;
2798 if (rc) {
2799 keydb_release (c->hd); c->hd = NULL;
2800 c->eof = 1;
2801 return -1; /* eof */
2804 rc = keydb_get_keyblock (c->hd, &c->keyblock);
2805 c->node = c->keyblock;
2806 } while (!rc);
2808 return rc; /* error */
2813 /*********************************************
2814 *********** user ID printing helpers *******
2815 *********************************************/
2817 /****************
2818 * Return a string with a printable representation of the user_id.
2819 * this string must be freed by xfree.
2821 char*
2822 get_user_id_string( u32 *keyid )
2824 user_id_db_t r;
2825 char *p;
2826 int pass=0;
2827 /* try it two times; second pass reads from key resources */
2830 for(r=user_id_db; r; r = r->next )
2832 keyid_list_t a;
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 );
2839 return p;
2843 } while( ++pass < 2 && !get_pubkey( NULL, keyid ) );
2844 p = xmalloc( keystrlen() + 5 );
2845 sprintf(p, "%s [?]", keystr(keyid));
2846 return p;
2850 char*
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 );
2855 xfree(p);
2856 return p2;
2860 char*
2861 get_long_user_id_string( u32 *keyid )
2863 user_id_db_t r;
2864 char *p;
2865 int pass=0;
2866 /* try it two times; second pass reads from key resources */
2867 do {
2868 for(r=user_id_db; r; r = r->next ) {
2869 keyid_list_t a;
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],
2875 r->len, r->name );
2876 return p;
2880 } while( ++pass < 2 && !get_pubkey( NULL, keyid ) );
2881 p = xmalloc( 25 );
2882 sprintf(p, "%08lX%08lX [?]", (ulong)keyid[0], (ulong)keyid[1] );
2883 return p;
2886 char*
2887 get_user_id( u32 *keyid, size_t *rn )
2889 user_id_db_t r;
2890 char *p;
2891 int pass=0;
2893 /* try it two times; second pass reads from key resources */
2894 do {
2895 for(r=user_id_db; r; r = r->next ) {
2896 keyid_list_t a;
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 );
2901 *rn = r->len;
2902 return p;
2906 } while( ++pass < 2 && !get_pubkey( NULL, keyid ) );
2907 p = xstrdup( user_id_not_found_utf8 () );
2908 *rn = strlen(p);
2909 return p;
2912 char*
2913 get_user_id_native( u32 *keyid )
2915 size_t rn;
2916 char *p = get_user_id( keyid, &rn );
2917 char *p2 = utf8_to_native( p, rn, 0 );
2918 xfree(p);
2919 return p2;
2922 KEYDB_HANDLE
2923 get_ctx_handle(GETKEY_CTX ctx)
2925 return ctx->kr_handle;
2928 static void
2929 free_akl(struct akl *akl)
2931 if(akl->spec)
2932 free_keyserver_spec(akl->spec);
2934 xfree(akl);
2937 void
2938 release_akl(void)
2940 while(opt.auto_key_locate)
2942 struct akl *akl2=opt.auto_key_locate;
2943 opt.auto_key_locate=opt.auto_key_locate->next;
2944 free_akl(akl2);
2949 parse_auto_key_locate(char *options)
2951 char *tok;
2953 while((tok=optsep(&options)))
2955 struct akl *akl,*check,*last=NULL;
2956 int dupe=0;
2958 if(tok[0]=='\0')
2959 continue;
2961 akl=xmalloc_clear(sizeof(*akl));
2963 if(ascii_strcasecmp(tok,"ldap")==0)
2964 akl->type=AKL_LDAP;
2965 else if(ascii_strcasecmp(tok,"keyserver")==0)
2966 akl->type=AKL_KEYSERVER;
2967 #ifdef USE_DNS_CERT
2968 else if(ascii_strcasecmp(tok,"cert")==0)
2969 akl->type=AKL_CERT;
2970 #endif
2971 #ifdef USE_DNS_PKA
2972 else if(ascii_strcasecmp(tok,"pka")==0)
2973 akl->type=AKL_PKA;
2974 #endif
2975 else if((akl->spec=parse_keyserver_uri(tok,1,NULL,0)))
2976 akl->type=AKL_SPEC;
2977 else
2979 free_akl(akl);
2980 return 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)))
2992 dupe=1;
2993 free_akl(akl);
2994 break;
2998 if(!dupe)
3000 if(last)
3001 last->next=akl;
3002 else
3003 opt.auto_key_locate=akl;
3007 return 1;