1 /* pkclist.c - create a list of public keys
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 * 2008 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
41 #define CONTROL_D ('D' - 'A' + 1)
44 * Show the revocation reason as it is stored with the given signature
47 do_show_revocation_reason( PKT_signature
*sig
)
54 while( (p
= enum_sig_subpkt (sig
->hashed
, SIGSUBPKT_REVOC_REASON
,
57 continue; /* invalid - just skip it */
60 text
= _("No reason specified");
62 text
= _("Key is superseded");
64 text
= _("Key has been compromised");
66 text
= _("Key is no longer used");
68 text
= _("User ID is no longer valid");
72 log_info( _("reason for revocation: ") );
74 fputs( text
, log_get_stream() );
76 fprintf( log_get_stream(), "code=%02x", *p
);
81 /* We don't want any empty lines, so skip them */
82 while( n
&& *p
== '\n' ) {
87 pp
= memchr( p
, '\n', n
);
89 log_info ( _("revocation comment: ") );
90 print_string ( log_get_stream(), p
, nn
, 0 );
98 /* Mode 0: try and find the revocation based on the pk (i.e. check
99 subkeys, etc.) Mode 1: use only the revocation on the main pk */
102 show_revocation_reason( PKT_public_key
*pk
, int mode
)
104 /* Hmmm, this is not so easy becuase we have to duplicate the code
105 * used in the trustbd to calculate the keyflags. We need to find
106 * a clean way to check revocation certificates on keys and
107 * signatures. And there should be no duplicate code. Because we
108 * enter this function only when the trustdb told us that we have
109 * a revoked key, we could simply look for a revocation cert and
110 * display this one, when there is only one. Let's try to do this
111 * until we have a better solution. */
112 KBNODE node
, keyblock
= NULL
;
113 byte fingerprint
[MAX_FINGERPRINT_LEN
];
117 /* get the keyblock */
118 fingerprint_from_pk( pk
, fingerprint
, &fingerlen
);
119 rc
= get_keyblock_byfprint( &keyblock
, fingerprint
, fingerlen
);
120 if( rc
) { /* that should never happen */
121 log_debug( "failed to get the keyblock\n");
125 for( node
=keyblock
; node
; node
= node
->next
) {
126 if( (mode
&& node
->pkt
->pkttype
== PKT_PUBLIC_KEY
) ||
127 ( ( node
->pkt
->pkttype
== PKT_PUBLIC_KEY
128 || node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
)
129 && !cmp_public_keys( node
->pkt
->pkt
.public_key
, pk
) ) )
133 log_debug("Oops, PK not in keyblock\n");
134 release_kbnode( keyblock
);
137 /* now find the revocation certificate */
138 for( node
= node
->next
; node
; node
= node
->next
) {
139 if( node
->pkt
->pkttype
== PKT_PUBLIC_SUBKEY
)
141 if( node
->pkt
->pkttype
== PKT_SIGNATURE
142 && (node
->pkt
->pkt
.signature
->sig_class
== 0x20
143 || node
->pkt
->pkt
.signature
->sig_class
== 0x28 ) ) {
144 /* FIXME: we should check the signature here */
145 do_show_revocation_reason ( node
->pkt
->pkt
.signature
);
150 /* We didn't find it, so check if the whole key is revoked */
152 show_revocation_reason(pk
,1);
154 release_kbnode( keyblock
);
160 * 1 = Without key info and additional menu option 'm'
161 * this does also add an option to set the key to ultimately trusted.
163 * -2 = nothing changed - caller should show some additional info
164 * -1 = quit operation
165 * 0 = nothing changed
166 * 1 = new ownertrust now in new_trust
169 do_edit_ownertrust (PKT_public_key
*pk
, int mode
,
170 unsigned *new_trust
, int defer_help
)
178 int did_help
=defer_help
;
179 unsigned int minimum
=get_min_ownertrust(pk
);
184 case TRUST_UNDEFINED
: min_num
=1; break;
185 case TRUST_NEVER
: min_num
=2; break;
186 case TRUST_MARGINAL
: min_num
=3; break;
187 case TRUST_FULLY
: min_num
=4; break;
190 keyid_from_pk (pk
, keyid
);
192 /* A string with valid answers.
194 Note to translators: These are the allowed answers in lower and
195 uppercase. Below you will find the matching strings which
196 should be translated accordingly and the letter changed to
197 match the one in the answer string.
199 i = please show me more information
200 m = back to the main menu
204 const char *ans
= _("iImMqQsS");
212 tty_printf(_("No trust value assigned to:\n"));
213 tty_printf("%4u%c/%s %s\n",nbits_from_pk( pk
),
214 pubkey_letter( pk
->pubkey_algo
),
215 keystr(keyid
), datestr_from_pk( pk
) );
216 p
=get_user_id_native(keyid
);
217 tty_printf(_(" \"%s\"\n"),p
);
220 keyblock
= get_pubkeyblock (keyid
);
223 for (un
=keyblock
; un
; un
= un
->next
)
225 if (un
->pkt
->pkttype
!= PKT_USER_ID
)
227 if (un
->pkt
->pkt
.user_id
->is_revoked
)
229 if (un
->pkt
->pkt
.user_id
->is_expired
)
231 /* Only skip textual primaries */
232 if (un
->pkt
->pkt
.user_id
->is_primary
233 && !un
->pkt
->pkt
.user_id
->attrib_data
)
236 if((opt
.verify_options
&VERIFY_SHOW_PHOTOS
)
237 && un
->pkt
->pkt
.user_id
->attrib_data
)
238 show_photos(un
->pkt
->pkt
.user_id
->attribs
,
239 un
->pkt
->pkt
.user_id
->numattribs
,pk
,NULL
,
240 un
->pkt
->pkt
.user_id
);
242 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
243 un
->pkt
->pkt
.user_id
->len
,0);
245 tty_printf(_(" aka \"%s\"\n"),p
);
248 print_fingerprint (pk
, NULL
, 2);
250 release_kbnode (keyblock
);
253 if(opt
.trust_model
==TM_DIRECT
)
255 tty_printf(_("How much do you trust that this key actually "
256 "belongs to the named user?\n"));
261 /* This string also used in keyedit.c:trustsig_prompt */
262 tty_printf(_("Please decide how far you trust this user to"
263 " correctly verify other users' keys\n"
264 "(by looking at passports, checking fingerprints from"
265 " different sources, etc.)\n"));
270 tty_printf (_(" %d = I don't know or won't say\n"), 1);
272 tty_printf (_(" %d = I do NOT trust\n"), 2);
274 tty_printf (_(" %d = I trust marginally\n"), 3);
276 tty_printf (_(" %d = I trust fully\n"), 4);
278 tty_printf (_(" %d = I trust ultimately\n"), 5);
280 /* not yet implemented */
281 tty_printf (" i = please show me more information\n");
284 tty_printf(_(" m = back to the main menu\n"));
287 tty_printf(_(" s = skip this key\n"));
288 tty_printf(_(" q = quit\n"));
292 tty_printf(_("The minimum trust level for this key is: %s\n\n"),
293 trust_value_to_string(minimum
));
296 if( strlen(ans
) != 8 )
298 p
= cpr_get("edit_ownertrust.value",_("Your decision? "));
303 else if( *p
&& p
[1] )
305 else if( !p
[1] && ((*p
>= '0'+min_num
) && *p
<= (mode
?'5':'4')) )
310 case '1': trust
= TRUST_UNDEFINED
; break;
311 case '2': trust
= TRUST_NEVER
; break;
312 case '3': trust
= TRUST_MARGINAL
; break;
313 case '4': trust
= TRUST_FULLY
; break;
314 case '5': trust
= TRUST_ULTIMATE
; break;
317 if (trust
== TRUST_ULTIMATE
318 && !cpr_get_answer_is_yes ("edit_ownertrust.set_ultimate.okay",
319 _("Do you really want to set this key"
320 " to ultimate trust? (y/N) ")))
330 /* not yet implemented */
331 else if( *p
== ans
[0] || *p
== ans
[1] )
333 tty_printf(_("Certificates leading to an ultimately trusted key:\n"));
338 else if( mode
&& (*p
== ans
[2] || *p
== ans
[3] || *p
== CONTROL_D
) )
340 break ; /* back to the menu */
342 else if( !mode
&& (*p
== ans
[6] || *p
== ans
[7] ) )
346 else if( !mode
&& (*p
== ans
[4] || *p
== ans
[5] ) )
349 break ; /* back to the menu */
354 return show
? -2: quit
? -1 : changed
;
358 * Display a menu to change the ownertrust of the key PK (which should
360 * For mode values see do_edit_ownertrust ()
363 edit_ownertrust (PKT_public_key
*pk
, int mode
)
365 unsigned int trust
= 0;
370 switch ( do_edit_ownertrust (pk
, mode
, &trust
, no_help
) )
374 case -2: /* show info */
377 case 1: /* trust value set */
378 trust
&= ~TRUST_FLAG_DISABLED
;
379 trust
|= get_ownertrust (pk
) & TRUST_FLAG_DISABLED
;
380 update_ownertrust (pk
, trust
);
390 * Check whether we can trust this pk which has a trustlevel of TRUSTLEVEL
391 * Returns: true if we trust.
394 do_we_trust( PKT_public_key
*pk
, unsigned int trustlevel
)
396 /* We should not be able to get here with a revoked or expired
398 if(trustlevel
& TRUST_FLAG_REVOKED
399 || trustlevel
& TRUST_FLAG_SUB_REVOKED
400 || (trustlevel
& TRUST_MASK
) == TRUST_EXPIRED
)
403 if( opt
.trust_model
==TM_ALWAYS
)
406 log_info("No trust check due to `--trust-model always' option\n");
410 switch(trustlevel
& TRUST_MASK
)
413 log_error ("invalid trustlevel %u returned from validation layer\n",
417 case TRUST_UNDEFINED
:
418 log_info(_("%s: There is no assurance this key belongs"
419 " to the named user\n"),keystr_from_pk(pk
));
423 log_info(_("%s: There is limited assurance this key belongs"
424 " to the named user\n"),keystr_from_pk(pk
));
429 log_info(_("This key probably belongs to the named user\n"));
434 log_info(_("This key belongs to us\n"));
438 return 1; /*NOTREACHED*/
443 * wrapper around do_we_trust, so we can ask whether to use the
447 do_we_trust_pre( PKT_public_key
*pk
, unsigned int trustlevel
)
451 rc
= do_we_trust( pk
, trustlevel
);
453 if( !opt
.batch
&& !rc
)
455 print_pubkey_info(NULL
,pk
);
456 print_fingerprint (pk
, NULL
, 2);
460 _("It is NOT certain that the key belongs to the person named\n"
461 "in the user ID. If you *really* know what you are doing,\n"
462 "you may answer the next question with yes.\n"));
467 if (is_status_enabled ())
472 keyid_from_pk (pk
, kid
);
473 hint_str
= get_long_user_id_string ( kid
);
474 write_status_text ( STATUS_USERID_HINT
, hint_str
);
478 if( cpr_get_answer_is_yes("untrusted_key.override",
479 _("Use this key anyway? (y/N) ")) )
482 /* Hmmm: Should we set a flag to tell the user about
483 * his decision the next time he encrypts for this recipient?
492 * Check whether we can trust this signature.
493 * Returns: Error if we shall not trust this signatures.
496 check_signatures_trust( PKT_signature
*sig
)
498 PKT_public_key
*pk
= xmalloc_clear( sizeof *pk
);
499 unsigned int trustlevel
;
502 rc
= get_pubkey( pk
, sig
->keyid
);
504 { /* this should not happen */
505 log_error("Ooops; the key vanished - can't check the trust\n");
506 rc
= G10ERR_NO_PUBKEY
;
510 if ( opt
.trust_model
==TM_ALWAYS
)
513 log_info(_("WARNING: Using untrusted key!\n"));
514 if (opt
.with_fingerprint
)
515 print_fingerprint (pk
, NULL
, 1);
519 if(pk
->maybe_revoked
&& !pk
->is_revoked
)
520 log_info(_("WARNING: this key might be revoked (revocation key"
523 trustlevel
= get_validity (pk
, NULL
);
525 if ( (trustlevel
& TRUST_FLAG_REVOKED
) )
527 write_status( STATUS_KEYREVOKED
);
528 if(pk
->is_revoked
==2)
529 log_info(_("WARNING: This key has been revoked by its"
530 " designated revoker!\n"));
532 log_info(_("WARNING: This key has been revoked by its owner!\n"));
533 log_info(_(" This could mean that the signature is forged.\n"));
534 show_revocation_reason( pk
, 0 );
536 else if ((trustlevel
& TRUST_FLAG_SUB_REVOKED
) )
538 write_status( STATUS_KEYREVOKED
);
539 log_info(_("WARNING: This subkey has been revoked by its owner!\n"));
540 show_revocation_reason( pk
, 0 );
543 if ((trustlevel
& TRUST_FLAG_DISABLED
))
544 log_info (_("Note: This key has been disabled.\n"));
546 /* If we have PKA information adjust the trustlevel. */
547 if (sig
->pka_info
&& sig
->pka_info
->valid
)
549 unsigned char fpr
[MAX_FINGERPRINT_LEN
];
550 PKT_public_key
*primary_pk
;
555 primary_pk
= xmalloc_clear (sizeof *primary_pk
);
556 get_pubkey (primary_pk
, pk
->main_keyid
);
557 fingerprint_from_pk (primary_pk
, fpr
, &fprlen
);
558 free_public_key (primary_pk
);
560 if ( fprlen
== 20 && !memcmp (sig
->pka_info
->fpr
, fpr
, 20) )
563 write_status_text (STATUS_PKA_TRUST_GOOD
, sig
->pka_info
->email
);
564 log_info (_("Note: Verified signer's address is `%s'\n"),
565 sig
->pka_info
->email
);
570 write_status_text (STATUS_PKA_TRUST_BAD
, sig
->pka_info
->email
);
571 log_info (_("Note: Signer's address `%s' "
572 "does not match DNS entry\n"), sig
->pka_info
->email
);
575 switch ( (trustlevel
& TRUST_MASK
) )
578 case TRUST_UNDEFINED
:
580 if (okay
&& opt
.verify_options
&VERIFY_PKA_TRUST_INCREASE
)
582 trustlevel
= ((trustlevel
& ~TRUST_MASK
) | TRUST_FULLY
);
583 log_info (_("trustlevel adjusted to FULL"
584 " due to valid PKA info\n"));
590 trustlevel
= ((trustlevel
& ~TRUST_MASK
) | TRUST_NEVER
);
591 log_info (_("trustlevel adjusted to NEVER"
592 " due to bad PKA info\n"));
598 /* Now let the user know what up with the trustlevel. */
599 switch ( (trustlevel
& TRUST_MASK
) )
602 log_info(_("Note: This key has expired!\n"));
603 print_fingerprint (pk
, NULL
, 1);
607 log_error ("invalid trustlevel %u returned from validation layer\n",
611 case TRUST_UNDEFINED
:
612 write_status( STATUS_TRUST_UNDEFINED
);
613 log_info(_("WARNING: This key is not certified with"
614 " a trusted signature!\n"));
615 log_info(_(" There is no indication that the "
616 "signature belongs to the owner.\n" ));
617 print_fingerprint (pk
, NULL
, 1);
621 /* currently we won't get that status */
622 write_status( STATUS_TRUST_NEVER
);
623 log_info(_("WARNING: We do NOT trust this key!\n"));
624 log_info(_(" The signature is probably a FORGERY.\n"));
625 if (opt
.with_fingerprint
)
626 print_fingerprint (pk
, NULL
, 1);
627 rc
= gpg_error (GPG_ERR_BAD_SIGNATURE
);
631 write_status( STATUS_TRUST_MARGINAL
);
632 log_info(_("WARNING: This key is not certified with"
633 " sufficiently trusted signatures!\n"));
634 log_info(_(" It is not certain that the"
635 " signature belongs to the owner.\n" ));
636 print_fingerprint (pk
, NULL
, 1);
640 write_status( STATUS_TRUST_FULLY
);
641 if (opt
.with_fingerprint
)
642 print_fingerprint (pk
, NULL
, 1);
646 write_status( STATUS_TRUST_ULTIMATE
);
647 if (opt
.with_fingerprint
)
648 print_fingerprint (pk
, NULL
, 1);
653 free_public_key( pk
);
659 release_pk_list( PK_LIST pk_list
)
663 for( ; pk_list
; pk_list
= pk_rover
) {
664 pk_rover
= pk_list
->next
;
665 free_public_key( pk_list
->pk
);
672 key_present_in_pk_list(PK_LIST pk_list
, PKT_public_key
*pk
)
674 for( ; pk_list
; pk_list
= pk_list
->next
)
675 if (cmp_public_keys(pk_list
->pk
, pk
) == 0)
683 * Return a malloced string with a default reciepient if there is any
686 default_recipient(void)
689 byte fpr
[MAX_FINGERPRINT_LEN
+1];
694 if( opt
.def_recipient
)
695 return xstrdup( opt
.def_recipient
);
696 if( !opt
.def_recipient_self
)
698 sk
= xmalloc_clear( sizeof *sk
);
699 i
= get_seckey_byname( sk
, NULL
, 0 );
701 free_secret_key( sk
);
704 n
= MAX_FINGERPRINT_LEN
;
705 fingerprint_from_sk( sk
, fpr
, &n
);
706 free_secret_key( sk
);
707 p
= xmalloc( 2*n
+3 );
710 for(i
=0; i
< n
; i
++ )
711 sprintf( p
+2*i
, "%02X", fpr
[i
] );
717 expand_id(const char *id
,strlist_t
*into
,unsigned int flags
)
719 struct groupitem
*groups
;
722 for(groups
=opt
.grouplist
;groups
;groups
=groups
->next
)
724 /* need strcasecmp() here, as this should be localized */
725 if(strcasecmp(groups
->name
,id
)==0)
729 /* this maintains the current utf8-ness */
730 for(each
=groups
->values
;each
;each
=each
->next
)
732 sl
=add_to_strlist(into
,each
->d
);
744 /* For simplicity, and to avoid potential loops, we only expand once -
745 you can't make an alias that points to an alias. */
747 expand_group(strlist_t input
)
749 strlist_t sl
,output
=NULL
,rover
;
751 for(rover
=input
;rover
;rover
=rover
->next
)
752 if(expand_id(rover
->d
,&output
,rover
->flags
)==0)
754 /* Didn't find any groups, so use the existing string */
755 sl
=add_to_strlist(&output
,rover
->d
);
756 sl
->flags
=rover
->flags
;
763 /* This is the central function to collect the keys for recipients.
764 It is thus used to prepare a public key encryption. encrypt-to
765 keys, default keys and the keys for the actual recipients are all
766 collected here. When not in batch mode and no recipient has been
767 passed on the commandline, the function will also ask for
770 RCPTS is a string list with the recipients; NULL is an allowed
771 value but not very useful. Group expansion is done on these names;
772 they may be in any of the user Id formats we can handle. The flags
773 bits for each string in the string list are used for:
774 Bit 0: This is an encrypt-to recipient.
775 Bit 1: This is a hidden recipient.
777 USE is the desired use for the key - usually PUBKEY_USAGE_ENC.
779 On success a list of keys is stored at the address RET_PK_LIST; the
780 caller must free this list. On error the value at this address is
784 build_pk_list( strlist_t rcpts
, PK_LIST
*ret_pk_list
, unsigned int use
)
786 PK_LIST pk_list
= NULL
;
787 PKT_public_key
*pk
=NULL
;
789 int any_recipients
=0;
790 strlist_t rov
,remusr
;
791 char *def_rec
= NULL
;
793 /* Try to expand groups if any have been defined. */
795 remusr
= expand_group (rcpts
);
799 /* Check whether there are any recipients in the list and build the
800 * list of the encrypt-to ones (we always trust them). */
801 for ( rov
= remusr
; rov
; rov
= rov
->next
)
803 if ( !(rov
->flags
& 1) )
805 /* This is a regular recipient; i.e. not an encrypt-to
809 /* Hidden recipients are not allowed while in PGP mode,
810 issue a warning and switch into GnuPG mode. */
811 if ((rov
->flags
&2) && (PGP2
|| PGP6
|| PGP7
|| PGP8
))
813 log_info(_("you may not use %s while in %s mode\n"),
814 "--hidden-recipient",
815 compliance_option_string());
817 compliance_failure();
820 else if ( (use
& PUBKEY_USAGE_ENC
) && !opt
.no_encrypt_to
)
822 /* Encryption has been requested and --encrypt-to has not
823 been disabled. Check this encrypt-to key. */
824 pk
= xmalloc_clear( sizeof *pk
);
827 /* We explicitly allow encrypt-to to an disabled key; thus
828 we pass 1for the second last argument and 1 as the last
829 argument to disable AKL. */
830 if ( (rc
= get_pubkey_byname (NULL
, pk
, rov
->d
, NULL
, NULL
, 1, 1)) )
832 free_public_key ( pk
); pk
= NULL
;
833 log_error (_("%s: skipped: %s\n"), rov
->d
, g10_errstr(rc
) );
834 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
835 rov
->d
, strlen (rov
->d
), -1);
838 else if ( !(rc
=openpgp_pk_test_algo2 (pk
->pubkey_algo
, use
)) )
840 /* Skip the actual key if the key is already present
841 * in the list. Add it to our list if not. */
842 if (key_present_in_pk_list(pk_list
, pk
) == 0)
844 free_public_key (pk
); pk
= NULL
;
845 log_info (_("%s: skipped: public key already present\n"),
851 r
= xmalloc( sizeof *r
);
852 r
->pk
= pk
; pk
= NULL
;
854 r
->flags
= (rov
->flags
&2)?1:0;
857 /* Hidden encrypt-to recipients are not allowed while
858 in PGP mode, issue a warning and switch into
860 if ((r
->flags
&1) && (PGP2
|| PGP6
|| PGP7
|| PGP8
))
862 log_info(_("you may not use %s while in %s mode\n"),
863 "--hidden-encrypt-to",
864 compliance_option_string());
866 compliance_failure();
872 /* The public key is not usable for encryption or not
874 free_public_key( pk
); pk
= NULL
;
875 log_error(_("%s: skipped: %s\n"), rov
->d
, g10_errstr(rc
) );
876 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
877 rov
->d
, strlen (rov
->d
), -1);
883 /* If we don't have any recipients yet and we are not in batch mode
884 drop into interactive selection mode. */
885 if ( !any_recipients
&& !opt
.batch
)
889 strlist_t backlog
= NULL
;
893 def_rec
= default_recipient();
894 have_def_rec
= !!def_rec
;
896 tty_printf(_("You did not specify a user ID. (you may use \"-r\")\n"));
904 /* A default recipient is taken as the first entry. */
910 /* This is part of our trick to expand and display groups. */
911 answer
= strlist_pop (&backlog
);
915 /* Show the list of already collected recipients and ask
920 tty_printf(_("Current recipients:\n"));
921 for (iter
=pk_list
;iter
;iter
=iter
->next
)
925 keyid_from_pk(iter
->pk
,keyid
);
926 tty_printf("%4u%c/%s %s \"",
927 nbits_from_pk(iter
->pk
),
928 pubkey_letter(iter
->pk
->pubkey_algo
),
930 datestr_from_pk(iter
->pk
));
932 if (iter
->pk
->user_id
)
933 tty_print_utf8_string(iter
->pk
->user_id
->name
,
934 iter
->pk
->user_id
->len
);
938 char *p
= get_user_id( keyid
, &n
);
939 tty_print_utf8_string( p
, n
);
945 answer
= cpr_get_utf8("pklist.user_id.enter",
946 _("\nEnter the user ID. "
947 "End with an empty line: "));
952 if ( !answer
|| !*answer
)
955 break; /* No more recipients entered - get out of loop. */
958 /* Do group expand here too. The trick here is to continue
959 the loop if any expansion occured. The code above will
960 then list all expanded keys. */
961 if (expand_id(answer
,&backlog
,0))
964 /* Get and check key for the current name. */
966 free_public_key (pk
);
967 pk
= xmalloc_clear( sizeof *pk
);
969 rc
= get_pubkey_byname (NULL
, pk
, answer
, NULL
, NULL
, 0, 0 );
971 tty_printf(_("No such user ID.\n"));
972 else if ( !(rc
=openpgp_pk_test_algo2 (pk
->pubkey_algo
, use
)) )
976 /* No validation for a default recipient. */
977 if (!key_present_in_pk_list(pk_list
, pk
))
979 free_public_key (pk
); pk
= NULL
;
980 log_info (_("skipped: public key "
981 "already set as default recipient\n") );
985 PK_LIST r
= xmalloc (sizeof *r
);
986 r
->pk
= pk
; pk
= NULL
;
988 r
->flags
= 0; /* No throwing default ids. */
995 { /* Check validity of this key. */
998 trustlevel
= get_validity (pk
, pk
->user_id
);
999 if ( (trustlevel
& TRUST_FLAG_DISABLED
) )
1001 tty_printf (_("Public key is disabled.\n") );
1003 else if ( do_we_trust_pre (pk
, trustlevel
) )
1005 /* Skip the actual key if the key is already
1006 * present in the list */
1007 if (!key_present_in_pk_list(pk_list
, pk
))
1009 free_public_key(pk
); pk
= NULL
;
1010 log_info(_("skipped: public key already set\n") );
1015 r
= xmalloc( sizeof *r
);
1016 r
->pk
= pk
; pk
= NULL
;
1018 r
->flags
= 0; /* No throwing interactive ids. */
1026 xfree(def_rec
); def_rec
= NULL
;
1031 free_public_key( pk
);
1035 else if ( !any_recipients
&& (def_rec
= default_recipient()) )
1037 /* We are in batch mode and have only a default recipient. */
1038 pk
= xmalloc_clear( sizeof *pk
);
1039 pk
->req_usage
= use
;
1041 /* The default recipient is allowed to be disabled; thus pass 1
1042 as second last argument. We also don't want an AKL. */
1043 rc
= get_pubkey_byname (NULL
, pk
, def_rec
, NULL
, NULL
, 1, 1);
1045 log_error(_("unknown default recipient \"%s\"\n"), def_rec
);
1046 else if ( !(rc
=openpgp_pk_test_algo2(pk
->pubkey_algo
, use
)) )
1048 /* Mark any_recipients here since the default recipient
1049 would have been used if it wasn't already there. It
1050 doesn't really matter if we got this key from the default
1051 recipient or an encrypt-to. */
1053 if (!key_present_in_pk_list(pk_list
, pk
))
1054 log_info (_("skipped: public key already set "
1055 "as default recipient\n"));
1058 PK_LIST r
= xmalloc( sizeof *r
);
1059 r
->pk
= pk
; pk
= NULL
;
1061 r
->flags
= 0; /* No throwing default ids. */
1067 free_public_key( pk
);
1070 xfree(def_rec
); def_rec
= NULL
;
1074 /* General case: Check all keys. */
1076 for (; remusr
; remusr
= remusr
->next
)
1078 if ( (remusr
->flags
& 1) )
1079 continue; /* encrypt-to keys are already handled. */
1081 pk
= xmalloc_clear( sizeof *pk
);
1082 pk
->req_usage
= use
;
1083 if ((rc
= get_pubkey_byname (NULL
, pk
, remusr
->d
, NULL
, NULL
, 0, 0)))
1085 /* Key not found or other error. */
1086 free_public_key( pk
); pk
= NULL
;
1087 log_error(_("%s: skipped: %s\n"), remusr
->d
, g10_errstr(rc
) );
1088 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
1089 remusr
->d
, strlen (remusr
->d
),
1093 else if ( !(rc
=openpgp_pk_test_algo2(pk
->pubkey_algo
, use
)) )
1095 /* Key found and usable. Check validity. */
1098 trustlevel
= get_validity (pk
, pk
->user_id
);
1099 if ( (trustlevel
& TRUST_FLAG_DISABLED
) )
1101 /*Key has been disabled. */
1102 free_public_key(pk
); pk
= NULL
;
1103 log_info(_("%s: skipped: public key is disabled\n"),
1105 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
1109 rc
=G10ERR_UNU_PUBKEY
;
1112 else if ( do_we_trust_pre( pk
, trustlevel
) )
1114 /* Note: do_we_trust may have changed the trustlevel */
1116 /* We have at least one valid recipient. It doesn't
1117 * matters if this recipient is already present. */
1120 /* Skip the actual key if the key is already present
1122 if (!key_present_in_pk_list(pk_list
, pk
))
1124 free_public_key(pk
); pk
= NULL
;
1125 log_info(_("%s: skipped: public key already present\n"),
1131 r
= xmalloc( sizeof *r
);
1132 r
->pk
= pk
; pk
= NULL
;
1134 r
->flags
= (remusr
->flags
&2)?1:0;
1139 { /* We don't trust this key. */
1140 free_public_key( pk
); pk
= NULL
;
1141 write_status_text_and_buffer (STATUS_INV_RECP
, "10 ",
1145 rc
=G10ERR_UNU_PUBKEY
;
1151 /* Key found but not usable for us (e.g. sign-only key). */
1152 free_public_key( pk
); pk
= NULL
;
1153 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
1157 log_error(_("%s: skipped: %s\n"), remusr
->d
, g10_errstr(rc
) );
1163 if ( !rc
&& !any_recipients
)
1165 log_error(_("no valid addressees\n"));
1166 write_status_text (STATUS_NO_RECP
, "0");
1167 rc
= G10ERR_NO_USER_ID
;
1173 release_pk_list( pk_list
);
1175 *ret_pk_list
= pk_list
;
1177 free_strlist(remusr
);
1182 /* In pgp6 mode, disallow all ciphers except IDEA (1), 3DES (2), and
1183 CAST5 (3), all hashes except MD5 (1), SHA1 (2), and RIPEMD160 (3),
1184 and all compressions except none (0) and ZIP (1). pgp7 and pgp8
1185 mode expands the cipher list to include AES128 (7), AES192 (8),
1186 AES256 (9), and TWOFISH (10). pgp8 adds the SHA-256 hash (8). For
1187 a true PGP key all of this is unneeded as they are the only items
1188 present in the preferences subpacket, but checking here covers the
1189 weird case of encrypting to a key that had preferences from a
1190 different implementation which was then used with PGP. I am not
1191 completely comfortable with this as the right thing to do, as it
1192 slightly alters the list of what the user is supposedly requesting.
1193 It is not against the RFC however, as the preference chosen will
1194 never be one that the user didn't specify somewhere ("The
1195 implementation may use any mechanism to pick an algorithm in the
1196 intersection"), and PGP has no mechanism to fix such a broken
1197 preference list, so I'm including it. -dms */
1200 algo_available( preftype_t preftype
, int algo
, const union pref_hint
*hint
)
1202 if( preftype
== PREFTYPE_SYM
)
1204 if(PGP6
&& (algo
!= CIPHER_ALGO_IDEA
1205 && algo
!= CIPHER_ALGO_3DES
1206 && algo
!= CIPHER_ALGO_CAST5
))
1209 if(PGP7
&& (algo
!= CIPHER_ALGO_IDEA
1210 && algo
!= CIPHER_ALGO_3DES
1211 && algo
!= CIPHER_ALGO_CAST5
1212 && algo
!= CIPHER_ALGO_AES
1213 && algo
!= CIPHER_ALGO_AES192
1214 && algo
!= CIPHER_ALGO_AES256
1215 && algo
!= CIPHER_ALGO_TWOFISH
))
1218 /* PGP8 supports all the ciphers we do.. */
1220 return algo
&& !openpgp_cipher_test_algo ( algo
);
1222 else if( preftype
== PREFTYPE_HASH
)
1224 if (hint
&& hint
->digest_length
)
1226 if (hint
->digest_length
!=20 || opt
.flags
.dsa2
)
1228 /* If --enable-dsa2 is set or the hash isn't 160 bits
1229 (which implies DSA2), then we'll accept a hash that
1230 is larger than we need. Otherwise we won't accept
1231 any hash that isn't exactly the right size. */
1232 if (hint
->digest_length
> gcry_md_get_algo_dlen (algo
))
1235 else if (hint
->digest_length
!= gcry_md_get_algo_dlen (algo
))
1239 if((PGP6
|| PGP7
) && (algo
!= DIGEST_ALGO_MD5
1240 && algo
!= DIGEST_ALGO_SHA1
1241 && algo
!= DIGEST_ALGO_RMD160
))
1245 if(PGP8
&& (algo
!= DIGEST_ALGO_MD5
1246 && algo
!= DIGEST_ALGO_SHA1
1247 && algo
!= DIGEST_ALGO_RMD160
1248 && algo
!= DIGEST_ALGO_SHA256
))
1251 return algo
&& !openpgp_md_test_algo (algo
);
1253 else if( preftype
== PREFTYPE_ZIP
)
1255 if((PGP6
|| PGP7
) && (algo
!= COMPRESS_ALGO_NONE
1256 && algo
!= COMPRESS_ALGO_ZIP
))
1259 /* PGP8 supports all the compression algos we do */
1261 return !check_compress_algo( algo
);
1268 * Return -1 if we could not find an algorithm.
1271 select_algo_from_prefs(PK_LIST pk_list
, int preftype
,
1272 int request
, const union pref_hint
*hint
)
1276 const prefitem_t
*prefs
;
1278 unsigned int best
=-1;
1284 memset(bits
,0xFF,sizeof(bits
));
1285 memset(scores
,0,sizeof(scores
));
1287 for( pkr
= pk_list
; pkr
; pkr
= pkr
->next
)
1290 int rank
=1,implicit
=-1;
1292 memset(mask
,0,sizeof(mask
));
1297 /* IDEA is implicitly there for v3 keys with v3 selfsigs if
1298 --pgp2 mode is on. This was a 2440 thing that was
1299 dropped from 4880 but is still relevant to GPG's 1991
1300 support. All this doesn't mean IDEA is actually
1301 available, of course. */
1302 if(PGP2
&& pkr
->pk
->version
<4 && pkr
->pk
->selfsigversion
<4)
1303 implicit
=CIPHER_ALGO_IDEA
;
1305 implicit
=CIPHER_ALGO_3DES
;
1310 /* While I am including this code for completeness, note
1311 that currently --pgp2 mode locks the hash at MD5, so this
1312 code will never even be called. Even if the hash wasn't
1313 locked at MD5, we don't support sign+encrypt in --pgp2
1314 mode, and that's the only time PREFTYPE_HASH is used
1317 /* MD5 is there for v3 keys with v3 selfsigs when --pgp2 is
1319 if(PGP2
&& pkr
->pk
->version
<4 && pkr
->pk
->selfsigversion
<4)
1320 implicit
=DIGEST_ALGO_MD5
;
1322 implicit
=DIGEST_ALGO_SHA1
;
1327 /* Uncompressed is always an option. */
1328 implicit
=COMPRESS_ALGO_NONE
;
1331 if (pkr
->pk
->user_id
) /* selected by user ID */
1332 prefs
= pkr
->pk
->user_id
->prefs
;
1334 prefs
= pkr
->pk
->prefs
;
1338 for (i
=0; prefs
[i
].type
; i
++ )
1340 if( prefs
[i
].type
== preftype
)
1342 scores
[prefs
[i
].value
]+=rank
;
1343 mask
[prefs
[i
].value
/32] |= 1<<(prefs
[i
].value
%32);
1347 /* We saw the implicit algorithm, so we don't need
1348 tack it on the end ourselves. */
1349 if(implicit
==prefs
[i
].value
)
1355 if(rank
==1 && preftype
==PREFTYPE_ZIP
)
1357 /* If the compression preferences are not present, they are
1358 assumed to be ZIP, Uncompressed (RFC4880:13.3.1) */
1359 scores
[1]=1; /* ZIP is first choice */
1360 scores
[0]=2; /* Uncompressed is second choice */
1364 /* If the key didn't have the implicit algorithm listed
1365 explicitly, add it here at the tail of the list. */
1368 scores
[implicit
]+=rank
;
1369 mask
[implicit
/32] |= 1<<(implicit
%32);
1376 /* We've now scored all of the algorithms, and the usable ones have
1377 bits set. Let's pick the winner. */
1379 /* The caller passed us a request. Can we use it? */
1380 if(request
>-1 && (bits
[request
/32] & (1<<(request
%32))) &&
1381 algo_available(preftype
,request
,hint
))
1386 /* If we have personal prefs set, use them. */
1388 if(preftype
==PREFTYPE_SYM
&& opt
.personal_cipher_prefs
)
1389 prefs
=opt
.personal_cipher_prefs
;
1390 else if(preftype
==PREFTYPE_HASH
&& opt
.personal_digest_prefs
)
1391 prefs
=opt
.personal_digest_prefs
;
1392 else if(preftype
==PREFTYPE_ZIP
&& opt
.personal_compress_prefs
)
1393 prefs
=opt
.personal_compress_prefs
;
1396 for(i
=0; prefs
[i
].type
; i
++ )
1398 if(bits
[prefs
[i
].value
/32] & (1<<(prefs
[i
].value
%32))
1399 && algo_available( preftype
, prefs
[i
].value
, hint
))
1401 result
= prefs
[i
].value
;
1409 /* At this point, we have not selected an algorithm due to a
1410 special request or via personal prefs. Pick the highest
1411 ranked algorithm (i.e. the one with the lowest score). */
1415 /* Note the '<' here. This means in case of a tie, we will
1416 favor the lower algorithm number. We have a choice
1417 between the lower number (probably an older algorithm
1418 with more time in use), or the higher number (probably a
1419 newer algorithm with less time in use). Older is
1420 probably safer here, even though the newer algorithms
1421 tend to be "stronger". */
1422 if(scores
[i
] && scores
[i
]<best
1423 && (bits
[i
/32] & (1<<(i
%32)))
1424 && algo_available(preftype
,i
,hint
))
1431 /* "If you are building an authentication system, the recipient
1432 may specify a preferred signing algorithm. However, the
1433 signer would be foolish to use a weak algorithm simply
1434 because the recipient requests it." (RFC4880:14). If we
1435 settle on MD5, and SHA1 is also available, use SHA1 instead.
1436 Note that if the user intentionally chose MD5 by putting it
1437 in their personal prefs, then we do what the user said (as we
1438 never reach this code). */
1439 if(preftype
==PREFTYPE_HASH
&& result
==DIGEST_ALGO_MD5
1440 && (bits
[0] & (1<<DIGEST_ALGO_SHA1
)))
1441 result
=DIGEST_ALGO_SHA1
;
1448 * Select the MDC flag from the pk_list. We can only use MDC if all
1449 * recipients support this feature.
1452 select_mdc_from_pklist (PK_LIST pk_list
)
1459 for (pkr
= pk_list
; pkr
; pkr
= pkr
->next
)
1463 if (pkr
->pk
->user_id
) /* selected by user ID */
1464 mdc
= pkr
->pk
->user_id
->flags
.mdc
;
1466 mdc
= pkr
->pk
->mdc_feature
;
1468 return 0; /* At least one recipient does not support it. */
1470 return 1; /* Can be used. */
1474 /* Print a warning for all keys in PK_LIST missing the MDC feature. */
1476 warn_missing_mdc_from_pklist (PK_LIST pk_list
)
1480 for (pkr
= pk_list
; pkr
; pkr
= pkr
->next
)
1484 if (pkr
->pk
->user_id
) /* selected by user ID */
1485 mdc
= pkr
->pk
->user_id
->flags
.mdc
;
1487 mdc
= pkr
->pk
->mdc_feature
;
1489 log_info (_("Note: key %s has no %s feature\n"),
1490 keystr_from_pk (pkr
->pk
), "MDC");
1495 warn_missing_aes_from_pklist (PK_LIST pk_list
)
1499 for (pkr
= pk_list
; pkr
; pkr
= pkr
->next
)
1501 const prefitem_t
*prefs
;
1505 prefs
= pkr
->pk
->user_id
? pkr
->pk
->user_id
->prefs
: pkr
->pk
->prefs
;
1508 for (i
=0; !gotit
&& prefs
[i
].type
; i
++ )
1509 if (prefs
[i
].type
== PREFTYPE_SYM
1510 && prefs
[i
].value
== CIPHER_ALGO_AES
)
1514 log_info (_("Note: key %s has no preference for %s\n"),
1515 keystr_from_pk (pkr
->pk
), "AES");