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,
43 #define CONTROL_D ('D' - 'A' + 1)
46 * Show the revocation reason as it is stored with the given signature
49 do_show_revocation_reason( PKT_signature
*sig
)
56 while( (p
= enum_sig_subpkt (sig
->hashed
, SIGSUBPKT_REVOC_REASON
,
59 continue; /* invalid - just skip it */
62 text
= _("No reason specified");
64 text
= _("Key is superseded");
66 text
= _("Key has been compromised");
68 text
= _("Key is no longer used");
70 text
= _("User ID is no longer valid");
74 log_info( _("reason for revocation: ") );
76 fputs( text
, log_stream() );
78 fprintf( log_stream(), "code=%02x", *p
);
79 putc( '\n', log_stream() );
83 /* We don't want any empty lines, so skip them */
84 while( n
&& *p
== '\n' ) {
89 pp
= memchr( p
, '\n', n
);
91 log_info( _("revocation comment: ") );
92 print_string( log_stream(), p
, nn
, 0 );
93 putc( '\n', log_stream() );
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 */
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
];
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");
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
) ) )
135 log_debug("Oops, PK not in keyblock\n");
136 release_kbnode( keyblock
);
139 /* now find the revocation certificate */
140 for( node
= node
->next
; node
; node
= node
->next
) {
141 if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
)
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
);
152 /* We didn't find it, so check if the whole key is revoked */
154 show_revocation_reason(pk
,1);
156 release_kbnode( keyblock
);
162 * 1 = Without key info and additional menu option 'm'
163 * this does also add an option to set the key to ultimately trusted.
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
171 do_edit_ownertrust (PKT_public_key
*pk
, int mode
,
172 unsigned *new_trust
, int defer_help
)
180 int did_help
=defer_help
;
181 unsigned int minimum
=get_min_ownertrust(pk
);
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
);
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
206 const char *ans
= _("iImMqQsS");
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
);
222 keyblock
= get_pubkeyblock (keyid
);
225 for (un
=keyblock
; un
; un
= un
->next
)
227 if (un
->pkt
->pkttype
!= PKT_USER_ID
)
229 if (un
->pkt
->pkt
.user_id
->is_revoked
)
231 if (un
->pkt
->pkt
.user_id
->is_expired
)
233 /* Only skip textual primaries */
234 if (un
->pkt
->pkt
.user_id
->is_primary
235 && !un
->pkt
->pkt
.user_id
->attrib_data
)
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);
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"));
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"));
271 tty_printf (_(" %d = I don't know or won't say\n"), 1);
273 tty_printf (_(" %d = I do NOT trust\n"), 2);
275 tty_printf (_(" %d = I trust marginally\n"), 3);
277 tty_printf (_(" %d = I trust fully\n"), 4);
279 tty_printf (_(" %d = I trust ultimately\n"), 5);
281 /* not yet implemented */
282 tty_printf (" i = please show me more information\n");
285 tty_printf(_(" m = back to the main menu\n"));
288 tty_printf(_(" s = skip this key\n"));
289 tty_printf(_(" q = quit\n"));
293 tty_printf(_("The minimum trust level for this key is: %s\n\n"),
294 trust_value_to_string(minimum
));
297 if( strlen(ans
) != 8 )
299 p
= cpr_get("edit_ownertrust.value",_("Your decision? "));
304 else if( *p
&& p
[1] )
306 else if( !p
[1] && ((*p
>= '0'+min_num
) && *p
<= (mode
?'5':'4')) )
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;
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) ")))
331 /* not yet implemented */
332 else if( *p
== ans
[0] || *p
== ans
[1] )
334 tty_printf(_("Certificates leading to an ultimately trusted key:\n"));
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] ) )
347 else if( !mode
&& (*p
== ans
[4] || *p
== ans
[5] ) )
350 break ; /* back to the menu */
355 return show
? -2: quit
? -1 : changed
;
359 * Display a menu to change the ownertrust of the key PK (which should
361 * For mode values see do_edit_ownertrust ()
364 edit_ownertrust (PKT_public_key
*pk
, int mode
)
371 switch ( do_edit_ownertrust (pk
, mode
, &trust
, no_help
) )
375 case -2: /* show info */
378 case 1: /* trust value set */
379 trust
&= ~TRUST_FLAG_DISABLED
;
380 trust
|= get_ownertrust (pk
) & TRUST_FLAG_DISABLED
;
381 update_ownertrust (pk
, trust
);
391 * Check whether we can trust this pk which has a trustlevel of TRUSTLEVEL
392 * Returns: true if we trust.
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
399 if(trustlevel
& TRUST_FLAG_REVOKED
400 || trustlevel
& TRUST_FLAG_SUB_REVOKED
401 || (trustlevel
& TRUST_MASK
) == TRUST_EXPIRED
)
404 if( opt
.trust_model
==TM_ALWAYS
)
407 log_info("No trust check due to `--trust-model always' option\n");
411 switch(trustlevel
& TRUST_MASK
)
414 log_error ("invalid trustlevel %u returned from validation layer\n",
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
));
424 log_info(_("%s: There is limited assurance this key belongs"
425 " to the named user\n"),keystr_from_pk(pk
));
430 log_info(_("This key probably belongs to the named user\n"));
435 log_info(_("This key belongs to us\n"));
439 return 1; /*NOTREACHED*/
444 * wrapper around do_we_trust, so we can ask whether to use the
448 do_we_trust_pre( PKT_public_key
*pk
, unsigned int trustlevel
)
452 rc
= do_we_trust( pk
, trustlevel
);
454 if( !opt
.batch
&& !rc
)
456 print_pubkey_info(NULL
,pk
);
457 print_fingerprint (pk
, NULL
, 2);
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"));
467 if( cpr_get_answer_is_yes("untrusted_key.override",
468 _("Use this key anyway? (y/N) ")) )
471 /* Hmmm: Should we set a flag to tell the user about
472 * his decision the next time he encrypts for this recipient?
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
;
491 rc
= get_pubkey( pk
, sig
->keyid
);
493 { /* this should not happen */
494 log_error("Ooops; the key vanished - can't check the trust\n");
495 rc
= G10ERR_NO_PUBKEY
;
499 if ( opt
.trust_model
==TM_ALWAYS
)
502 log_info(_("WARNING: Using untrusted key!\n"));
503 if (opt
.with_fingerprint
)
504 print_fingerprint (pk
, NULL
, 1);
508 if(pk
->maybe_revoked
&& !pk
->is_revoked
)
509 log_info(_("WARNING: this key might be revoked (revocation key"
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"));
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
;
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) )
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
);
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
) )
567 case TRUST_UNDEFINED
:
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"));
579 trustlevel
= ((trustlevel
& ~TRUST_MASK
) | TRUST_NEVER
);
580 log_info (_("trustlevel adjusted to NEVER"
581 " due to bad PKA info\n"));
587 /* Now let the user know what up with the trustlevel. */
588 switch ( (trustlevel
& TRUST_MASK
) )
591 log_info(_("Note: This key has expired!\n"));
592 print_fingerprint (pk
, NULL
, 1);
596 log_error ("invalid trustlevel %u returned from validation layer\n",
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);
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
;
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);
629 write_status( STATUS_TRUST_FULLY
);
630 if (opt
.with_fingerprint
)
631 print_fingerprint (pk
, NULL
, 1);
635 write_status( STATUS_TRUST_ULTIMATE
);
636 if (opt
.with_fingerprint
)
637 print_fingerprint (pk
, NULL
, 1);
642 free_public_key( pk
);
648 release_pk_list( PK_LIST pk_list
)
652 for( ; pk_list
; pk_list
= pk_rover
) {
653 pk_rover
= pk_list
->next
;
654 free_public_key( pk_list
->pk
);
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)
672 * Return a malloced string with a default reciepient if there is any
675 default_recipient(void)
678 byte fpr
[MAX_FINGERPRINT_LEN
+1];
683 if( opt
.def_recipient
)
684 return xstrdup( opt
.def_recipient
);
685 if( !opt
.def_recipient_self
)
687 sk
= xmalloc_clear( sizeof *sk
);
688 i
= get_seckey_byname( sk
, NULL
, 0 );
690 free_secret_key( sk
);
693 n
= MAX_FINGERPRINT_LEN
;
694 fingerprint_from_sk( sk
, fpr
, &n
);
695 free_secret_key( sk
);
696 p
= xmalloc( 2*n
+3 );
699 for(i
=0; i
< n
; i
++ )
700 sprintf( p
+2*i
, "%02X", fpr
[i
] );
706 expand_id(const char *id
,STRLIST
*into
,unsigned int flags
)
708 struct groupitem
*groups
;
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)
718 /* this maintains the current utf8-ness */
719 for(each
=groups
->values
;each
;each
=each
->next
)
721 sl
=add_to_strlist(into
,each
->d
);
733 /* For simplicity, and to avoid potential loops, we only expand once -
734 you can't make an alias that points to an alias. */
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
;
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
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.
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
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
;
779 int any_recipients
=0;
781 char *def_rec
= NULL
;
783 /* Try to expand groups if any have been defined. */
785 remusr
= expand_group (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
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
);
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);
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"),
840 r
= xmalloc( sizeof *r
);
841 r
->pk
= pk
; pk
= NULL
;
843 r
->flags
= (rov
->flags
&2)?1:0;
846 /* Hidden encrypt-to recipients are not allowed while
847 in PGP mode, issue a warning and switch into
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();
861 /* The public key is not usable for encryption or not
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);
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
)
878 STRLIST backlog
= NULL
;
882 def_rec
= default_recipient();
883 have_def_rec
= !!def_rec
;
885 tty_printf(_("You did not specify a user ID. (you may use \"-r\")\n"));
893 /* A default recipient is taken as the first entry. */
899 /* This is part of our trick to expand and display groups. */
900 answer
= pop_strlist (&backlog
);
904 /* Show the list of already collected recipients and ask
909 tty_printf(_("Current recipients:\n"));
910 for (iter
=pk_list
;iter
;iter
=iter
->next
)
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
),
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
);
927 char *p
= get_user_id( keyid
, &n
);
928 tty_print_utf8_string( p
, n
);
934 answer
= cpr_get_utf8("pklist.user_id.enter",
935 _("\nEnter the user ID. "
936 "End with an empty line: "));
941 if ( !answer
|| !*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))
953 /* Get and check key for the current name. */
955 free_public_key (pk
);
956 pk
= xmalloc_clear( sizeof *pk
);
958 rc
= get_pubkey_byname( pk
, answer
, NULL
, NULL
, 0 );
960 tty_printf(_("No such user ID.\n"));
961 else if ( !(rc
=check_pubkey_algo2(pk
->pubkey_algo
, use
)) )
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") );
974 PK_LIST r
= xmalloc (sizeof *r
);
975 r
->pk
= pk
; pk
= NULL
;
977 r
->flags
= 0; /* No throwing default ids. */
984 { /* Check validity of this key. */
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") );
1004 r
= xmalloc( sizeof *r
);
1005 r
->pk
= pk
; pk
= NULL
;
1007 r
->flags
= 0; /* No throwing interactive ids. */
1015 xfree(def_rec
); def_rec
= NULL
;
1020 free_public_key( pk
);
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);
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. */
1042 if (!key_present_in_pk_list(pk_list
, pk
))
1043 log_info (_("skipped: public key already set "
1044 "as default recipient\n"));
1047 PK_LIST r
= xmalloc( sizeof *r
);
1048 r
->pk
= pk
; pk
= NULL
;
1050 r
->flags
= 0; /* No throwing default ids. */
1056 free_public_key( pk
);
1059 xfree(def_rec
); def_rec
= NULL
;
1063 /* General case: Check all keys. */
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
),
1082 else if ( !(rc
=check_pubkey_algo2(pk
->pubkey_algo
, use
)) )
1084 /* Key found and usable. Check validity. */
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"),
1094 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
1098 rc
=G10ERR_UNU_PUBKEY
;
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. */
1109 /* Skip the actual key if the key is already present
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"),
1120 r
= xmalloc( sizeof *r
);
1121 r
->pk
= pk
; pk
= NULL
;
1123 r
->flags
= (remusr
->flags
&2)?1:0;
1128 { /* We don't trust this key. */
1129 free_public_key( pk
); pk
= NULL
;
1130 write_status_text_and_buffer (STATUS_INV_RECP
, "10 ",
1134 rc
=G10ERR_UNU_PUBKEY
;
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 ",
1146 log_error(_("%s: skipped: %s\n"), remusr
->d
, g10_errstr(rc
) );
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
;
1162 release_pk_list( pk_list
);
1164 *ret_pk_list
= pk_list
;
1166 free_strlist(remusr
);
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
))
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
))
1207 /* PGP8 supports all the ciphers we do.. */
1209 return algo
&& !check_cipher_algo( algo
);
1211 else if( preftype
== PREFTYPE_HASH
)
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
1221 if((*(int *)hint
) > md_digest_length(algo
))
1224 else if(((*(int *)hint
) != md_digest_length(algo
)))
1228 if((PGP6
|| PGP7
) && (algo
!= DIGEST_ALGO_MD5
1229 && algo
!= DIGEST_ALGO_SHA1
1230 && algo
!= DIGEST_ALGO_RMD160
))
1234 if(PGP8
&& (algo
!= DIGEST_ALGO_MD5
1235 && algo
!= DIGEST_ALGO_SHA1
1236 && algo
!= DIGEST_ALGO_RMD160
1237 && algo
!= DIGEST_ALGO_SHA256
))
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
))
1248 /* PGP8 supports all the compression algos we do */
1250 return !check_compress_algo( algo
);
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
)
1266 const prefitem_t
*prefs
;
1274 memset( bits
, ~0, 8 * sizeof *bits
);
1275 for( pkr
= pk_list
; pkr
; pkr
= pkr
->next
) {
1278 memset( mask
, 0, 8 * sizeof *mask
);
1279 if( preftype
== PREFTYPE_SYM
) {
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
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 */
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. */
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
;
1312 prefs
= pkr
->pk
->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);
1324 if( (!prefs
|| !any
) && preftype
== PREFTYPE_ZIP
) {
1325 mask
[0] |= 3; /* asume no_compression and old pgp */
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]);
1334 for(i
=0; i
< 8; i
++ )
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]);
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.
1351 /* Can we use the requested algorithm? */
1352 if(request
>-1 && (bits
[request
/32] & (1<<(request
%32))) &&
1353 algo_available(preftype
,request
,hint
))
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
;
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
) ) {
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
) ) {
1388 log_debug("prefs of type %d: selected %d\n", preftype
, i
);
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
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
)))
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
)
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
)
1436 for (pkr
= pk_list
; pkr
; pkr
= pkr
->next
) {
1439 if (pkr
->pk
->user_id
) /* selected by user ID */
1440 mdc
= pkr
->pk
->user_id
->flags
.mdc
;
1442 mdc
= pkr
->pk
->mdc_feature
;
1444 return 0; /* at least one recipient does not support it */
1446 return 1; /* can be used */