* exec.c (make_tempdir) [_WIN32]: Modified to properly handle
[gnupg.git] / g10 / pkclist.c
blob7f3285bf940fd27a89b73a15141df031cd0ec451
1 /* pkclist.c
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 <errno.h>
28 #include <assert.h>
30 #include "options.h"
31 #include "packet.h"
32 #include "errors.h"
33 #include "keydb.h"
34 #include "memory.h"
35 #include "util.h"
36 #include "main.h"
37 #include "trustdb.h"
38 #include "ttyio.h"
39 #include "status.h"
40 #include "photoid.h"
41 #include "i18n.h"
43 #define CONTROL_D ('D' - 'A' + 1)
45 /****************
46 * Show the revocation reason as it is stored with the given signature
48 static void
49 do_show_revocation_reason( PKT_signature *sig )
51 size_t n, nn;
52 const byte *p, *pp;
53 int seq = 0;
54 const char *text;
56 while( (p = enum_sig_subpkt (sig->hashed, SIGSUBPKT_REVOC_REASON,
57 &n, &seq, NULL )) ) {
58 if( !n )
59 continue; /* invalid - just skip it */
61 if( *p == 0 )
62 text = _("No reason specified");
63 else if( *p == 0x01 )
64 text = _("Key is superseded");
65 else if( *p == 0x02 )
66 text = _("Key has been compromised");
67 else if( *p == 0x03 )
68 text = _("Key is no longer used");
69 else if( *p == 0x20 )
70 text = _("User ID is no longer valid");
71 else
72 text = NULL;
74 log_info( _("reason for revocation: ") );
75 if( text )
76 fputs( text, log_stream() );
77 else
78 fprintf( log_stream(), "code=%02x", *p );
79 putc( '\n', log_stream() );
80 n--; p++;
81 pp = NULL;
82 do {
83 /* We don't want any empty lines, so skip them */
84 while( n && *p == '\n' ) {
85 p++;
86 n--;
88 if( n ) {
89 pp = memchr( p, '\n', n );
90 nn = pp? pp - p : n;
91 log_info( _("revocation comment: ") );
92 print_string( log_stream(), p, nn, 0 );
93 putc( '\n', log_stream() );
94 p += nn; n -= nn;
96 } while( pp );
100 /* Mode 0: try and find the revocation based on the pk (i.e. check
101 subkeys, etc.) Mode 1: use only the revocation on the main pk */
103 void
104 show_revocation_reason( PKT_public_key *pk, int mode )
106 /* Hmmm, this is not so easy becuase we have to duplicate the code
107 * used in the trustbd to calculate the keyflags. We need to find
108 * a clean way to check revocation certificates on keys and
109 * signatures. And there should be no duplicate code. Because we
110 * enter this function only when the trustdb told us that we have
111 * a revoked key, we could simply look for a revocation cert and
112 * display this one, when there is only one. Let's try to do this
113 * until we have a better solution. */
114 KBNODE node, keyblock = NULL;
115 byte fingerprint[MAX_FINGERPRINT_LEN];
116 size_t fingerlen;
117 int rc;
119 /* get the keyblock */
120 fingerprint_from_pk( pk, fingerprint, &fingerlen );
121 rc = get_keyblock_byfprint( &keyblock, fingerprint, fingerlen );
122 if( rc ) { /* that should never happen */
123 log_debug( "failed to get the keyblock\n");
124 return;
127 for( node=keyblock; node; node = node->next ) {
128 if( (mode && node->pkt->pkttype == PKT_PUBLIC_KEY) ||
129 ( ( node->pkt->pkttype == PKT_PUBLIC_KEY
130 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
131 && !cmp_public_keys( node->pkt->pkt.public_key, pk ) ) )
132 break;
134 if( !node ) {
135 log_debug("Oops, PK not in keyblock\n");
136 release_kbnode( keyblock );
137 return;
139 /* now find the revocation certificate */
140 for( node = node->next; node ; node = node->next ) {
141 if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
142 break;
143 if( node->pkt->pkttype == PKT_SIGNATURE
144 && (node->pkt->pkt.signature->sig_class == 0x20
145 || node->pkt->pkt.signature->sig_class == 0x28 ) ) {
146 /* FIXME: we should check the signature here */
147 do_show_revocation_reason ( node->pkt->pkt.signature );
148 break;
152 /* We didn't find it, so check if the whole key is revoked */
153 if(!node && !mode)
154 show_revocation_reason(pk,1);
156 release_kbnode( keyblock );
160 /****************
161 * mode: 0 = standard
162 * 1 = Without key info and additional menu option 'm'
163 * this does also add an option to set the key to ultimately trusted.
164 * Returns:
165 * -2 = nothing changed - caller should show some additional info
166 * -1 = quit operation
167 * 0 = nothing changed
168 * 1 = new ownertrust now in new_trust
170 static int
171 do_edit_ownertrust (PKT_public_key *pk, int mode,
172 unsigned *new_trust, int defer_help )
174 char *p;
175 u32 keyid[2];
176 int changed=0;
177 int quit=0;
178 int show=0;
179 int min_num;
180 int did_help=defer_help;
181 unsigned int minimum=get_min_ownertrust(pk);
183 switch(minimum)
185 default:
186 case TRUST_UNDEFINED: min_num=1; break;
187 case TRUST_NEVER: min_num=2; break;
188 case TRUST_MARGINAL: min_num=3; break;
189 case TRUST_FULLY: min_num=4; break;
192 keyid_from_pk (pk, keyid);
193 for(;;) {
194 /* A string with valid answers.
196 Note to translators: These are the allowed answers in lower and
197 uppercase. Below you will find the matching strings which
198 should be translated accordingly and the letter changed to
199 match the one in the answer string.
201 i = please show me more information
202 m = back to the main menu
203 s = skip this key
204 q = quit
206 const char *ans = _("iImMqQsS");
208 if( !did_help )
210 if( !mode )
212 KBNODE keyblock, un;
214 tty_printf(_("No trust value assigned to:\n"));
215 tty_printf("%4u%c/%s %s\n",nbits_from_pk( pk ),
216 pubkey_letter( pk->pubkey_algo ),
217 keystr(keyid), datestr_from_pk( pk ) );
218 p=get_user_id_native(keyid);
219 tty_printf(_(" \"%s\"\n"),p);
220 xfree(p);
222 keyblock = get_pubkeyblock (keyid);
223 if (!keyblock)
224 BUG ();
225 for (un=keyblock; un; un = un->next)
227 if (un->pkt->pkttype != PKT_USER_ID )
228 continue;
229 if (un->pkt->pkt.user_id->is_revoked )
230 continue;
231 if (un->pkt->pkt.user_id->is_expired )
232 continue;
233 /* Only skip textual primaries */
234 if (un->pkt->pkt.user_id->is_primary
235 && !un->pkt->pkt.user_id->attrib_data )
236 continue;
238 if((opt.verify_options&VERIFY_SHOW_PHOTOS)
239 && un->pkt->pkt.user_id->attrib_data)
240 show_photos(un->pkt->pkt.user_id->attribs,
241 un->pkt->pkt.user_id->numattribs,pk,NULL);
243 p=utf8_to_native(un->pkt->pkt.user_id->name,
244 un->pkt->pkt.user_id->len,0);
246 tty_printf(_(" aka \"%s\"\n"),p);
249 print_fingerprint (pk, NULL, 2);
250 tty_printf("\n");
251 release_kbnode (keyblock);
254 if(opt.trust_model==TM_DIRECT)
256 tty_printf(_("How much do you trust that this key actually "
257 "belongs to the named user?\n"));
258 tty_printf("\n");
260 else
262 /* This string also used in keyedit.c:trustsig_prompt */
263 tty_printf(_("Please decide how far you trust this user to"
264 " correctly verify other users' keys\n"
265 "(by looking at passports, checking fingerprints from"
266 " different sources, etc.)\n"));
267 tty_printf("\n");
270 if(min_num<=1)
271 tty_printf (_(" %d = I don't know or won't say\n"), 1);
272 if(min_num<=2)
273 tty_printf (_(" %d = I do NOT trust\n"), 2);
274 if(min_num<=3)
275 tty_printf (_(" %d = I trust marginally\n"), 3);
276 if(min_num<=4)
277 tty_printf (_(" %d = I trust fully\n"), 4);
278 if (mode)
279 tty_printf (_(" %d = I trust ultimately\n"), 5);
280 #if 0
281 /* not yet implemented */
282 tty_printf (" i = please show me more information\n");
283 #endif
284 if( mode )
285 tty_printf(_(" m = back to the main menu\n"));
286 else
288 tty_printf(_(" s = skip this key\n"));
289 tty_printf(_(" q = quit\n"));
291 tty_printf("\n");
292 if(minimum)
293 tty_printf(_("The minimum trust level for this key is: %s\n\n"),
294 trust_value_to_string(minimum));
295 did_help = 1;
297 if( strlen(ans) != 8 )
298 BUG();
299 p = cpr_get("edit_ownertrust.value",_("Your decision? "));
300 trim_spaces(p);
301 cpr_kill_prompt();
302 if( !*p )
303 did_help = 0;
304 else if( *p && p[1] )
306 else if( !p[1] && ((*p >= '0'+min_num) && *p <= (mode?'5':'4')) )
308 unsigned int trust;
309 switch( *p )
311 case '1': trust = TRUST_UNDEFINED; break;
312 case '2': trust = TRUST_NEVER ; break;
313 case '3': trust = TRUST_MARGINAL ; break;
314 case '4': trust = TRUST_FULLY ; break;
315 case '5': trust = TRUST_ULTIMATE ; break;
316 default: BUG();
318 if (trust == TRUST_ULTIMATE
319 && !cpr_get_answer_is_yes ("edit_ownertrust.set_ultimate.okay",
320 _("Do you really want to set this key"
321 " to ultimate trust? (y/N) ")))
322 ; /* no */
323 else
325 *new_trust = trust;
326 changed = 1;
327 break;
330 #if 0
331 /* not yet implemented */
332 else if( *p == ans[0] || *p == ans[1] )
334 tty_printf(_("Certificates leading to an ultimately trusted key:\n"));
335 show = 1;
336 break;
338 #endif
339 else if( mode && (*p == ans[2] || *p == ans[3] || *p == CONTROL_D ) )
341 break ; /* back to the menu */
343 else if( !mode && (*p == ans[6] || *p == ans[7] ) )
345 break; /* skip */
347 else if( !mode && (*p == ans[4] || *p == ans[5] ) )
349 quit = 1;
350 break ; /* back to the menu */
352 xfree(p); p = NULL;
354 xfree(p);
355 return show? -2: quit? -1 : changed;
359 * Display a menu to change the ownertrust of the key PK (which should
360 * be a primary key).
361 * For mode values see do_edit_ownertrust ()
364 edit_ownertrust (PKT_public_key *pk, int mode )
366 unsigned int trust;
367 int no_help = 0;
369 for(;;)
371 switch ( do_edit_ownertrust (pk, mode, &trust, no_help ) )
373 case -1: /* quit */
374 return -1;
375 case -2: /* show info */
376 no_help = 1;
377 break;
378 case 1: /* trust value set */
379 trust &= ~TRUST_FLAG_DISABLED;
380 trust |= get_ownertrust (pk) & TRUST_FLAG_DISABLED;
381 update_ownertrust (pk, trust );
382 return 1;
383 default:
384 return 0;
390 /****************
391 * Check whether we can trust this pk which has a trustlevel of TRUSTLEVEL
392 * Returns: true if we trust.
394 static int
395 do_we_trust( PKT_public_key *pk, unsigned int trustlevel )
397 /* We should not be able to get here with a revoked or expired
398 key */
399 if(trustlevel & TRUST_FLAG_REVOKED
400 || trustlevel & TRUST_FLAG_SUB_REVOKED
401 || (trustlevel & TRUST_MASK) == TRUST_EXPIRED)
402 BUG();
404 if( opt.trust_model==TM_ALWAYS )
406 if( opt.verbose )
407 log_info("No trust check due to `--trust-model always' option\n");
408 return 1;
411 switch(trustlevel & TRUST_MASK)
413 default:
414 log_error ("invalid trustlevel %u returned from validation layer\n",
415 trustlevel);
416 /* fall thru */
417 case TRUST_UNKNOWN:
418 case TRUST_UNDEFINED:
419 log_info(_("%s: There is no assurance this key belongs"
420 " to the named user\n"),keystr_from_pk(pk));
421 return 0; /* no */
423 case TRUST_MARGINAL:
424 log_info(_("%s: There is limited assurance this key belongs"
425 " to the named user\n"),keystr_from_pk(pk));
426 return 1; /* yes */
428 case TRUST_FULLY:
429 if( opt.verbose )
430 log_info(_("This key probably belongs to the named user\n"));
431 return 1; /* yes */
433 case TRUST_ULTIMATE:
434 if( opt.verbose )
435 log_info(_("This key belongs to us\n"));
436 return 1; /* yes */
439 return 1; /*NOTREACHED*/
443 /****************
444 * wrapper around do_we_trust, so we can ask whether to use the
445 * key anyway.
447 static int
448 do_we_trust_pre( PKT_public_key *pk, unsigned int trustlevel )
450 int rc;
452 rc = do_we_trust( pk, trustlevel );
454 if( !opt.batch && !rc )
456 print_pubkey_info(NULL,pk);
457 print_fingerprint (pk, NULL, 2);
458 tty_printf("\n");
460 tty_printf(
461 _("It is NOT certain that the key belongs to the person named\n"
462 "in the user ID. If you *really* know what you are doing,\n"
463 "you may answer the next question with yes.\n"));
465 tty_printf("\n");
467 if( cpr_get_answer_is_yes("untrusted_key.override",
468 _("Use this key anyway? (y/N) ")) )
469 rc = 1;
471 /* Hmmm: Should we set a flag to tell the user about
472 * his decision the next time he encrypts for this recipient?
476 return rc;
480 /****************
481 * Check whether we can trust this signature.
482 * Returns: Error if we shall not trust this signatures.
485 check_signatures_trust( PKT_signature *sig )
487 PKT_public_key *pk = xmalloc_clear( sizeof *pk );
488 unsigned int trustlevel;
489 int rc=0;
491 rc = get_pubkey( pk, sig->keyid );
492 if (rc)
493 { /* this should not happen */
494 log_error("Ooops; the key vanished - can't check the trust\n");
495 rc = G10ERR_NO_PUBKEY;
496 goto leave;
499 if ( opt.trust_model==TM_ALWAYS )
501 if( !opt.quiet )
502 log_info(_("WARNING: Using untrusted key!\n"));
503 if (opt.with_fingerprint)
504 print_fingerprint (pk, NULL, 1);
505 goto leave;
508 if(pk->maybe_revoked && !pk->is_revoked)
509 log_info(_("WARNING: this key might be revoked (revocation key"
510 " not present)\n"));
512 trustlevel = get_validity (pk, NULL);
514 if ( (trustlevel & TRUST_FLAG_REVOKED) )
516 write_status( STATUS_KEYREVOKED );
517 if(pk->is_revoked==2)
518 log_info(_("WARNING: This key has been revoked by its"
519 " designated revoker!\n"));
520 else
521 log_info(_("WARNING: This key has been revoked by its owner!\n"));
522 log_info(_(" This could mean that the signature is forged.\n"));
523 show_revocation_reason( pk, 0 );
525 else if ((trustlevel & TRUST_FLAG_SUB_REVOKED) )
527 write_status( STATUS_KEYREVOKED );
528 log_info(_("WARNING: This subkey has been revoked by its owner!\n"));
529 show_revocation_reason( pk, 0 );
532 if ((trustlevel & TRUST_FLAG_DISABLED))
533 log_info (_("Note: This key has been disabled.\n"));
535 /* If we have PKA information adjust the trustlevel. */
536 if (sig->pka_info && sig->pka_info->valid)
538 unsigned char fpr[MAX_FINGERPRINT_LEN];
539 PKT_public_key *primary_pk;
540 size_t fprlen;
541 int okay;
544 primary_pk = xmalloc_clear (sizeof *primary_pk);
545 get_pubkey (primary_pk, pk->main_keyid);
546 fingerprint_from_pk (primary_pk, fpr, &fprlen);
547 free_public_key (primary_pk);
549 if ( fprlen == 20 && !memcmp (sig->pka_info->fpr, fpr, 20) )
551 okay = 1;
552 write_status_text (STATUS_PKA_TRUST_GOOD, sig->pka_info->email);
553 log_info (_("Note: Verified signer's address is `%s'\n"),
554 sig->pka_info->email);
556 else
558 okay = 0;
559 write_status_text (STATUS_PKA_TRUST_BAD, sig->pka_info->email);
560 log_info (_("Note: Signer's address `%s' "
561 "does not match DNS entry\n"), sig->pka_info->email);
564 switch ( (trustlevel & TRUST_MASK) )
566 case TRUST_UNKNOWN:
567 case TRUST_UNDEFINED:
568 case TRUST_MARGINAL:
569 if (okay && opt.verify_options&VERIFY_PKA_TRUST_INCREASE)
571 trustlevel = ((trustlevel & ~TRUST_MASK) | TRUST_FULLY);
572 log_info (_("trustlevel adjusted to FULL"
573 " due to valid PKA info\n"));
575 /* (fall through) */
576 case TRUST_FULLY:
577 if (!okay)
579 trustlevel = ((trustlevel & ~TRUST_MASK) | TRUST_NEVER);
580 log_info (_("trustlevel adjusted to NEVER"
581 " due to bad PKA info\n"));
583 break;
587 /* Now let the user know what up with the trustlevel. */
588 switch ( (trustlevel & TRUST_MASK) )
590 case TRUST_EXPIRED:
591 log_info(_("Note: This key has expired!\n"));
592 print_fingerprint (pk, NULL, 1);
593 break;
595 default:
596 log_error ("invalid trustlevel %u returned from validation layer\n",
597 trustlevel);
598 /* fall thru */
599 case TRUST_UNKNOWN:
600 case TRUST_UNDEFINED:
601 write_status( STATUS_TRUST_UNDEFINED );
602 log_info(_("WARNING: This key is not certified with"
603 " a trusted signature!\n"));
604 log_info(_(" There is no indication that the "
605 "signature belongs to the owner.\n" ));
606 print_fingerprint (pk, NULL, 1);
607 break;
609 case TRUST_NEVER:
610 /* currently we won't get that status */
611 write_status( STATUS_TRUST_NEVER );
612 log_info(_("WARNING: We do NOT trust this key!\n"));
613 log_info(_(" The signature is probably a FORGERY.\n"));
614 if (opt.with_fingerprint)
615 print_fingerprint (pk, NULL, 1);
616 rc = G10ERR_BAD_SIGN;
617 break;
619 case TRUST_MARGINAL:
620 write_status( STATUS_TRUST_MARGINAL );
621 log_info(_("WARNING: This key is not certified with"
622 " sufficiently trusted signatures!\n"));
623 log_info(_(" It is not certain that the"
624 " signature belongs to the owner.\n" ));
625 print_fingerprint (pk, NULL, 1);
626 break;
628 case TRUST_FULLY:
629 write_status( STATUS_TRUST_FULLY );
630 if (opt.with_fingerprint)
631 print_fingerprint (pk, NULL, 1);
632 break;
634 case TRUST_ULTIMATE:
635 write_status( STATUS_TRUST_ULTIMATE );
636 if (opt.with_fingerprint)
637 print_fingerprint (pk, NULL, 1);
638 break;
641 leave:
642 free_public_key( pk );
643 return rc;
647 void
648 release_pk_list( PK_LIST pk_list )
650 PK_LIST pk_rover;
652 for( ; pk_list; pk_list = pk_rover ) {
653 pk_rover = pk_list->next;
654 free_public_key( pk_list->pk );
655 xfree( pk_list );
660 static int
661 key_present_in_pk_list(PK_LIST pk_list, PKT_public_key *pk)
663 for( ; pk_list; pk_list = pk_list->next)
664 if (cmp_public_keys(pk_list->pk, pk) == 0)
665 return 0;
667 return -1;
671 /****************
672 * Return a malloced string with a default reciepient if there is any
674 static char *
675 default_recipient(void)
677 PKT_secret_key *sk;
678 byte fpr[MAX_FINGERPRINT_LEN+1];
679 size_t n;
680 char *p;
681 int i;
683 if( opt.def_recipient )
684 return xstrdup( opt.def_recipient );
685 if( !opt.def_recipient_self )
686 return NULL;
687 sk = xmalloc_clear( sizeof *sk );
688 i = get_seckey_byname( sk, NULL, 0 );
689 if( i ) {
690 free_secret_key( sk );
691 return NULL;
693 n = MAX_FINGERPRINT_LEN;
694 fingerprint_from_sk( sk, fpr, &n );
695 free_secret_key( sk );
696 p = xmalloc( 2*n+3 );
697 *p++ = '0';
698 *p++ = 'x';
699 for(i=0; i < n; i++ )
700 sprintf( p+2*i, "%02X", fpr[i] );
701 p -= 2;
702 return p;
705 static int
706 expand_id(const char *id,STRLIST *into,unsigned int flags)
708 struct groupitem *groups;
709 int count=0;
711 for(groups=opt.grouplist;groups;groups=groups->next)
713 /* need strcasecmp() here, as this should be localized */
714 if(strcasecmp(groups->name,id)==0)
716 STRLIST each,sl;
718 /* this maintains the current utf8-ness */
719 for(each=groups->values;each;each=each->next)
721 sl=add_to_strlist(into,each->d);
722 sl->flags=flags;
723 count++;
726 break;
730 return count;
733 /* For simplicity, and to avoid potential loops, we only expand once -
734 you can't make an alias that points to an alias. */
735 static STRLIST
736 expand_group(STRLIST input)
738 STRLIST sl,output=NULL,rover;
740 for(rover=input;rover;rover=rover->next)
741 if(expand_id(rover->d,&output,rover->flags)==0)
743 /* Didn't find any groups, so use the existing string */
744 sl=add_to_strlist(&output,rover->d);
745 sl->flags=rover->flags;
748 return output;
752 /* This is the central function to collect the keys for recipients.
753 It is thus used to prepare a public key encryption. encrypt-to
754 keys, default keys and the keys for the actual recipients are all
755 collected here. When not in batch mode and no recipient has been
756 passed on the commandline, the function will also ask for
757 recipients.
759 RCPTS is a string list with the recipients; NULL is an allowed
760 value but not very useful. Group expansion is done on these names;
761 they may be in any of the user Id formats we can handle. The flags
762 bits for each string in the string list are used for:
763 Bit 0: This is an encrypt-to recipient.
764 Bit 1: This is a hidden recipient.
766 USE is the desired use for the key - usually PUBKEY_USAGE_ENC.
767 RET_PK_LIST.
769 On success a list of keys is stored at the address RET_PK_LIST; the
770 caller must free this list. On error the value at this address is
771 not changed.
774 build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned int use )
776 PK_LIST pk_list = NULL;
777 PKT_public_key *pk=NULL;
778 int rc=0;
779 int any_recipients=0;
780 STRLIST rov,remusr;
781 char *def_rec = NULL;
783 /* Try to expand groups if any have been defined. */
784 if (opt.grouplist)
785 remusr = expand_group (rcpts);
786 else
787 remusr = rcpts;
789 /* Check whether there are any recipients in the list and build the
790 * list of the encrypt-to ones (we always trust them). */
791 for ( rov = remusr; rov; rov = rov->next )
793 if ( !(rov->flags & 1) )
795 /* This is a regular recipient; i.e. not an encrypt-to
796 one. */
797 any_recipients = 1;
799 /* Hidden recipients are not allowed while in PGP mode,
800 issue a warning and switch into GnuPG mode. */
801 if ((rov->flags&2) && (PGP2 || PGP6 || PGP7 || PGP8))
803 log_info(_("you may not use %s while in %s mode\n"),
804 "--hidden-recipient",
805 compliance_option_string());
807 compliance_failure();
810 else if ( (use & PUBKEY_USAGE_ENC) && !opt.no_encrypt_to )
812 /* Encryption has been requested and --encrypt-to has not
813 been disabled. Check this encrypt-to key. */
814 pk = xmalloc_clear( sizeof *pk );
815 pk->req_usage = use;
817 /* We explicitly allow encrypt-to to an disabled key; thus
818 we pass 1 as last argument. */
819 if ( (rc = get_pubkey_byname ( pk, rov->d, NULL, NULL, 1 )) )
821 free_public_key ( pk ); pk = NULL;
822 log_error (_("%s: skipped: %s\n"), rov->d, g10_errstr(rc) );
823 write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
824 rov->d, strlen (rov->d), -1);
825 goto fail;
827 else if ( !(rc=check_pubkey_algo2 (pk->pubkey_algo, use )) )
829 /* Skip the actual key if the key is already present
830 * in the list. Add it to our list if not. */
831 if (key_present_in_pk_list(pk_list, pk) == 0)
833 free_public_key (pk); pk = NULL;
834 log_info (_("%s: skipped: public key already present\n"),
835 rov->d);
837 else
839 PK_LIST r;
840 r = xmalloc( sizeof *r );
841 r->pk = pk; pk = NULL;
842 r->next = pk_list;
843 r->flags = (rov->flags&2)?1:0;
844 pk_list = r;
846 /* Hidden encrypt-to recipients are not allowed while
847 in PGP mode, issue a warning and switch into
848 GnuPG mode. */
849 if ((r->flags&1) && (PGP2 || PGP6 || PGP7 || PGP8))
851 log_info(_("you may not use %s while in %s mode\n"),
852 "--hidden-encrypt-to",
853 compliance_option_string());
855 compliance_failure();
859 else
861 /* The public key is not usable for encryption or not
862 available. */
863 free_public_key( pk ); pk = NULL;
864 log_error(_("%s: skipped: %s\n"), rov->d, g10_errstr(rc) );
865 write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
866 rov->d, strlen (rov->d), -1);
867 goto fail;
872 /* If we don't have any recipients yet and we are not in batch mode
873 drop into interactive selection mode. */
874 if ( !any_recipients && !opt.batch )
876 int have_def_rec;
877 char *answer = NULL;
878 STRLIST backlog = NULL;
880 if (pk_list)
881 any_recipients = 1;
882 def_rec = default_recipient();
883 have_def_rec = !!def_rec;
884 if ( !have_def_rec )
885 tty_printf(_("You did not specify a user ID. (you may use \"-r\")\n"));
887 for (;;)
889 rc = 0;
890 xfree(answer);
891 if ( have_def_rec )
893 /* A default recipient is taken as the first entry. */
894 answer = def_rec;
895 def_rec = NULL;
897 else if (backlog)
899 /* This is part of our trick to expand and display groups. */
900 answer = pop_strlist (&backlog);
902 else
904 /* Show the list of already collected recipients and ask
905 for more. */
906 PK_LIST iter;
908 tty_printf("\n");
909 tty_printf(_("Current recipients:\n"));
910 for (iter=pk_list;iter;iter=iter->next)
912 u32 keyid[2];
914 keyid_from_pk(iter->pk,keyid);
915 tty_printf("%4u%c/%s %s \"",
916 nbits_from_pk(iter->pk),
917 pubkey_letter(iter->pk->pubkey_algo),
918 keystr(keyid),
919 datestr_from_pk(iter->pk));
921 if (iter->pk->user_id)
922 tty_print_utf8_string(iter->pk->user_id->name,
923 iter->pk->user_id->len);
924 else
926 size_t n;
927 char *p = get_user_id( keyid, &n );
928 tty_print_utf8_string( p, n );
929 xfree(p);
931 tty_printf("\"\n");
934 answer = cpr_get_utf8("pklist.user_id.enter",
935 _("\nEnter the user ID. "
936 "End with an empty line: "));
937 trim_spaces(answer);
938 cpr_kill_prompt();
941 if ( !answer || !*answer )
943 xfree(answer);
944 break; /* No more recipients entered - get out of loop. */
947 /* Do group expand here too. The trick here is to continue
948 the loop if any expansion occured. The code above will
949 then list all expanded keys. */
950 if (expand_id(answer,&backlog,0))
951 continue;
953 /* Get and check key for the current name. */
954 if (pk)
955 free_public_key (pk);
956 pk = xmalloc_clear( sizeof *pk );
957 pk->req_usage = use;
958 rc = get_pubkey_byname( pk, answer, NULL, NULL, 0 );
959 if (rc)
960 tty_printf(_("No such user ID.\n"));
961 else if ( !(rc=check_pubkey_algo2(pk->pubkey_algo, use)) )
963 if ( have_def_rec )
965 /* No validation for a default recipient. */
966 if (!key_present_in_pk_list(pk_list, pk))
968 free_public_key (pk); pk = NULL;
969 log_info (_("skipped: public key "
970 "already set as default recipient\n") );
972 else
974 PK_LIST r = xmalloc (sizeof *r);
975 r->pk = pk; pk = NULL;
976 r->next = pk_list;
977 r->flags = 0; /* No throwing default ids. */
978 pk_list = r;
980 any_recipients = 1;
981 continue;
983 else
984 { /* Check validity of this key. */
985 int trustlevel;
987 trustlevel = get_validity (pk, pk->user_id);
988 if ( (trustlevel & TRUST_FLAG_DISABLED) )
990 tty_printf (_("Public key is disabled.\n") );
992 else if ( do_we_trust_pre (pk, trustlevel) )
994 /* Skip the actual key if the key is already
995 * present in the list */
996 if (!key_present_in_pk_list(pk_list, pk))
998 free_public_key(pk); pk = NULL;
999 log_info(_("skipped: public key already set\n") );
1001 else
1003 PK_LIST r;
1004 r = xmalloc( sizeof *r );
1005 r->pk = pk; pk = NULL;
1006 r->next = pk_list;
1007 r->flags = 0; /* No throwing interactive ids. */
1008 pk_list = r;
1010 any_recipients = 1;
1011 continue;
1015 xfree(def_rec); def_rec = NULL;
1016 have_def_rec = 0;
1018 if ( pk )
1020 free_public_key( pk );
1021 pk = NULL;
1024 else if ( !any_recipients && (def_rec = default_recipient()) )
1026 /* We are in batch mode and have only a default recipient. */
1027 pk = xmalloc_clear( sizeof *pk );
1028 pk->req_usage = use;
1030 /* The default recipient is allowed to be disabled; thus pass 1
1031 as last argument. */
1032 rc = get_pubkey_byname (pk, def_rec, NULL, NULL, 1);
1033 if (rc)
1034 log_error(_("unknown default recipient \"%s\"\n"), def_rec );
1035 else if ( !(rc=check_pubkey_algo2(pk->pubkey_algo, use)) )
1037 /* Mark any_recipients here since the default recipient
1038 would have been used if it wasn't already there. It
1039 doesn't really matter if we got this key from the default
1040 recipient or an encrypt-to. */
1041 any_recipients = 1;
1042 if (!key_present_in_pk_list(pk_list, pk))
1043 log_info (_("skipped: public key already set "
1044 "as default recipient\n"));
1045 else
1047 PK_LIST r = xmalloc( sizeof *r );
1048 r->pk = pk; pk = NULL;
1049 r->next = pk_list;
1050 r->flags = 0; /* No throwing default ids. */
1051 pk_list = r;
1054 if ( pk )
1056 free_public_key( pk );
1057 pk = NULL;
1059 xfree(def_rec); def_rec = NULL;
1061 else
1063 /* General case: Check all keys. */
1064 any_recipients = 0;
1065 for (; remusr; remusr = remusr->next )
1067 if ( (remusr->flags & 1) )
1068 continue; /* encrypt-to keys are already handled. */
1070 pk = xmalloc_clear( sizeof *pk );
1071 pk->req_usage = use;
1072 if ( (rc = get_pubkey_byname( pk, remusr->d, NULL, NULL, 0 )) )
1074 /* Key not found or other error. */
1075 free_public_key( pk ); pk = NULL;
1076 log_error(_("%s: skipped: %s\n"), remusr->d, g10_errstr(rc) );
1077 write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
1078 remusr->d, strlen (remusr->d),
1079 -1);
1080 goto fail;
1082 else if ( !(rc=check_pubkey_algo2(pk->pubkey_algo, use )) )
1084 /* Key found and usable. Check validity. */
1085 int trustlevel;
1087 trustlevel = get_validity (pk, pk->user_id);
1088 if ( (trustlevel & TRUST_FLAG_DISABLED) )
1090 /*Key has been disabled. */
1091 free_public_key(pk); pk = NULL;
1092 log_info(_("%s: skipped: public key is disabled\n"),
1093 remusr->d);
1094 write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
1095 remusr->d,
1096 strlen (remusr->d),
1097 -1);
1098 rc=G10ERR_UNU_PUBKEY;
1099 goto fail;
1101 else if ( do_we_trust_pre( pk, trustlevel ) )
1103 /* Note: do_we_trust may have changed the trustlevel */
1105 /* We have at least one valid recipient. It doesn't
1106 * matters if this recipient is already present. */
1107 any_recipients = 1;
1109 /* Skip the actual key if the key is already present
1110 * in the list */
1111 if (!key_present_in_pk_list(pk_list, pk))
1113 free_public_key(pk); pk = NULL;
1114 log_info(_("%s: skipped: public key already present\n"),
1115 remusr->d);
1117 else
1119 PK_LIST r;
1120 r = xmalloc( sizeof *r );
1121 r->pk = pk; pk = NULL;
1122 r->next = pk_list;
1123 r->flags = (remusr->flags&2)?1:0;
1124 pk_list = r;
1127 else
1128 { /* We don't trust this key. */
1129 free_public_key( pk ); pk = NULL;
1130 write_status_text_and_buffer (STATUS_INV_RECP, "10 ",
1131 remusr->d,
1132 strlen (remusr->d),
1133 -1);
1134 rc=G10ERR_UNU_PUBKEY;
1135 goto fail;
1138 else
1140 /* Key found but not usable for us (e.g. sign-only key). */
1141 free_public_key( pk ); pk = NULL;
1142 write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
1143 remusr->d,
1144 strlen (remusr->d),
1145 -1);
1146 log_error(_("%s: skipped: %s\n"), remusr->d, g10_errstr(rc) );
1147 goto fail;
1152 if ( !rc && !any_recipients )
1154 log_error(_("no valid addressees\n"));
1155 write_status_text (STATUS_NO_RECP, "0");
1156 rc = G10ERR_NO_USER_ID;
1159 fail:
1161 if ( rc )
1162 release_pk_list( pk_list );
1163 else
1164 *ret_pk_list = pk_list;
1165 if (opt.grouplist)
1166 free_strlist(remusr);
1167 return rc;
1171 /* In pgp6 mode, disallow all ciphers except IDEA (1), 3DES (2), and
1172 CAST5 (3), all hashes except MD5 (1), SHA1 (2), and RIPEMD160 (3),
1173 and all compressions except none (0) and ZIP (1). pgp7 and pgp8
1174 mode expands the cipher list to include AES128 (7), AES192 (8),
1175 AES256 (9), and TWOFISH (10). pgp8 adds the SHA-256 hash (8). For
1176 a true PGP key all of this is unneeded as they are the only items
1177 present in the preferences subpacket, but checking here covers the
1178 weird case of encrypting to a key that had preferences from a
1179 different implementation which was then used with PGP. I am not
1180 completely comfortable with this as the right thing to do, as it
1181 slightly alters the list of what the user is supposedly requesting.
1182 It is not against the RFC however, as the preference chosen will
1183 never be one that the user didn't specify somewhere ("The
1184 implementation may use any mechanism to pick an algorithm in the
1185 intersection"), and PGP has no mechanism to fix such a broken
1186 preference list, so I'm including it. -dms */
1189 algo_available( preftype_t preftype, int algo, void *hint )
1191 if( preftype == PREFTYPE_SYM )
1193 if(PGP6 && (algo != CIPHER_ALGO_IDEA
1194 && algo != CIPHER_ALGO_3DES
1195 && algo != CIPHER_ALGO_CAST5))
1196 return 0;
1198 if(PGP7 && (algo != CIPHER_ALGO_IDEA
1199 && algo != CIPHER_ALGO_3DES
1200 && algo != CIPHER_ALGO_CAST5
1201 && algo != CIPHER_ALGO_AES
1202 && algo != CIPHER_ALGO_AES192
1203 && algo != CIPHER_ALGO_AES256
1204 && algo != CIPHER_ALGO_TWOFISH))
1205 return 0;
1207 /* PGP8 supports all the ciphers we do.. */
1209 return algo && !check_cipher_algo( algo );
1211 else if( preftype == PREFTYPE_HASH )
1213 if(hint)
1215 if(opt.flags.dsa2)
1217 /* If --enable-dsa2 is set, then we'll accept a hash
1218 that is larger than we need. If --enable-dsa2 is not
1219 set, then we won't accept any hash that isn't exactly
1220 the right size. */
1221 if((*(int *)hint) > md_digest_length(algo))
1222 return 0;
1224 else if(((*(int *)hint) != md_digest_length(algo)))
1225 return 0;
1228 if((PGP6 || PGP7) && (algo != DIGEST_ALGO_MD5
1229 && algo != DIGEST_ALGO_SHA1
1230 && algo != DIGEST_ALGO_RMD160))
1231 return 0;
1234 if(PGP8 && (algo != DIGEST_ALGO_MD5
1235 && algo != DIGEST_ALGO_SHA1
1236 && algo != DIGEST_ALGO_RMD160
1237 && algo != DIGEST_ALGO_SHA256))
1238 return 0;
1240 return algo && !check_digest_algo( algo );
1242 else if( preftype == PREFTYPE_ZIP )
1244 if((PGP6 || PGP7) && (algo != COMPRESS_ALGO_NONE
1245 && algo != COMPRESS_ALGO_ZIP))
1246 return 0;
1248 /* PGP8 supports all the compression algos we do */
1250 return !check_compress_algo( algo );
1252 else
1253 return 0;
1258 /****************
1259 * Return -1 if we could not find an algorithm.
1262 select_algo_from_prefs(PK_LIST pk_list, int preftype, int request, void *hint)
1264 PK_LIST pkr;
1265 u32 bits[8];
1266 const prefitem_t *prefs;
1267 int i, j;
1268 int compr_hack=0;
1269 int any;
1271 if( !pk_list )
1272 return -1;
1274 memset( bits, ~0, 8 * sizeof *bits );
1275 for( pkr = pk_list; pkr; pkr = pkr->next ) {
1276 u32 mask[8];
1278 memset( mask, 0, 8 * sizeof *mask );
1279 if( preftype == PREFTYPE_SYM ) {
1280 if( PGP2 &&
1281 pkr->pk->version < 4 &&
1282 pkr->pk->selfsigversion < 4 )
1283 mask[0] |= (1<<1); /* IDEA is implicitly there for v3 keys
1284 with v3 selfsigs (rfc2440:12.1) if
1285 --pgp2 mode is on. This doesn't
1286 mean it's actually available, of
1287 course. */
1288 else
1289 mask[0] |= (1<<2); /* 3DES is implicitly there for everyone else */
1291 else if( preftype == PREFTYPE_HASH ) {
1292 /* While I am including this code for completeness, note
1293 that currently --pgp2 mode locks the hash at MD5, so this
1294 function will never even be called. Even if the hash
1295 wasn't locked at MD5, we don't support sign+encrypt in
1296 --pgp2 mode, and that's the only time PREFTYPE_HASH is
1297 used anyway. -dms */
1298 if( PGP2 &&
1299 pkr->pk->version < 4 &&
1300 pkr->pk->selfsigversion < 4 )
1301 mask[0] |= (1<<1); /* MD5 is there for v3 keys with v3
1302 selfsigs when --pgp2 is on. */
1303 else
1304 mask[0] |= (1<<2); /* SHA1 is there for everyone else */
1306 else if( preftype == PREFTYPE_ZIP )
1307 mask[0] |= (1<<0); /* Uncompressed is implicit */
1309 if (pkr->pk->user_id) /* selected by user ID */
1310 prefs = pkr->pk->user_id->prefs;
1311 else
1312 prefs = pkr->pk->prefs;
1314 any = 0;
1315 if( prefs ) {
1316 for (i=0; prefs[i].type; i++ ) {
1317 if( prefs[i].type == preftype ) {
1318 mask[prefs[i].value/32] |= 1 << (prefs[i].value%32);
1319 any = 1;
1324 if( (!prefs || !any) && preftype == PREFTYPE_ZIP ) {
1325 mask[0] |= 3; /* asume no_compression and old pgp */
1326 compr_hack = 1;
1329 #if 0
1330 log_debug("pref mask=%08lX%08lX%08lX%08lX%08lX%08lX%08lX%08lX\n",
1331 (ulong)mask[7], (ulong)mask[6], (ulong)mask[5], (ulong)mask[4],
1332 (ulong)mask[3], (ulong)mask[2], (ulong)mask[1], (ulong)mask[0]);
1333 #endif
1334 for(i=0; i < 8; i++ )
1335 bits[i] &= mask[i];
1336 #if 0
1337 log_debug("pref bits=%08lX%08lX%08lX%08lX%08lX%08lX%08lX%08lX\n",
1338 (ulong)bits[7], (ulong)bits[6], (ulong)bits[5], (ulong)bits[4],
1339 (ulong)bits[3], (ulong)bits[2], (ulong)bits[1], (ulong)bits[0]);
1340 #endif
1342 /* usable algorithms are now in bits
1343 * We now use the last key from pk_list to select
1344 * the algorithm we want to use. there are no
1345 * preferences for the last key, we select the one
1346 * corresponding to first set bit.
1348 i = -1;
1349 any = 0;
1351 /* Can we use the requested algorithm? */
1352 if(request>-1 && (bits[request/32] & (1<<(request%32))) &&
1353 algo_available(preftype,request,hint))
1354 return request;
1356 /* If we have personal prefs set, use them instead of the last key */
1357 if(preftype==PREFTYPE_SYM && opt.personal_cipher_prefs)
1358 prefs=opt.personal_cipher_prefs;
1359 else if(preftype==PREFTYPE_HASH && opt.personal_digest_prefs)
1360 prefs=opt.personal_digest_prefs;
1361 else if(preftype==PREFTYPE_ZIP && opt.personal_compress_prefs)
1362 prefs=opt.personal_compress_prefs;
1364 if( prefs ) {
1365 for(j=0; prefs[j].type; j++ ) {
1366 if( prefs[j].type == preftype ) {
1367 if( (bits[prefs[j].value/32] & (1<<(prefs[j].value%32))) ) {
1368 if( algo_available( preftype, prefs[j].value, hint ) ) {
1369 any = 1;
1370 i = prefs[j].value;
1371 break;
1377 if( !prefs || !any ) {
1378 for(j=0; j < 256; j++ )
1379 if( (bits[j/32] & (1<<(j%32))) ) {
1380 if( algo_available( preftype, j, hint ) ) {
1381 i = j;
1382 break;
1387 #if 0
1388 log_debug("prefs of type %d: selected %d\n", preftype, i );
1389 #endif
1390 if( compr_hack && !i ) {
1391 /* selected no compression, but we should check whether
1392 * algorithm 1 is also available (the ordering is not relevant
1393 * in this case). */
1394 if( bits[0] & (1<<1) )
1395 i = 1; /* yep; we can use compression algo 1 */
1398 /* "If you are building an authentication system, the recipient
1399 may specify a preferred signing algorithm. However, the signer
1400 would be foolish to use a weak algorithm simply because the
1401 recipient requests it." RFC2440:13. If we settle on MD5, and
1402 SHA1 is also available, use SHA1 instead. Of course, if the
1403 user intentionally chose MD5 (by putting it in their personal
1404 prefs), then we should do what they say. */
1406 if(preftype==PREFTYPE_HASH &&
1407 i==DIGEST_ALGO_MD5 && (bits[0] & (1<<DIGEST_ALGO_SHA1)))
1409 i=DIGEST_ALGO_SHA1;
1411 if(opt.personal_digest_prefs)
1412 for(j=0; prefs[j].type; j++ )
1413 if(opt.personal_digest_prefs[j].type==PREFTYPE_HASH &&
1414 opt.personal_digest_prefs[j].value==DIGEST_ALGO_MD5)
1416 i=DIGEST_ALGO_MD5;
1417 break;
1421 return i;
1425 * Select the MDC flag from the pk_list. We can only use MDC if all recipients
1426 * support this feature
1429 select_mdc_from_pklist (PK_LIST pk_list)
1431 PK_LIST pkr;
1433 if( !pk_list )
1434 return 0;
1436 for (pkr = pk_list; pkr; pkr = pkr->next) {
1437 int mdc;
1439 if (pkr->pk->user_id) /* selected by user ID */
1440 mdc = pkr->pk->user_id->flags.mdc;
1441 else
1442 mdc = pkr->pk->mdc_feature;
1443 if (!mdc)
1444 return 0; /* at least one recipient does not support it */
1446 return 1; /* can be used */