1 /* pkclist.c - create a list of public keys
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 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
);
241 p
=utf8_to_native(un
->pkt
->pkt
.user_id
->name
,
242 un
->pkt
->pkt
.user_id
->len
,0);
244 tty_printf(_(" aka \"%s\"\n"),p
);
247 print_fingerprint (pk
, NULL
, 2);
249 release_kbnode (keyblock
);
252 if(opt
.trust_model
==TM_DIRECT
)
254 tty_printf(_("How much do you trust that this key actually "
255 "belongs to the named user?\n"));
260 /* This string also used in keyedit.c:trustsig_prompt */
261 tty_printf(_("Please decide how far you trust this user to"
262 " correctly verify other users' keys\n"
263 "(by looking at passports, checking fingerprints from"
264 " different sources, etc.)\n"));
269 tty_printf (_(" %d = I don't know or won't say\n"), 1);
271 tty_printf (_(" %d = I do NOT trust\n"), 2);
273 tty_printf (_(" %d = I trust marginally\n"), 3);
275 tty_printf (_(" %d = I trust fully\n"), 4);
277 tty_printf (_(" %d = I trust ultimately\n"), 5);
279 /* not yet implemented */
280 tty_printf (" i = please show me more information\n");
283 tty_printf(_(" m = back to the main menu\n"));
286 tty_printf(_(" s = skip this key\n"));
287 tty_printf(_(" q = quit\n"));
291 tty_printf(_("The minimum trust level for this key is: %s\n\n"),
292 trust_value_to_string(minimum
));
295 if( strlen(ans
) != 8 )
297 p
= cpr_get("edit_ownertrust.value",_("Your decision? "));
302 else if( *p
&& p
[1] )
304 else if( !p
[1] && ((*p
>= '0'+min_num
) && *p
<= (mode
?'5':'4')) )
309 case '1': trust
= TRUST_UNDEFINED
; break;
310 case '2': trust
= TRUST_NEVER
; break;
311 case '3': trust
= TRUST_MARGINAL
; break;
312 case '4': trust
= TRUST_FULLY
; break;
313 case '5': trust
= TRUST_ULTIMATE
; break;
316 if (trust
== TRUST_ULTIMATE
317 && !cpr_get_answer_is_yes ("edit_ownertrust.set_ultimate.okay",
318 _("Do you really want to set this key"
319 " to ultimate trust? (y/N) ")))
329 /* not yet implemented */
330 else if( *p
== ans
[0] || *p
== ans
[1] )
332 tty_printf(_("Certificates leading to an ultimately trusted key:\n"));
337 else if( mode
&& (*p
== ans
[2] || *p
== ans
[3] || *p
== CONTROL_D
) )
339 break ; /* back to the menu */
341 else if( !mode
&& (*p
== ans
[6] || *p
== ans
[7] ) )
345 else if( !mode
&& (*p
== ans
[4] || *p
== ans
[5] ) )
348 break ; /* back to the menu */
353 return show
? -2: quit
? -1 : changed
;
357 * Display a menu to change the ownertrust of the key PK (which should
359 * For mode values see do_edit_ownertrust ()
362 edit_ownertrust (PKT_public_key
*pk
, int mode
)
364 unsigned int trust
= 0;
369 switch ( do_edit_ownertrust (pk
, mode
, &trust
, no_help
) )
373 case -2: /* show info */
376 case 1: /* trust value set */
377 trust
&= ~TRUST_FLAG_DISABLED
;
378 trust
|= get_ownertrust (pk
) & TRUST_FLAG_DISABLED
;
379 update_ownertrust (pk
, trust
);
389 * Check whether we can trust this pk which has a trustlevel of TRUSTLEVEL
390 * Returns: true if we trust.
393 do_we_trust( PKT_public_key
*pk
, unsigned int trustlevel
)
395 /* We should not be able to get here with a revoked or expired
397 if(trustlevel
& TRUST_FLAG_REVOKED
398 || trustlevel
& TRUST_FLAG_SUB_REVOKED
399 || (trustlevel
& TRUST_MASK
) == TRUST_EXPIRED
)
402 if( opt
.trust_model
==TM_ALWAYS
)
405 log_info("No trust check due to `--trust-model always' option\n");
409 switch(trustlevel
& TRUST_MASK
)
412 log_error ("invalid trustlevel %u returned from validation layer\n",
416 case TRUST_UNDEFINED
:
417 log_info(_("%s: There is no assurance this key belongs"
418 " to the named user\n"),keystr_from_pk(pk
));
422 log_info(_("%s: There is limited assurance this key belongs"
423 " to the named user\n"),keystr_from_pk(pk
));
428 log_info(_("This key probably belongs to the named user\n"));
433 log_info(_("This key belongs to us\n"));
437 return 1; /*NOTREACHED*/
442 * wrapper around do_we_trust, so we can ask whether to use the
446 do_we_trust_pre( PKT_public_key
*pk
, unsigned int trustlevel
)
450 rc
= do_we_trust( pk
, trustlevel
);
452 if( !opt
.batch
&& !rc
)
454 print_pubkey_info(NULL
,pk
);
455 print_fingerprint (pk
, NULL
, 2);
459 _("It is NOT certain that the key belongs to the person named\n"
460 "in the user ID. If you *really* know what you are doing,\n"
461 "you may answer the next question with yes.\n"));
466 if (is_status_enabled ())
471 keyid_from_pk (pk
, kid
);
472 hint_str
= get_long_user_id_string ( kid
);
473 write_status_text ( STATUS_USERID_HINT
, hint_str
);
477 if( cpr_get_answer_is_yes("untrusted_key.override",
478 _("Use this key anyway? (y/N) ")) )
481 /* Hmmm: Should we set a flag to tell the user about
482 * his decision the next time he encrypts for this recipient?
491 * Check whether we can trust this signature.
492 * Returns: Error if we shall not trust this signatures.
495 check_signatures_trust( PKT_signature
*sig
)
497 PKT_public_key
*pk
= xmalloc_clear( sizeof *pk
);
498 unsigned int trustlevel
;
501 rc
= get_pubkey( pk
, sig
->keyid
);
503 { /* this should not happen */
504 log_error("Ooops; the key vanished - can't check the trust\n");
505 rc
= G10ERR_NO_PUBKEY
;
509 if ( opt
.trust_model
==TM_ALWAYS
)
512 log_info(_("WARNING: Using untrusted key!\n"));
513 if (opt
.with_fingerprint
)
514 print_fingerprint (pk
, NULL
, 1);
518 if(pk
->maybe_revoked
&& !pk
->is_revoked
)
519 log_info(_("WARNING: this key might be revoked (revocation key"
522 trustlevel
= get_validity (pk
, NULL
);
524 if ( (trustlevel
& TRUST_FLAG_REVOKED
) )
526 write_status( STATUS_KEYREVOKED
);
527 if(pk
->is_revoked
==2)
528 log_info(_("WARNING: This key has been revoked by its"
529 " designated revoker!\n"));
531 log_info(_("WARNING: This key has been revoked by its owner!\n"));
532 log_info(_(" This could mean that the signature is forged.\n"));
533 show_revocation_reason( pk
, 0 );
535 else if ((trustlevel
& TRUST_FLAG_SUB_REVOKED
) )
537 write_status( STATUS_KEYREVOKED
);
538 log_info(_("WARNING: This subkey has been revoked by its owner!\n"));
539 show_revocation_reason( pk
, 0 );
542 if ((trustlevel
& TRUST_FLAG_DISABLED
))
543 log_info (_("Note: This key has been disabled.\n"));
545 /* If we have PKA information adjust the trustlevel. */
546 if (sig
->pka_info
&& sig
->pka_info
->valid
)
548 unsigned char fpr
[MAX_FINGERPRINT_LEN
];
549 PKT_public_key
*primary_pk
;
554 primary_pk
= xmalloc_clear (sizeof *primary_pk
);
555 get_pubkey (primary_pk
, pk
->main_keyid
);
556 fingerprint_from_pk (primary_pk
, fpr
, &fprlen
);
557 free_public_key (primary_pk
);
559 if ( fprlen
== 20 && !memcmp (sig
->pka_info
->fpr
, fpr
, 20) )
562 write_status_text (STATUS_PKA_TRUST_GOOD
, sig
->pka_info
->email
);
563 log_info (_("Note: Verified signer's address is `%s'\n"),
564 sig
->pka_info
->email
);
569 write_status_text (STATUS_PKA_TRUST_BAD
, sig
->pka_info
->email
);
570 log_info (_("Note: Signer's address `%s' "
571 "does not match DNS entry\n"), sig
->pka_info
->email
);
574 switch ( (trustlevel
& TRUST_MASK
) )
577 case TRUST_UNDEFINED
:
579 if (okay
&& opt
.verify_options
&VERIFY_PKA_TRUST_INCREASE
)
581 trustlevel
= ((trustlevel
& ~TRUST_MASK
) | TRUST_FULLY
);
582 log_info (_("trustlevel adjusted to FULL"
583 " due to valid PKA info\n"));
589 trustlevel
= ((trustlevel
& ~TRUST_MASK
) | TRUST_NEVER
);
590 log_info (_("trustlevel adjusted to NEVER"
591 " due to bad PKA info\n"));
597 /* Now let the user know what up with the trustlevel. */
598 switch ( (trustlevel
& TRUST_MASK
) )
601 log_info(_("Note: This key has expired!\n"));
602 print_fingerprint (pk
, NULL
, 1);
606 log_error ("invalid trustlevel %u returned from validation layer\n",
610 case TRUST_UNDEFINED
:
611 write_status( STATUS_TRUST_UNDEFINED
);
612 log_info(_("WARNING: This key is not certified with"
613 " a trusted signature!\n"));
614 log_info(_(" There is no indication that the "
615 "signature belongs to the owner.\n" ));
616 print_fingerprint (pk
, NULL
, 1);
620 /* currently we won't get that status */
621 write_status( STATUS_TRUST_NEVER
);
622 log_info(_("WARNING: We do NOT trust this key!\n"));
623 log_info(_(" The signature is probably a FORGERY.\n"));
624 if (opt
.with_fingerprint
)
625 print_fingerprint (pk
, NULL
, 1);
626 rc
= gpg_error (GPG_ERR_BAD_SIGNATURE
);
630 write_status( STATUS_TRUST_MARGINAL
);
631 log_info(_("WARNING: This key is not certified with"
632 " sufficiently trusted signatures!\n"));
633 log_info(_(" It is not certain that the"
634 " signature belongs to the owner.\n" ));
635 print_fingerprint (pk
, NULL
, 1);
639 write_status( STATUS_TRUST_FULLY
);
640 if (opt
.with_fingerprint
)
641 print_fingerprint (pk
, NULL
, 1);
645 write_status( STATUS_TRUST_ULTIMATE
);
646 if (opt
.with_fingerprint
)
647 print_fingerprint (pk
, NULL
, 1);
652 free_public_key( pk
);
658 release_pk_list( PK_LIST pk_list
)
662 for( ; pk_list
; pk_list
= pk_rover
) {
663 pk_rover
= pk_list
->next
;
664 free_public_key( pk_list
->pk
);
671 key_present_in_pk_list(PK_LIST pk_list
, PKT_public_key
*pk
)
673 for( ; pk_list
; pk_list
= pk_list
->next
)
674 if (cmp_public_keys(pk_list
->pk
, pk
) == 0)
682 * Return a malloced string with a default reciepient if there is any
685 default_recipient(void)
688 byte fpr
[MAX_FINGERPRINT_LEN
+1];
693 if( opt
.def_recipient
)
694 return xstrdup( opt
.def_recipient
);
695 if( !opt
.def_recipient_self
)
697 sk
= xmalloc_clear( sizeof *sk
);
698 i
= get_seckey_byname( sk
, NULL
, 0 );
700 free_secret_key( sk
);
703 n
= MAX_FINGERPRINT_LEN
;
704 fingerprint_from_sk( sk
, fpr
, &n
);
705 free_secret_key( sk
);
706 p
= xmalloc( 2*n
+3 );
709 for(i
=0; i
< n
; i
++ )
710 sprintf( p
+2*i
, "%02X", fpr
[i
] );
716 expand_id(const char *id
,strlist_t
*into
,unsigned int flags
)
718 struct groupitem
*groups
;
721 for(groups
=opt
.grouplist
;groups
;groups
=groups
->next
)
723 /* need strcasecmp() here, as this should be localized */
724 if(strcasecmp(groups
->name
,id
)==0)
728 /* this maintains the current utf8-ness */
729 for(each
=groups
->values
;each
;each
=each
->next
)
731 sl
=add_to_strlist(into
,each
->d
);
743 /* For simplicity, and to avoid potential loops, we only expand once -
744 you can't make an alias that points to an alias. */
746 expand_group(strlist_t input
)
748 strlist_t sl
,output
=NULL
,rover
;
750 for(rover
=input
;rover
;rover
=rover
->next
)
751 if(expand_id(rover
->d
,&output
,rover
->flags
)==0)
753 /* Didn't find any groups, so use the existing string */
754 sl
=add_to_strlist(&output
,rover
->d
);
755 sl
->flags
=rover
->flags
;
762 /* This is the central function to collect the keys for recipients.
763 It is thus used to prepare a public key encryption. encrypt-to
764 keys, default keys and the keys for the actual recipients are all
765 collected here. When not in batch mode and no recipient has been
766 passed on the commandline, the function will also ask for
769 RCPTS is a string list with the recipients; NULL is an allowed
770 value but not very useful. Group expansion is done on these names;
771 they may be in any of the user Id formats we can handle. The flags
772 bits for each string in the string list are used for:
773 Bit 0: This is an encrypt-to recipient.
774 Bit 1: This is a hidden recipient.
776 USE is the desired use for the key - usually PUBKEY_USAGE_ENC.
778 On success a list of keys is stored at the address RET_PK_LIST; the
779 caller must free this list. On error the value at this address is
783 build_pk_list( strlist_t rcpts
, PK_LIST
*ret_pk_list
, unsigned int use
)
785 PK_LIST pk_list
= NULL
;
786 PKT_public_key
*pk
=NULL
;
788 int any_recipients
=0;
789 strlist_t rov
,remusr
;
790 char *def_rec
= NULL
;
792 /* Try to expand groups if any have been defined. */
794 remusr
= expand_group (rcpts
);
798 /* Check whether there are any recipients in the list and build the
799 * list of the encrypt-to ones (we always trust them). */
800 for ( rov
= remusr
; rov
; rov
= rov
->next
)
802 if ( !(rov
->flags
& 1) )
804 /* This is a regular recipient; i.e. not an encrypt-to
808 /* Hidden recipients are not allowed while in PGP mode,
809 issue a warning and switch into GnuPG mode. */
810 if ((rov
->flags
&2) && (PGP2
|| PGP6
|| PGP7
|| PGP8
))
812 log_info(_("you may not use %s while in %s mode\n"),
813 "--hidden-recipient",
814 compliance_option_string());
816 compliance_failure();
819 else if ( (use
& PUBKEY_USAGE_ENC
) && !opt
.no_encrypt_to
)
821 /* Encryption has been requested and --encrypt-to has not
822 been disabled. Check this encrypt-to key. */
823 pk
= xmalloc_clear( sizeof *pk
);
826 /* We explicitly allow encrypt-to to an disabled key; thus
827 we pass 1for the second last argument and 1 as the last
828 argument to disable AKL. */
829 if ( (rc
= get_pubkey_byname (NULL
, pk
, rov
->d
, NULL
, NULL
, 1, 1)) )
831 free_public_key ( pk
); pk
= NULL
;
832 log_error (_("%s: skipped: %s\n"), rov
->d
, g10_errstr(rc
) );
833 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
834 rov
->d
, strlen (rov
->d
), -1);
837 else if ( !(rc
=openpgp_pk_test_algo2 (pk
->pubkey_algo
, use
)) )
839 /* Skip the actual key if the key is already present
840 * in the list. Add it to our list if not. */
841 if (key_present_in_pk_list(pk_list
, pk
) == 0)
843 free_public_key (pk
); pk
= NULL
;
844 log_info (_("%s: skipped: public key already present\n"),
850 r
= xmalloc( sizeof *r
);
851 r
->pk
= pk
; pk
= NULL
;
853 r
->flags
= (rov
->flags
&2)?1:0;
856 /* Hidden encrypt-to recipients are not allowed while
857 in PGP mode, issue a warning and switch into
859 if ((r
->flags
&1) && (PGP2
|| PGP6
|| PGP7
|| PGP8
))
861 log_info(_("you may not use %s while in %s mode\n"),
862 "--hidden-encrypt-to",
863 compliance_option_string());
865 compliance_failure();
871 /* The public key is not usable for encryption or not
873 free_public_key( pk
); pk
= NULL
;
874 log_error(_("%s: skipped: %s\n"), rov
->d
, g10_errstr(rc
) );
875 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
876 rov
->d
, strlen (rov
->d
), -1);
882 /* If we don't have any recipients yet and we are not in batch mode
883 drop into interactive selection mode. */
884 if ( !any_recipients
&& !opt
.batch
)
888 strlist_t backlog
= NULL
;
892 def_rec
= default_recipient();
893 have_def_rec
= !!def_rec
;
895 tty_printf(_("You did not specify a user ID. (you may use \"-r\")\n"));
903 /* A default recipient is taken as the first entry. */
909 /* This is part of our trick to expand and display groups. */
910 answer
= strlist_pop (&backlog
);
914 /* Show the list of already collected recipients and ask
919 tty_printf(_("Current recipients:\n"));
920 for (iter
=pk_list
;iter
;iter
=iter
->next
)
924 keyid_from_pk(iter
->pk
,keyid
);
925 tty_printf("%4u%c/%s %s \"",
926 nbits_from_pk(iter
->pk
),
927 pubkey_letter(iter
->pk
->pubkey_algo
),
929 datestr_from_pk(iter
->pk
));
931 if (iter
->pk
->user_id
)
932 tty_print_utf8_string(iter
->pk
->user_id
->name
,
933 iter
->pk
->user_id
->len
);
937 char *p
= get_user_id( keyid
, &n
);
938 tty_print_utf8_string( p
, n
);
944 answer
= cpr_get_utf8("pklist.user_id.enter",
945 _("\nEnter the user ID. "
946 "End with an empty line: "));
951 if ( !answer
|| !*answer
)
954 break; /* No more recipients entered - get out of loop. */
957 /* Do group expand here too. The trick here is to continue
958 the loop if any expansion occured. The code above will
959 then list all expanded keys. */
960 if (expand_id(answer
,&backlog
,0))
963 /* Get and check key for the current name. */
965 free_public_key (pk
);
966 pk
= xmalloc_clear( sizeof *pk
);
968 rc
= get_pubkey_byname (NULL
, pk
, answer
, NULL
, NULL
, 0, 0 );
970 tty_printf(_("No such user ID.\n"));
971 else if ( !(rc
=openpgp_pk_test_algo2 (pk
->pubkey_algo
, use
)) )
975 /* No validation for a default recipient. */
976 if (!key_present_in_pk_list(pk_list
, pk
))
978 free_public_key (pk
); pk
= NULL
;
979 log_info (_("skipped: public key "
980 "already set as default recipient\n") );
984 PK_LIST r
= xmalloc (sizeof *r
);
985 r
->pk
= pk
; pk
= NULL
;
987 r
->flags
= 0; /* No throwing default ids. */
994 { /* Check validity of this key. */
997 trustlevel
= get_validity (pk
, pk
->user_id
);
998 if ( (trustlevel
& TRUST_FLAG_DISABLED
) )
1000 tty_printf (_("Public key is disabled.\n") );
1002 else if ( do_we_trust_pre (pk
, trustlevel
) )
1004 /* Skip the actual key if the key is already
1005 * present in the list */
1006 if (!key_present_in_pk_list(pk_list
, pk
))
1008 free_public_key(pk
); pk
= NULL
;
1009 log_info(_("skipped: public key already set\n") );
1014 r
= xmalloc( sizeof *r
);
1015 r
->pk
= pk
; pk
= NULL
;
1017 r
->flags
= 0; /* No throwing interactive ids. */
1025 xfree(def_rec
); def_rec
= NULL
;
1030 free_public_key( pk
);
1034 else if ( !any_recipients
&& (def_rec
= default_recipient()) )
1036 /* We are in batch mode and have only a default recipient. */
1037 pk
= xmalloc_clear( sizeof *pk
);
1038 pk
->req_usage
= use
;
1040 /* The default recipient is allowed to be disabled; thus pass 1
1041 as second last argument. We also don't want an AKL. */
1042 rc
= get_pubkey_byname (NULL
, pk
, def_rec
, NULL
, NULL
, 1, 1);
1044 log_error(_("unknown default recipient \"%s\"\n"), def_rec
);
1045 else if ( !(rc
=openpgp_pk_test_algo2(pk
->pubkey_algo
, use
)) )
1047 /* Mark any_recipients here since the default recipient
1048 would have been used if it wasn't already there. It
1049 doesn't really matter if we got this key from the default
1050 recipient or an encrypt-to. */
1052 if (!key_present_in_pk_list(pk_list
, pk
))
1053 log_info (_("skipped: public key already set "
1054 "as default recipient\n"));
1057 PK_LIST r
= xmalloc( sizeof *r
);
1058 r
->pk
= pk
; pk
= NULL
;
1060 r
->flags
= 0; /* No throwing default ids. */
1066 free_public_key( pk
);
1069 xfree(def_rec
); def_rec
= NULL
;
1073 /* General case: Check all keys. */
1075 for (; remusr
; remusr
= remusr
->next
)
1077 if ( (remusr
->flags
& 1) )
1078 continue; /* encrypt-to keys are already handled. */
1080 pk
= xmalloc_clear( sizeof *pk
);
1081 pk
->req_usage
= use
;
1082 if ((rc
= get_pubkey_byname (NULL
, pk
, remusr
->d
, NULL
, NULL
, 0, 0)))
1084 /* Key not found or other error. */
1085 free_public_key( pk
); pk
= NULL
;
1086 log_error(_("%s: skipped: %s\n"), remusr
->d
, g10_errstr(rc
) );
1087 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
1088 remusr
->d
, strlen (remusr
->d
),
1092 else if ( !(rc
=openpgp_pk_test_algo2(pk
->pubkey_algo
, use
)) )
1094 /* Key found and usable. Check validity. */
1097 trustlevel
= get_validity (pk
, pk
->user_id
);
1098 if ( (trustlevel
& TRUST_FLAG_DISABLED
) )
1100 /*Key has been disabled. */
1101 free_public_key(pk
); pk
= NULL
;
1102 log_info(_("%s: skipped: public key is disabled\n"),
1104 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
1108 rc
=G10ERR_UNU_PUBKEY
;
1111 else if ( do_we_trust_pre( pk
, trustlevel
) )
1113 /* Note: do_we_trust may have changed the trustlevel */
1115 /* We have at least one valid recipient. It doesn't
1116 * matters if this recipient is already present. */
1119 /* Skip the actual key if the key is already present
1121 if (!key_present_in_pk_list(pk_list
, pk
))
1123 free_public_key(pk
); pk
= NULL
;
1124 log_info(_("%s: skipped: public key already present\n"),
1130 r
= xmalloc( sizeof *r
);
1131 r
->pk
= pk
; pk
= NULL
;
1133 r
->flags
= (remusr
->flags
&2)?1:0;
1138 { /* We don't trust this key. */
1139 free_public_key( pk
); pk
= NULL
;
1140 write_status_text_and_buffer (STATUS_INV_RECP
, "10 ",
1144 rc
=G10ERR_UNU_PUBKEY
;
1150 /* Key found but not usable for us (e.g. sign-only key). */
1151 free_public_key( pk
); pk
= NULL
;
1152 write_status_text_and_buffer (STATUS_INV_RECP
, "0 ",
1156 log_error(_("%s: skipped: %s\n"), remusr
->d
, g10_errstr(rc
) );
1162 if ( !rc
&& !any_recipients
)
1164 log_error(_("no valid addressees\n"));
1165 write_status_text (STATUS_NO_RECP
, "0");
1166 rc
= G10ERR_NO_USER_ID
;
1172 release_pk_list( pk_list
);
1174 *ret_pk_list
= pk_list
;
1176 free_strlist(remusr
);
1181 /* In pgp6 mode, disallow all ciphers except IDEA (1), 3DES (2), and
1182 CAST5 (3), all hashes except MD5 (1), SHA1 (2), and RIPEMD160 (3),
1183 and all compressions except none (0) and ZIP (1). pgp7 and pgp8
1184 mode expands the cipher list to include AES128 (7), AES192 (8),
1185 AES256 (9), and TWOFISH (10). pgp8 adds the SHA-256 hash (8). For
1186 a true PGP key all of this is unneeded as they are the only items
1187 present in the preferences subpacket, but checking here covers the
1188 weird case of encrypting to a key that had preferences from a
1189 different implementation which was then used with PGP. I am not
1190 completely comfortable with this as the right thing to do, as it
1191 slightly alters the list of what the user is supposedly requesting.
1192 It is not against the RFC however, as the preference chosen will
1193 never be one that the user didn't specify somewhere ("The
1194 implementation may use any mechanism to pick an algorithm in the
1195 intersection"), and PGP has no mechanism to fix such a broken
1196 preference list, so I'm including it. -dms */
1199 algo_available( preftype_t preftype
, int algo
, const union pref_hint
*hint
)
1201 if( preftype
== PREFTYPE_SYM
)
1203 if(PGP6
&& (algo
!= CIPHER_ALGO_IDEA
1204 && algo
!= CIPHER_ALGO_3DES
1205 && algo
!= CIPHER_ALGO_CAST5
))
1208 if(PGP7
&& (algo
!= CIPHER_ALGO_IDEA
1209 && algo
!= CIPHER_ALGO_3DES
1210 && algo
!= CIPHER_ALGO_CAST5
1211 && algo
!= CIPHER_ALGO_AES
1212 && algo
!= CIPHER_ALGO_AES192
1213 && algo
!= CIPHER_ALGO_AES256
1214 && algo
!= CIPHER_ALGO_TWOFISH
))
1217 /* PGP8 supports all the ciphers we do.. */
1219 return algo
&& !openpgp_cipher_test_algo ( algo
);
1221 else if( preftype
== PREFTYPE_HASH
)
1223 if (hint
&& hint
->digest_length
)
1225 if (hint
->digest_length
!=20 || opt
.flags
.dsa2
)
1227 /* If --enable-dsa2 is set or the hash isn't 160 bits
1228 (which implies DSA2), then we'll accept a hash that
1229 is larger than we need. Otherwise we won't accept
1230 any hash that isn't exactly the right size. */
1231 if (hint
->digest_length
> gcry_md_get_algo_dlen (algo
))
1234 else if (hint
->digest_length
!= gcry_md_get_algo_dlen (algo
))
1238 if((PGP6
|| PGP7
) && (algo
!= DIGEST_ALGO_MD5
1239 && algo
!= DIGEST_ALGO_SHA1
1240 && algo
!= DIGEST_ALGO_RMD160
))
1244 if(PGP8
&& (algo
!= DIGEST_ALGO_MD5
1245 && algo
!= DIGEST_ALGO_SHA1
1246 && algo
!= DIGEST_ALGO_RMD160
1247 && algo
!= DIGEST_ALGO_SHA256
))
1250 return algo
&& !openpgp_md_test_algo (algo
);
1252 else if( preftype
== PREFTYPE_ZIP
)
1254 if((PGP6
|| PGP7
) && (algo
!= COMPRESS_ALGO_NONE
1255 && algo
!= COMPRESS_ALGO_ZIP
))
1258 /* PGP8 supports all the compression algos we do */
1260 return !check_compress_algo( algo
);
1269 * Return -1 if we could not find an algorithm.
1272 select_algo_from_prefs(PK_LIST pk_list
, int preftype
, int request
,
1273 const union pref_hint
*hint
)
1277 const prefitem_t
*prefs
;
1285 memset( bits
, ~0, 8 * sizeof *bits
);
1286 for( pkr
= pk_list
; pkr
; pkr
= pkr
->next
) {
1289 memset( mask
, 0, 8 * sizeof *mask
);
1290 if( preftype
== PREFTYPE_SYM
) {
1292 pkr
->pk
->version
< 4 &&
1293 pkr
->pk
->selfsigversion
< 4 )
1294 mask
[0] |= (1<<1); /* IDEA is implicitly there for v3 keys
1295 with v3 selfsigs (rfc2440:12.1) if
1296 --pgp2 mode is on. This doesn't
1297 mean it's actually available, of
1300 mask
[0] |= (1<<2); /* 3DES is implicitly there for everyone else */
1302 else if( preftype
== PREFTYPE_HASH
) {
1303 /* While I am including this code for completeness, note
1304 that currently --pgp2 mode locks the hash at MD5, so this
1305 function will never even be called. Even if the hash
1306 wasn't locked at MD5, we don't support sign+encrypt in
1307 --pgp2 mode, and that's the only time PREFTYPE_HASH is
1308 used anyway. -dms */
1310 pkr
->pk
->version
< 4 &&
1311 pkr
->pk
->selfsigversion
< 4 )
1312 mask
[0] |= (1<<1); /* MD5 is there for v3 keys with v3
1313 selfsigs when --pgp2 is on. */
1315 mask
[0] |= (1<<2); /* SHA1 is there for everyone else */
1317 else if( preftype
== PREFTYPE_ZIP
)
1318 mask
[0] |= (1<<0); /* Uncompressed is implicit */
1320 if (pkr
->pk
->user_id
) /* selected by user ID */
1321 prefs
= pkr
->pk
->user_id
->prefs
;
1323 prefs
= pkr
->pk
->prefs
;
1327 for (i
=0; prefs
[i
].type
; i
++ ) {
1328 if( prefs
[i
].type
== preftype
) {
1329 mask
[prefs
[i
].value
/32] |= 1 << (prefs
[i
].value
%32);
1335 if( (!prefs
|| !any
) && preftype
== PREFTYPE_ZIP
) {
1336 mask
[0] |= 3; /* asume no_compression and old pgp */
1341 log_debug("pref mask=%08lX%08lX%08lX%08lX%08lX%08lX%08lX%08lX (%s)\n",
1342 (ulong
)mask
[7], (ulong
)mask
[6], (ulong
)mask
[5], (ulong
)mask
[4],
1343 (ulong
)mask
[3], (ulong
)mask
[2], (ulong
)mask
[1], (ulong
)mask
[0],
1344 keystr_from_pk (pkr
->pk
));
1346 for(i
=0; i
< 8; i
++ )
1349 log_debug("pref bits=%08lX%08lX%08lX%08lX%08lX%08lX%08lX%08lX\n",
1350 (ulong
)bits
[7], (ulong
)bits
[6], (ulong
)bits
[5], (ulong
)bits
[4],
1351 (ulong
)bits
[3], (ulong
)bits
[2], (ulong
)bits
[1], (ulong
)bits
[0]);
1354 /* usable algorithms are now in bits
1355 * We now use the last key from pk_list to select
1356 * the algorithm we want to use. there are no
1357 * preferences for the last key, we select the one
1358 * corresponding to first set bit.
1363 /* Can we use the requested algorithm? */
1364 if(request
>-1 && (bits
[request
/32] & (1<<(request
%32))) &&
1365 algo_available(preftype
,request
,hint
))
1368 /* If we have personal prefs set, use them instead of the last key */
1369 if(preftype
==PREFTYPE_SYM
&& opt
.personal_cipher_prefs
)
1370 prefs
=opt
.personal_cipher_prefs
;
1371 else if(preftype
==PREFTYPE_HASH
&& opt
.personal_digest_prefs
)
1372 prefs
=opt
.personal_digest_prefs
;
1373 else if(preftype
==PREFTYPE_ZIP
&& opt
.personal_compress_prefs
)
1374 prefs
=opt
.personal_compress_prefs
;
1377 for(j
=0; prefs
[j
].type
; j
++ ) {
1378 if( prefs
[j
].type
== preftype
) {
1379 if( (bits
[prefs
[j
].value
/32] & (1<<(prefs
[j
].value
%32))) ) {
1380 if( algo_available( preftype
, prefs
[j
].value
, hint
) ) {
1389 if( !prefs
|| !any
) {
1390 for(j
=0; j
< 256; j
++ )
1391 if( (bits
[j
/32] & (1<<(j
%32))) ) {
1392 if( algo_available( preftype
, j
, hint
) ) {
1400 log_debug("prefs of type %d: selected %d\n", preftype
, i
);
1402 if( compr_hack
&& !i
) {
1403 /* selected no compression, but we should check whether
1404 * algorithm 1 is also available (the ordering is not relevant
1406 if( bits
[0] & (1<<1) )
1407 i
= 1; /* yep; we can use compression algo 1 */
1410 /* "If you are building an authentication system, the recipient
1411 may specify a preferred signing algorithm. However, the signer
1412 would be foolish to use a weak algorithm simply because the
1413 recipient requests it." RFC2440:13. If we settle on MD5, and
1414 SHA1 is also available, use SHA1 instead. Of course, if the
1415 user intentionally chose MD5 (by putting it in their personal
1416 prefs), then we should do what they say. */
1418 if(preftype
==PREFTYPE_HASH
&&
1419 i
==DIGEST_ALGO_MD5
&& (bits
[0] & (1<<DIGEST_ALGO_SHA1
)))
1423 if(opt
.personal_digest_prefs
)
1424 for(j
=0; prefs
[j
].type
; j
++ )
1425 if(opt
.personal_digest_prefs
[j
].type
==PREFTYPE_HASH
&&
1426 opt
.personal_digest_prefs
[j
].value
==DIGEST_ALGO_MD5
)
1437 * Select the MDC flag from the pk_list. We can only use MDC if all
1438 * recipients support this feature.
1441 select_mdc_from_pklist (PK_LIST pk_list
)
1448 for (pkr
= pk_list
; pkr
; pkr
= pkr
->next
)
1452 if (pkr
->pk
->user_id
) /* selected by user ID */
1453 mdc
= pkr
->pk
->user_id
->flags
.mdc
;
1455 mdc
= pkr
->pk
->mdc_feature
;
1457 return 0; /* At least one recipient does not support it. */
1459 return 1; /* Can be used. */
1463 /* Print a warning for all keys in PK_LIST missing the MDC feature. */
1465 warn_missing_mdc_from_pklist (PK_LIST pk_list
)
1469 for (pkr
= pk_list
; pkr
; pkr
= pkr
->next
)
1473 if (pkr
->pk
->user_id
) /* selected by user ID */
1474 mdc
= pkr
->pk
->user_id
->flags
.mdc
;
1476 mdc
= pkr
->pk
->mdc_feature
;
1478 log_info (_("Note: key %s has no %s feature\n"),
1479 keystr_from_pk (pkr
->pk
), "MDC");
1484 warn_missing_aes_from_pklist (PK_LIST pk_list
)
1488 for (pkr
= pk_list
; pkr
; pkr
= pkr
->next
)
1490 const prefitem_t
*prefs
;
1494 prefs
= pkr
->pk
->user_id
? pkr
->pk
->user_id
->prefs
: pkr
->pk
->prefs
;
1497 for (i
=0; !gotit
&& prefs
[i
].type
; i
++ )
1498 if (prefs
[i
].type
== PREFTYPE_SYM
1499 && prefs
[i
].value
== CIPHER_ALGO_AES
)
1503 log_info (_("Note: key %s has no preference for %s\n"),
1504 keystr_from_pk (pkr
->pk
), "AES");