1 /* pkclist.c - create a list of public keys
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 * 2008, 2009, 2010 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 <https://www.gnu.org/licenses/>.
41 #define CONTROL_D ('D' - 'A' + 1)
44 send_status_inv_recp (int reason, const char *name)
48 snprintf (buf, sizeof buf, "%d ", reason);
49 write_status_text_and_buffer (STATUS_INV_RECP, buf,
56 * Show the revocation reason as it is stored with the given signature
59 do_show_revocation_reason( PKT_signature *sig )
66 while( (p = enum_sig_subpkt (sig->hashed, SIGSUBPKT_REVOC_REASON,
69 continue; /* invalid - just skip it */
72 text = _("No reason specified");
74 text = _("Key is superseded");
76 text = _("Key has been compromised");
78 text = _("Key is no longer used");
80 text = _("User ID is no longer valid");
84 log_info ( _("reason for revocation: "));
86 log_printf ("%s\n", text);
88 log_printf ("code=%02x\n", *p );
92 /* We don't want any empty lines, so skip them */
93 while( n && *p == '\n' ) {
98 pp = memchr( p, '\n', n );
100 log_info ( _("revocation comment: ") );
101 es_write_sanitized (log_get_stream(), p, nn, NULL, NULL);
109 /* Mode 0: try and find the revocation based on the pk (i.e. check
110 subkeys, etc.) Mode 1: use only the revocation on the main pk */
113 show_revocation_reason( PKT_public_key *pk, int mode )
115 /* Hmmm, this is not so easy because we have to duplicate the code
116 * used in the trustbd to calculate the keyflags. We need to find
117 * a clean way to check revocation certificates on keys and
118 * signatures. And there should be no duplicate code. Because we
119 * enter this function only when the trustdb told us that we have
120 * a revoked key, we could simply look for a revocation cert and
121 * display this one, when there is only one. Let's try to do this
122 * until we have a better solution. */
123 KBNODE node, keyblock = NULL;
124 byte fingerprint[MAX_FINGERPRINT_LEN];
128 /* get the keyblock */
129 fingerprint_from_pk( pk, fingerprint, &fingerlen );
130 rc = get_pubkey_byfprint(NULL, &keyblock, fingerprint, fingerlen);
131 if( rc ) { /* that should never happen */
132 log_debug( "failed to get the keyblock\n");
136 for( node=keyblock; node; node = node->next ) {
137 if( (mode && node->pkt->pkttype == PKT_PUBLIC_KEY) ||
138 ( ( node->pkt->pkttype == PKT_PUBLIC_KEY
139 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
140 && !cmp_public_keys( node->pkt->pkt.public_key, pk ) ) )
144 log_debug("Oops, PK not in keyblock\n");
145 release_kbnode( keyblock );
148 /* now find the revocation certificate */
149 for( node = node->next; node ; node = node->next ) {
150 if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
152 if( node->pkt->pkttype == PKT_SIGNATURE
153 && (node->pkt->pkt.signature->sig_class == 0x20
154 || node->pkt->pkt.signature->sig_class == 0x28 ) ) {
155 /* FIXME: we should check the signature here */
156 do_show_revocation_reason ( node->pkt->pkt.signature );
161 /* We didn't find it, so check if the whole key is revoked */
163 show_revocation_reason(pk,1);
165 release_kbnode( keyblock );
171 * 1 = Without key info and additional menu option 'm'
172 * this does also add an option to set the key to ultimately trusted.
174 * -2 = nothing changed - caller should show some additional info
175 * -1 = quit operation
176 * 0 = nothing changed
177 * 1 = new ownertrust now in new_trust
179 #ifndef NO_TRUST_MODELS
181 do_edit_ownertrust (ctrl_t ctrl, PKT_public_key *pk, int mode,
182 unsigned *new_trust, int defer_help )
190 int did_help=defer_help;
191 unsigned int minimum = tdb_get_min_ownertrust (pk);
196 case TRUST_UNDEFINED: min_num=1; break;
197 case TRUST_NEVER: min_num=2; break;
198 case TRUST_MARGINAL: min_num=3; break;
199 case TRUST_FULLY: min_num=4; break;
202 keyid_from_pk (pk, keyid);
204 /* A string with valid answers.
206 TRANSLATORS: These are the allowed answers in lower and
207 uppercase. Below you will find the matching strings which
208 should be translated accordingly and the letter changed to
209 match the one in the answer string.
211 i = please show me more information
212 m = back to the main menu
216 const char *ans = _("iImMqQsS");
224 tty_printf (_("No trust value assigned to:\n"));
225 print_key_line (NULL, pk, 0);
227 p = get_user_id_native(keyid);
228 tty_printf (_(" \"%s\"\n"),p);
231 keyblock = get_pubkeyblock (keyid);
234 for (un=keyblock; un; un = un->next)
236 if (un->pkt->pkttype != PKT_USER_ID )
238 if (un->pkt->pkt.user_id->is_revoked )
240 if (un->pkt->pkt.user_id->is_expired )
242 /* Only skip textual primaries */
243 if (un->pkt->pkt.user_id->is_primary
244 && !un->pkt->pkt.user_id->attrib_data )
247 if((opt.verify_options&VERIFY_SHOW_PHOTOS)
248 && un->pkt->pkt.user_id->attrib_data)
250 un->pkt->pkt.user_id->attribs,
251 un->pkt->pkt.user_id->numattribs, pk,
252 un->pkt->pkt.user_id);
254 p=utf8_to_native(un->pkt->pkt.user_id->name,
255 un->pkt->pkt.user_id->len,0);
257 tty_printf(_(" aka \"%s\"\n"),p);
260 print_fingerprint (NULL, pk, 2);
262 release_kbnode (keyblock);
265 if(opt.trust_model==TM_DIRECT)
267 tty_printf(_("How much do you trust that this key actually "
268 "belongs to the named user?\n"));
273 /* This string also used in keyedit.c:trustsig_prompt */
274 tty_printf(_("Please decide how far you trust this user to"
275 " correctly verify other users' keys\n"
276 "(by looking at passports, checking fingerprints from"
277 " different sources, etc.)\n"));
282 tty_printf (_(" %d = I don't know or won't say\n"), 1);
284 tty_printf (_(" %d = I do NOT trust\n"), 2);
286 tty_printf (_(" %d = I trust marginally\n"), 3);
288 tty_printf (_(" %d = I trust fully\n"), 4);
290 tty_printf (_(" %d = I trust ultimately\n"), 5);
292 /* not yet implemented */
293 tty_printf (" i = please show me more information\n");
296 tty_printf(_(" m = back to the main menu\n"));
299 tty_printf(_(" s = skip this key\n"));
300 tty_printf(_(" q = quit\n"));
304 tty_printf(_("The minimum trust level for this key is: %s\n\n"),
305 trust_value_to_string(minimum));
308 if( strlen(ans) != 8 )
310 p = cpr_get("edit_ownertrust.value",_("Your decision? "));
315 else if( *p && p[1] )
317 else if( !p[1] && ((*p >= '0'+min_num) && *p <= (mode?'5':'4')) )
322 case '1': trust = TRUST_UNDEFINED; break;
323 case '2': trust = TRUST_NEVER ; break;
324 case '3': trust = TRUST_MARGINAL ; break;
325 case '4': trust = TRUST_FULLY ; break;
326 case '5': trust = TRUST_ULTIMATE ; break;
329 if (trust == TRUST_ULTIMATE
330 && !cpr_get_answer_is_yes ("edit_ownertrust.set_ultimate.okay",
331 _("Do you really want to set this key"
332 " to ultimate trust? (y/N) ")))
342 /* not yet implemented */
343 else if( *p == ans[0] || *p == ans[1] )
345 tty_printf(_("Certificates leading to an ultimately trusted key:\n"));
350 else if( mode && (*p == ans[2] || *p == ans[3] || *p == CONTROL_D ) )
352 break ; /* back to the menu */
354 else if( !mode && (*p == ans[6] || *p == ans[7] ) )
358 else if( !mode && (*p == ans[4] || *p == ans[5] ) )
361 break ; /* back to the menu */
366 return show? -2: quit? -1 : changed;
368 #endif /*!NO_TRUST_MODELS*/
372 * Display a menu to change the ownertrust of the key PK (which should
374 * For mode values see do_edit_ownertrust ()
376 #ifndef NO_TRUST_MODELS
378 edit_ownertrust (ctrl_t ctrl, PKT_public_key *pk, int mode )
380 unsigned int trust = 0;
385 switch ( do_edit_ownertrust (ctrl, pk, mode, &trust, no_help ) )
389 case -2: /* show info */
392 case 1: /* trust value set */
393 trust &= ~TRUST_FLAG_DISABLED;
394 trust |= get_ownertrust (pk) & TRUST_FLAG_DISABLED;
395 update_ownertrust (pk, trust );
402 #endif /*!NO_TRUST_MODELS*/
406 * Check whether we can trust this pk which has a trustlevel of TRUSTLEVEL
407 * Returns: true if we trust.
410 do_we_trust( PKT_public_key *pk, unsigned int trustlevel )
412 /* We should not be able to get here with a revoked or expired
414 if(trustlevel & TRUST_FLAG_REVOKED
415 || trustlevel & TRUST_FLAG_SUB_REVOKED
416 || (trustlevel & TRUST_MASK) == TRUST_EXPIRED)
419 if( opt.trust_model==TM_ALWAYS )
422 log_info("No trust check due to '--trust-model always' option\n");
426 switch(trustlevel & TRUST_MASK)
429 log_error ("invalid trustlevel %u returned from validation layer\n",
433 case TRUST_UNDEFINED:
434 log_info(_("%s: There is no assurance this key belongs"
435 " to the named user\n"),keystr_from_pk(pk));
439 log_info(_("%s: There is limited assurance this key belongs"
440 " to the named user\n"),keystr_from_pk(pk));
445 log_info(_("This key probably belongs to the named user\n"));
450 log_info(_("This key belongs to us\n"));
454 /* This can be returned by TOFU, which can return negative
456 log_info(_("%s: This key is bad! It has been marked as untrusted!\n"),
461 return 1; /*NOTREACHED*/
466 * wrapper around do_we_trust, so we can ask whether to use the
470 do_we_trust_pre( PKT_public_key *pk, unsigned int trustlevel )
474 rc = do_we_trust( pk, trustlevel );
476 if( !opt.batch && !rc )
478 print_pubkey_info(NULL,pk);
479 print_fingerprint (NULL, pk, 2);
482 if ((trustlevel & TRUST_MASK) == TRUST_NEVER)
484 _("This key is bad! It has been marked as untrusted! If you\n"
485 "*really* know what you are doing, you may answer the next\n"
486 "question with yes.\n"));
489 _("It is NOT certain that the key belongs to the person named\n"
490 "in the user ID. If you *really* know what you are doing,\n"
491 "you may answer the next question with yes.\n"));
496 if (is_status_enabled ())
501 keyid_from_pk (pk, kid);
502 hint_str = get_long_user_id_string ( kid );
503 write_status_text ( STATUS_USERID_HINT, hint_str );
507 if( cpr_get_answer_is_yes("untrusted_key.override",
508 _("Use this key anyway? (y/N) ")) )
511 /* Hmmm: Should we set a flag to tell the user about
512 * his decision the next time he encrypts for this recipient?
520 /* Write a TRUST_foo status line inclduing the validation model. */
522 write_trust_status (int statuscode, int trustlevel)
524 #ifdef NO_TRUST_MODELS
525 write_status (statuscode);
526 #else /* NO_TRUST_MODELS */
529 /* For the combined tofu+pgp method, we return the trust model which
530 * was responsible for the trustlevel. */
531 if (opt.trust_model == TM_TOFU_PGP)
532 tm = (trustlevel & TRUST_FLAG_TOFU_BASED)? TM_TOFU : TM_PGP;
534 tm = opt.trust_model;
535 write_status_strings (statuscode, "0 ", trust_model_string (tm), NULL);
536 #endif /* NO_TRUST_MODELS */
541 * Check whether we can trust this signature.
542 * Returns an error code if we should not trust this signature.
545 check_signatures_trust (ctrl_t ctrl, PKT_signature *sig)
547 PKT_public_key *pk = xmalloc_clear( sizeof *pk );
548 unsigned int trustlevel = TRUST_UNKNOWN;
551 rc = get_pubkey( pk, sig->keyid );
553 { /* this should not happen */
554 log_error("Ooops; the key vanished - can't check the trust\n");
555 rc = GPG_ERR_NO_PUBKEY;
559 if ( opt.trust_model==TM_ALWAYS )
562 log_info(_("WARNING: Using untrusted key!\n"));
563 if (opt.with_fingerprint)
564 print_fingerprint (NULL, pk, 1);
568 if(pk->flags.maybe_revoked && !pk->flags.revoked)
569 log_info(_("WARNING: this key might be revoked (revocation key"
572 trustlevel = get_validity (ctrl, NULL, pk, NULL, sig, 1);
574 if ( (trustlevel & TRUST_FLAG_REVOKED) )
576 write_status( STATUS_KEYREVOKED );
577 if(pk->flags.revoked == 2)
578 log_info(_("WARNING: This key has been revoked by its"
579 " designated revoker!\n"));
581 log_info(_("WARNING: This key has been revoked by its owner!\n"));
582 log_info(_(" This could mean that the signature is forged.\n"));
583 show_revocation_reason( pk, 0 );
585 else if ((trustlevel & TRUST_FLAG_SUB_REVOKED) )
587 write_status( STATUS_KEYREVOKED );
588 log_info(_("WARNING: This subkey has been revoked by its owner!\n"));
589 show_revocation_reason( pk, 0 );
592 if ((trustlevel & TRUST_FLAG_DISABLED))
593 log_info (_("Note: This key has been disabled.\n"));
595 /* If we have PKA information adjust the trustlevel. */
596 if (sig->pka_info && sig->pka_info->valid)
598 unsigned char fpr[MAX_FINGERPRINT_LEN];
599 PKT_public_key *primary_pk;
604 primary_pk = xmalloc_clear (sizeof *primary_pk);
605 get_pubkey (primary_pk, pk->main_keyid);
606 fingerprint_from_pk (primary_pk, fpr, &fprlen);
607 free_public_key (primary_pk);
609 if ( fprlen == 20 && !memcmp (sig->pka_info->fpr, fpr, 20) )
612 write_status_text (STATUS_PKA_TRUST_GOOD, sig->pka_info->email);
613 log_info (_("Note: Verified signer's address is '%s'\n"),
614 sig->pka_info->email);
619 write_status_text (STATUS_PKA_TRUST_BAD, sig->pka_info->email);
620 log_info (_("Note: Signer's address '%s' "
621 "does not match DNS entry\n"), sig->pka_info->email);
624 switch ( (trustlevel & TRUST_MASK) )
627 case TRUST_UNDEFINED:
629 if (okay && opt.verify_options&VERIFY_PKA_TRUST_INCREASE)
631 trustlevel = ((trustlevel & ~TRUST_MASK) | TRUST_FULLY);
632 log_info (_("trustlevel adjusted to FULL"
633 " due to valid PKA info\n"));
639 trustlevel = ((trustlevel & ~TRUST_MASK) | TRUST_NEVER);
640 log_info (_("trustlevel adjusted to NEVER"
641 " due to bad PKA info\n"));
647 /* Now let the user know what up with the trustlevel. */
648 switch ( (trustlevel & TRUST_MASK) )
651 log_info(_("Note: This key has expired!\n"));
652 print_fingerprint (NULL, pk, 1);
656 log_error ("invalid trustlevel %u returned from validation layer\n",
660 case TRUST_UNDEFINED:
661 write_trust_status (STATUS_TRUST_UNDEFINED, trustlevel);
662 log_info(_("WARNING: This key is not certified with"
663 " a trusted signature!\n"));
664 log_info(_(" There is no indication that the "
665 "signature belongs to the owner.\n" ));
666 print_fingerprint (NULL, pk, 1);
670 /* This level can be returned by TOFU, which supports negative
672 write_trust_status (STATUS_TRUST_NEVER, trustlevel);
673 log_info(_("WARNING: We do NOT trust this key!\n"));
674 log_info(_(" The signature is probably a FORGERY.\n"));
675 if (opt.with_fingerprint)
676 print_fingerprint (NULL, pk, 1);
677 rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
681 write_trust_status (STATUS_TRUST_MARGINAL, trustlevel);
682 log_info(_("WARNING: This key is not certified with"
683 " sufficiently trusted signatures!\n"));
684 log_info(_(" It is not certain that the"
685 " signature belongs to the owner.\n" ));
686 print_fingerprint (NULL, pk, 1);
690 write_trust_status (STATUS_TRUST_FULLY, trustlevel);
691 if (opt.with_fingerprint)
692 print_fingerprint (NULL, pk, 1);
696 write_trust_status (STATUS_TRUST_ULTIMATE, trustlevel);
697 if (opt.with_fingerprint)
698 print_fingerprint (NULL, pk, 1);
703 free_public_key( pk );
709 release_pk_list (pk_list_t pk_list)
713 for ( ; pk_list; pk_list = pk_rover)
715 pk_rover = pk_list->next;
716 free_public_key ( pk_list->pk );
723 key_present_in_pk_list(PK_LIST pk_list, PKT_public_key *pk)
725 for( ; pk_list; pk_list = pk_list->next)
726 if (cmp_public_keys(pk_list->pk, pk) == 0)
734 * Return a malloced string with a default recipient if there is any
737 default_recipient(ctrl_t ctrl)
740 byte fpr[MAX_FINGERPRINT_LEN+1];
745 if( opt.def_recipient )
746 return xstrdup( opt.def_recipient );
747 if( !opt.def_recipient_self )
749 pk = xmalloc_clear( sizeof *pk );
750 i = get_seckey_default (ctrl, pk);
752 free_public_key( pk );
755 n = MAX_FINGERPRINT_LEN;
756 fingerprint_from_pk( pk, fpr, &n );
757 free_public_key( pk );
758 p = xmalloc( 2*n+3 );
761 for(i=0; i < n; i++ )
762 sprintf( p+2*i, "%02X", fpr[i] );
768 expand_id(const char *id,strlist_t *into,unsigned int flags)
770 struct groupitem *groups;
773 for(groups=opt.grouplist;groups;groups=groups->next)
775 /* need strcasecmp() here, as this should be localized */
776 if(strcasecmp(groups->name,id)==0)
780 /* this maintains the current utf8-ness */
781 for(each=groups->values;each;each=each->next)
783 sl=add_to_strlist(into,each->d);
795 /* For simplicity, and to avoid potential loops, we only expand once -
796 * you can't make an alias that points to an alias. */
798 expand_group (strlist_t input)
800 strlist_t output = NULL;
803 for (rover = input; rover; rover = rover->next)
804 if (!(rover->flags & PK_LIST_FROM_FILE)
805 && !expand_id(rover->d,&output,rover->flags))
807 /* Didn't find any groups, so use the existing string */
808 sl=add_to_strlist(&output,rover->d);
809 sl->flags=rover->flags;
816 /* Helper for build_pk_list to find and check one key. This helper is
817 * also used directly in server mode by the RECIPIENTS command. On
818 * success the new key is added to PK_LIST_ADDR. NAME is the user id
819 * of the key. USE the requested usage and a set MARK_HIDDEN will
820 * mark the key in the updated list as a hidden recipient. If
821 * FROM_FILE is true, NAME is is not a user ID but the name of a file
824 find_and_check_key (ctrl_t ctrl, const char *name, unsigned int use,
825 int mark_hidden, int from_file, pk_list_t *pk_list_addr)
831 return gpg_error (GPG_ERR_INV_USER_ID);
833 pk = xtrycalloc (1, sizeof *pk);
835 return gpg_error_from_syserror ();
839 rc = get_pubkey_fromfile (ctrl, pk, name);
841 rc = get_best_pubkey_byname (ctrl, NULL, pk, name, NULL, 0, 0);
846 /* Key not found or other error. */
847 log_error (_("%s: skipped: %s\n"), name, gpg_strerror (rc) );
848 switch (gpg_err_code (rc))
850 case GPG_ERR_NO_SECKEY:
851 case GPG_ERR_NO_PUBKEY: code = 1; break;
852 case GPG_ERR_INV_USER_ID: code = 14; break;
853 default: code = 0; break;
855 send_status_inv_recp (code, name);
856 free_public_key (pk);
860 rc = openpgp_pk_test_algo2 (pk->pubkey_algo, use);
863 /* Key found but not usable for us (e.g. sign-only key). */
864 send_status_inv_recp (3, name); /* Wrong key usage */
865 log_error (_("%s: skipped: %s\n"), name, gpg_strerror (rc) );
866 free_public_key (pk);
870 /* Key found and usable. Check validity. */
875 trustlevel = get_validity (ctrl, NULL, pk, pk->user_id, NULL, 1);
876 if ( (trustlevel & TRUST_FLAG_DISABLED) )
878 /* Key has been disabled. */
879 send_status_inv_recp (13, name);
880 log_info (_("%s: skipped: public key is disabled\n"), name);
881 free_public_key (pk);
882 return GPG_ERR_UNUSABLE_PUBKEY;
885 if ( !do_we_trust_pre (pk, trustlevel) )
887 /* We don't trust this key. */
888 send_status_inv_recp (10, name);
889 free_public_key (pk);
890 return GPG_ERR_UNUSABLE_PUBKEY;
894 /* Skip the actual key if the key is already present in the
896 if (!key_present_in_pk_list (*pk_list_addr, pk))
899 log_info (_("%s: skipped: public key already present\n"), name);
900 free_public_key (pk);
906 r = xtrymalloc (sizeof *r);
909 rc = gpg_error_from_syserror ();
910 free_public_key (pk);
914 r->next = *pk_list_addr;
915 r->flags = mark_hidden? 1:0;
924 /* This is the central function to collect the keys for recipients.
925 * It is thus used to prepare a public key encryption. encrypt-to
926 * keys, default keys and the keys for the actual recipients are all
927 * collected here. When not in batch mode and no recipient has been
928 * passed on the commandline, the function will also ask for
931 * RCPTS is a string list with the recipients; NULL is an allowed
932 * value but not very useful. Group expansion is done on these names;
933 * they may be in any of the user Id formats we can handle. The flags
934 * bits for each string in the string list are used for:
936 * - PK_LIST_ENCRYPT_TO :: This is an encrypt-to recipient.
937 * - PK_LIST_HIDDEN :: This is a hidden recipient.
938 * - PK_LIST_FROM_FILE :: The argument is a file with a key.
940 * On success a list of keys is stored at the address RET_PK_LIST; the
941 * caller must free this list. On error the value at this address is
945 build_pk_list (ctrl_t ctrl, strlist_t rcpts, PK_LIST *ret_pk_list)
947 PK_LIST pk_list = NULL;
948 PKT_public_key *pk=NULL;
950 int any_recipients=0;
951 strlist_t rov,remusr;
952 char *def_rec = NULL;
953 char pkstrbuf[PUBKEY_STRING_SIZE];
955 /* Try to expand groups if any have been defined. */
957 remusr = expand_group (rcpts);
961 /* XXX: Change this function to use get_pubkeys instead of
962 get_pubkey_byname to detect ambiguous key specifications and warn
963 about duplicate keyblocks. For ambiguous key specifications on
964 the command line or provided interactively, prompt the user to
965 select the best key. If a key specification is ambiguous and we
966 are in batch mode, die. */
968 if (opt.encrypt_to_default_key)
972 const char *default_key = parse_def_secret_key (ctrl);
975 PK_LIST r = xmalloc_clear (sizeof *r);
977 r->pk = xmalloc_clear (sizeof *r->pk);
978 r->pk->req_usage = PUBKEY_USAGE_ENC;
980 rc = get_pubkey_byname (ctrl, NULL, r->pk, default_key,
987 log_error (_("can't encrypt to '%s'\n"), default_key);
989 log_info (_("(check argument of option '%s')\n"),
999 else if (opt.def_secret_key)
1002 log_info (_("option '%s' given, but no valid default keys given\n"),
1003 "--encrypt-to-default-key");
1009 log_info (_("option '%s' given, but option '%s' not given\n"),
1010 "--encrypt-to-default-key", "--default-key");
1015 /* Check whether there are any recipients in the list and build the
1016 * list of the encrypt-to ones (we always trust them). */
1017 for ( rov = remusr; rov; rov = rov->next )
1019 if ( !(rov->flags & PK_LIST_ENCRYPT_TO) )
1021 /* This is a regular recipient; i.e. not an encrypt-to
1025 /* Hidden recipients are not allowed while in PGP mode,
1026 issue a warning and switch into GnuPG mode. */
1027 if ((rov->flags & PK_LIST_HIDDEN) && (PGP6 || PGP7 || PGP8))
1029 log_info(_("you may not use %s while in %s mode\n"),
1030 "--hidden-recipient",
1031 compliance_option_string());
1033 compliance_failure();
1036 else if (!opt.no_encrypt_to)
1038 /* --encrypt-to has not been disabled. Check this
1040 pk = xmalloc_clear( sizeof *pk );
1041 pk->req_usage = PUBKEY_USAGE_ENC;
1043 /* We explicitly allow encrypt-to to an disabled key; thus
1044 we pass 1 for the second last argument and 1 as the last
1045 argument to disable AKL. */
1046 if ( (rc = get_pubkey_byname (ctrl,
1047 NULL, pk, rov->d, NULL, NULL, 1, 1)) )
1049 free_public_key ( pk ); pk = NULL;
1050 log_error (_("%s: skipped: %s\n"), rov->d, gpg_strerror (rc) );
1051 send_status_inv_recp (0, rov->d);
1054 else if ( !(rc=openpgp_pk_test_algo2 (pk->pubkey_algo,
1055 PUBKEY_USAGE_ENC)) )
1057 /* Skip the actual key if the key is already present
1058 * in the list. Add it to our list if not. */
1059 if (key_present_in_pk_list(pk_list, pk) == 0)
1061 free_public_key (pk); pk = NULL;
1063 log_info (_("%s: skipped: public key already present\n"),
1069 r = xmalloc( sizeof *r );
1070 r->pk = pk; pk = NULL;
1072 r->flags = (rov->flags&PK_LIST_HIDDEN)?1:0;
1075 /* Hidden encrypt-to recipients are not allowed while
1076 in PGP mode, issue a warning and switch into
1078 if ((r->flags&PK_LIST_ENCRYPT_TO) && (PGP6 || PGP7 || PGP8))
1080 log_info(_("you may not use %s while in %s mode\n"),
1081 "--hidden-encrypt-to",
1082 compliance_option_string());
1084 compliance_failure();
1090 /* The public key is not usable for encryption. */
1091 free_public_key( pk ); pk = NULL;
1092 log_error(_("%s: skipped: %s\n"), rov->d, gpg_strerror (rc) );
1093 send_status_inv_recp (3, rov->d); /* Wrong key usage */
1099 /* If we don't have any recipients yet and we are not in batch mode
1100 drop into interactive selection mode. */
1101 if ( !any_recipients && !opt.batch )
1104 char *answer = NULL;
1105 strlist_t backlog = NULL;
1109 def_rec = default_recipient(ctrl);
1110 have_def_rec = !!def_rec;
1111 if ( !have_def_rec )
1112 tty_printf(_("You did not specify a user ID. (you may use \"-r\")\n"));
1120 /* A default recipient is taken as the first entry. */
1126 /* This is part of our trick to expand and display groups. */
1127 answer = strlist_pop (&backlog);
1131 /* Show the list of already collected recipients and ask
1136 tty_printf(_("Current recipients:\n"));
1137 for (iter=pk_list;iter;iter=iter->next)
1141 keyid_from_pk(iter->pk,keyid);
1142 tty_printf ("%s/%s %s \"",
1143 pubkey_string (iter->pk,
1144 pkstrbuf, sizeof pkstrbuf),
1146 datestr_from_pk (iter->pk));
1148 if (iter->pk->user_id)
1149 tty_print_utf8_string(iter->pk->user_id->name,
1150 iter->pk->user_id->len);
1154 char *p = get_user_id( keyid, &n );
1155 tty_print_utf8_string( p, n );
1161 answer = cpr_get_utf8("pklist.user_id.enter",
1162 _("\nEnter the user ID. "
1163 "End with an empty line: "));
1164 trim_spaces(answer);
1168 if ( !answer || !*answer )
1171 break; /* No more recipients entered - get out of loop. */
1174 /* Do group expand here too. The trick here is to continue
1175 the loop if any expansion occurred. The code above will
1176 then list all expanded keys. */
1177 if (expand_id(answer,&backlog,0))
1180 /* Get and check key for the current name. */
1181 free_public_key (pk);
1182 pk = xmalloc_clear( sizeof *pk );
1183 pk->req_usage = PUBKEY_USAGE_ENC;
1184 rc = get_pubkey_byname (ctrl, NULL, pk, answer, NULL, NULL, 0, 0 );
1186 tty_printf(_("No such user ID.\n"));
1187 else if ( !(rc=openpgp_pk_test_algo2 (pk->pubkey_algo,
1188 PUBKEY_USAGE_ENC)) )
1192 /* No validation for a default recipient. */
1193 if (!key_present_in_pk_list(pk_list, pk))
1195 free_public_key (pk);
1197 log_info (_("skipped: public key "
1198 "already set as default recipient\n") );
1202 PK_LIST r = xmalloc (sizeof *r);
1203 r->pk = pk; pk = NULL;
1205 r->flags = 0; /* No throwing default ids. */
1212 { /* Check validity of this key. */
1216 get_validity (ctrl, NULL, pk, pk->user_id, NULL, 1);
1217 if ( (trustlevel & TRUST_FLAG_DISABLED) )
1219 tty_printf (_("Public key is disabled.\n") );
1221 else if ( do_we_trust_pre (pk, trustlevel) )
1223 /* Skip the actual key if the key is already
1224 * present in the list */
1225 if (!key_present_in_pk_list(pk_list, pk))
1227 free_public_key (pk);
1229 log_info(_("skipped: public key already set\n") );
1234 r = xmalloc( sizeof *r );
1235 r->pk = pk; pk = NULL;
1237 r->flags = 0; /* No throwing interactive ids. */
1245 xfree(def_rec); def_rec = NULL;
1250 free_public_key( pk );
1254 else if ( !any_recipients && (def_rec = default_recipient(ctrl)) )
1256 /* We are in batch mode and have only a default recipient. */
1257 pk = xmalloc_clear( sizeof *pk );
1258 pk->req_usage = PUBKEY_USAGE_ENC;
1260 /* The default recipient is allowed to be disabled; thus pass 1
1261 as second last argument. We also don't want an AKL. */
1262 rc = get_pubkey_byname (ctrl, NULL, pk, def_rec, NULL, NULL, 1, 1);
1264 log_error(_("unknown default recipient \"%s\"\n"), def_rec );
1265 else if ( !(rc=openpgp_pk_test_algo2(pk->pubkey_algo,
1266 PUBKEY_USAGE_ENC)) )
1268 /* Mark any_recipients here since the default recipient
1269 would have been used if it wasn't already there. It
1270 doesn't really matter if we got this key from the default
1271 recipient or an encrypt-to. */
1273 if (!key_present_in_pk_list(pk_list, pk))
1274 log_info (_("skipped: public key already set "
1275 "as default recipient\n"));
1278 PK_LIST r = xmalloc( sizeof *r );
1279 r->pk = pk; pk = NULL;
1281 r->flags = 0; /* No throwing default ids. */
1287 free_public_key( pk );
1290 xfree(def_rec); def_rec = NULL;
1294 /* General case: Check all keys. */
1296 for (; remusr; remusr = remusr->next )
1298 if ( (remusr->flags & PK_LIST_ENCRYPT_TO) )
1299 continue; /* encrypt-to keys are already handled. */
1301 rc = find_and_check_key (ctrl, remusr->d, PUBKEY_USAGE_ENC,
1302 !!(remusr->flags&PK_LIST_HIDDEN),
1303 !!(remusr->flags&PK_LIST_FROM_FILE),
1311 if ( !rc && !any_recipients )
1313 log_error(_("no valid addressees\n"));
1314 write_status_text (STATUS_NO_RECP, "0");
1315 rc = GPG_ERR_NO_USER_ID;
1319 if (! rc && (opt.trust_model == TM_TOFU_PGP || opt.trust_model == TM_TOFU))
1322 for (iter = pk_list; iter; iter = iter->next)
1326 /* Note: we already resolved any conflict when looking up
1327 the key. Don't annoy the user again if she selected
1329 rc2 = tofu_register_encryption (ctrl, iter->pk, NULL, 0);
1331 log_info ("WARNING: Failed to register encryption to %s"
1332 " with TOFU engine\n",
1333 keystr (pk_main_keyid (iter->pk)));
1335 log_debug ("Registered encryption to %s with TOFU DB.\n",
1336 keystr (pk_main_keyid (iter->pk)));
1344 release_pk_list( pk_list );
1346 *ret_pk_list = pk_list;
1348 free_strlist(remusr);
1353 /* In pgp6 mode, disallow all ciphers except IDEA (1), 3DES (2), and
1354 CAST5 (3), all hashes except MD5 (1), SHA1 (2), and RIPEMD160 (3),
1355 and all compressions except none (0) and ZIP (1). pgp7 and pgp8
1356 mode expands the cipher list to include AES128 (7), AES192 (8),
1357 AES256 (9), and TWOFISH (10). pgp8 adds the SHA-256 hash (8). For
1358 a true PGP key all of this is unneeded as they are the only items
1359 present in the preferences subpacket, but checking here covers the
1360 weird case of encrypting to a key that had preferences from a
1361 different implementation which was then used with PGP. I am not
1362 completely comfortable with this as the right thing to do, as it
1363 slightly alters the list of what the user is supposedly requesting.
1364 It is not against the RFC however, as the preference chosen will
1365 never be one that the user didn't specify somewhere ("The
1366 implementation may use any mechanism to pick an algorithm in the
1367 intersection"), and PGP has no mechanism to fix such a broken
1368 preference list, so I'm including it. -dms */
1371 algo_available( preftype_t preftype, int algo, const union pref_hint *hint)
1373 if( preftype == PREFTYPE_SYM )
1375 if(PGP6 && (algo != CIPHER_ALGO_IDEA
1376 && algo != CIPHER_ALGO_3DES
1377 && algo != CIPHER_ALGO_CAST5))
1380 if(PGP7 && (algo != CIPHER_ALGO_IDEA
1381 && algo != CIPHER_ALGO_3DES
1382 && algo != CIPHER_ALGO_CAST5
1383 && algo != CIPHER_ALGO_AES
1384 && algo != CIPHER_ALGO_AES192
1385 && algo != CIPHER_ALGO_AES256
1386 && algo != CIPHER_ALGO_TWOFISH))
1389 /* PGP8 supports all the ciphers we do.. */
1391 return algo && !openpgp_cipher_test_algo ( algo );
1393 else if( preftype == PREFTYPE_HASH )
1395 if (hint && hint->digest_length)
1397 if (hint->digest_length!=20 || opt.flags.dsa2)
1399 /* If --enable-dsa2 is set or the hash isn't 160 bits
1400 (which implies DSA2), then we'll accept a hash that
1401 is larger than we need. Otherwise we won't accept
1402 any hash that isn't exactly the right size. */
1403 if (hint->digest_length > gcry_md_get_algo_dlen (algo))
1406 else if (hint->digest_length != gcry_md_get_algo_dlen (algo))
1410 if((PGP6 || PGP7) && (algo != DIGEST_ALGO_MD5
1411 && algo != DIGEST_ALGO_SHA1
1412 && algo != DIGEST_ALGO_RMD160))
1416 if(PGP8 && (algo != DIGEST_ALGO_MD5
1417 && algo != DIGEST_ALGO_SHA1
1418 && algo != DIGEST_ALGO_RMD160
1419 && algo != DIGEST_ALGO_SHA256))
1422 return algo && !openpgp_md_test_algo (algo);
1424 else if( preftype == PREFTYPE_ZIP )
1426 if((PGP6 || PGP7) && (algo != COMPRESS_ALGO_NONE
1427 && algo != COMPRESS_ALGO_ZIP))
1430 /* PGP8 supports all the compression algos we do */
1432 return !check_compress_algo( algo );
1439 * Return -1 if we could not find an algorithm.
1442 select_algo_from_prefs(PK_LIST pk_list, int preftype,
1443 int request, const union pref_hint *hint)
1447 const prefitem_t *prefs;
1454 memset(bits,0xFF,sizeof(bits));
1455 memset(scores,0,sizeof(scores));
1457 for( pkr = pk_list; pkr; pkr = pkr->next )
1460 int rank=1,implicit=-1;
1462 memset(mask,0,sizeof(mask));
1467 /* IDEA is implicitly there for v3 keys with v3 selfsigs if
1468 --pgp2 mode is on. This was a 2440 thing that was
1469 dropped from 4880 but is still relevant to GPG's 1991
1470 support. All this doesn't mean IDEA is actually
1471 available, of course. */
1472 implicit=CIPHER_ALGO_3DES;
1477 /* While I am including this code for completeness, note
1478 that currently --pgp2 mode locks the hash at MD5, so this
1479 code will never even be called. Even if the hash wasn't
1480 locked at MD5, we don't support sign+encrypt in --pgp2
1481 mode, and that's the only time PREFTYPE_HASH is used
1484 implicit=DIGEST_ALGO_SHA1;
1489 /* Uncompressed is always an option. */
1490 implicit=COMPRESS_ALGO_NONE;
1493 if (pkr->pk->user_id) /* selected by user ID */
1494 prefs = pkr->pk->user_id->prefs;
1496 prefs = pkr->pk->prefs;
1500 for (i=0; prefs[i].type; i++ )
1502 if( prefs[i].type == preftype )
1504 /* Make sure all scores don't add up past 0xFFFF
1505 (and roll around) */
1506 if(rank+scores[prefs[i].value]<=0xFFFF)
1507 scores[prefs[i].value]+=rank;
1509 scores[prefs[i].value]=0xFFFF;
1511 mask[prefs[i].value/32] |= 1<<(prefs[i].value%32);
1515 /* We saw the implicit algorithm, so we don't need
1516 tack it on the end ourselves. */
1517 if(implicit==prefs[i].value)
1523 if(rank==1 && preftype==PREFTYPE_ZIP)
1525 /* If the compression preferences are not present, they are
1526 assumed to be ZIP, Uncompressed (RFC4880:13.3.1) */
1527 scores[1]=1; /* ZIP is first choice */
1528 scores[0]=2; /* Uncompressed is second choice */
1532 /* If the key didn't have the implicit algorithm listed
1533 explicitly, add it here at the tail of the list. */
1536 scores[implicit]+=rank;
1537 mask[implicit/32] |= 1<<(implicit%32);
1544 /* We've now scored all of the algorithms, and the usable ones have
1545 bits set. Let's pick the winner. */
1547 /* The caller passed us a request. Can we use it? */
1548 if(request>-1 && (bits[request/32] & (1<<(request%32))) &&
1549 algo_available(preftype,request,hint))
1554 /* If we have personal prefs set, use them. */
1556 if(preftype==PREFTYPE_SYM && opt.personal_cipher_prefs)
1557 prefs=opt.personal_cipher_prefs;
1558 else if(preftype==PREFTYPE_HASH && opt.personal_digest_prefs)
1559 prefs=opt.personal_digest_prefs;
1560 else if(preftype==PREFTYPE_ZIP && opt.personal_compress_prefs)
1561 prefs=opt.personal_compress_prefs;
1564 for(i=0; prefs[i].type; i++ )
1566 if(bits[prefs[i].value/32] & (1<<(prefs[i].value%32))
1567 && algo_available( preftype, prefs[i].value, hint))
1569 result = prefs[i].value;
1577 unsigned int best=-1;
1579 /* At this point, we have not selected an algorithm due to a
1580 special request or via personal prefs. Pick the highest
1581 ranked algorithm (i.e. the one with the lowest score). */
1583 if(preftype==PREFTYPE_HASH && scores[DIGEST_ALGO_MD5])
1585 /* "If you are building an authentication system, the recipient
1586 may specify a preferred signing algorithm. However, the
1587 signer would be foolish to use a weak algorithm simply
1588 because the recipient requests it." (RFC4880:14). If any
1589 other hash algorithm is available, pretend that MD5 isn't.
1590 Note that if the user intentionally chose MD5 by putting it
1591 in their personal prefs, then we do what the user said (as we
1592 never reach this code). */
1594 for(i=DIGEST_ALGO_MD5+1;i<256;i++)
1597 scores[DIGEST_ALGO_MD5]=0;
1604 /* Note the '<' here. This means in case of a tie, we will
1605 favor the lower algorithm number. We have a choice
1606 between the lower number (probably an older algorithm
1607 with more time in use), or the higher number (probably a
1608 newer algorithm with less time in use). Older is
1609 probably safer here, even though the newer algorithms
1610 tend to be "stronger". */
1611 if(scores[i] && scores[i]<best
1612 && (bits[i/32] & (1<<(i%32)))
1613 && algo_available(preftype,i,hint))
1625 * Select the MDC flag from the pk_list. We can only use MDC if all
1626 * recipients support this feature.
1629 select_mdc_from_pklist (PK_LIST pk_list)
1636 for (pkr = pk_list; pkr; pkr = pkr->next)
1640 if (pkr->pk->user_id) /* selected by user ID */
1641 mdc = pkr->pk->user_id->flags.mdc;
1643 mdc = pkr->pk->flags.mdc;
1645 return 0; /* At least one recipient does not support it. */
1647 return 1; /* Can be used. */
1651 /* Print a warning for all keys in PK_LIST missing the MDC feature. */
1653 warn_missing_mdc_from_pklist (PK_LIST pk_list)
1657 for (pkr = pk_list; pkr; pkr = pkr->next)
1661 if (pkr->pk->user_id) /* selected by user ID */
1662 mdc = pkr->pk->user_id->flags.mdc;
1664 mdc = pkr->pk->flags.mdc;
1666 log_info (_("Note: key %s has no %s feature\n"),
1667 keystr_from_pk (pkr->pk), "MDC");
1672 warn_missing_aes_from_pklist (PK_LIST pk_list)
1676 for (pkr = pk_list; pkr; pkr = pkr->next)
1678 const prefitem_t *prefs;
1682 prefs = pkr->pk->user_id? pkr->pk->user_id->prefs : pkr->pk->prefs;
1685 for (i=0; !gotit && prefs[i].type; i++ )
1686 if (prefs[i].type == PREFTYPE_SYM
1687 && prefs[i].value == CIPHER_ALGO_AES)
1691 log_info (_("Note: key %s has no preference for %s\n"),
1692 keystr_from_pk (pkr->pk), "AES");