2003-01-09 14:29:36 +01:00
|
|
|
/* delkey.c - delete keys
|
2006-04-19 13:26:11 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004,
|
|
|
|
* 2005, 2006 Free Software Foundation, Inc.
|
2019-05-27 10:40:38 +02:00
|
|
|
* Copyright (C) 2014, 2019 Werner Koch
|
2003-01-09 14:29:36 +01:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-01-09 14:29:36 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-01-09 14:29:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
#include "gpg.h"
|
2003-01-09 14:29:36 +01:00
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/status.h"
|
|
|
|
#include "../common/iobuf.h"
|
2003-01-09 14:29:36 +01:00
|
|
|
#include "keydb.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/util.h"
|
2003-01-09 14:29:36 +01:00
|
|
|
#include "main.h"
|
|
|
|
#include "trustdb.h"
|
|
|
|
#include "filter.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/ttyio.h"
|
|
|
|
#include "../common/i18n.h"
|
2020-11-11 01:13:13 +01:00
|
|
|
#include "../common/shareddefs.h"
|
2014-04-15 16:40:48 +02:00
|
|
|
#include "call-agent.h"
|
2003-01-09 14:29:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Delete a public or secret key from a keyring.
|
|
|
|
* r_sec_avail will be set if a secret key is available and the public
|
|
|
|
* key can't be deleted for that reason.
|
|
|
|
*/
|
2014-04-15 15:29:45 +02:00
|
|
|
static gpg_error_t
|
2017-03-31 20:03:52 +02:00
|
|
|
do_delete_key (ctrl_t ctrl, const char *username, int secret, int force,
|
|
|
|
int *r_sec_avail)
|
2003-01-09 14:29:36 +01:00
|
|
|
{
|
2014-04-15 15:29:45 +02:00
|
|
|
gpg_error_t err;
|
|
|
|
kbnode_t keyblock = NULL;
|
2014-04-15 16:40:48 +02:00
|
|
|
kbnode_t node, kbctx;
|
2019-05-27 10:40:38 +02:00
|
|
|
kbnode_t targetnode;
|
2014-04-15 15:29:45 +02:00
|
|
|
KEYDB_HANDLE hd;
|
|
|
|
PKT_public_key *pk = NULL;
|
|
|
|
u32 keyid[2];
|
|
|
|
int okay=0;
|
|
|
|
int yes;
|
|
|
|
KEYDB_SEARCH_DESC desc;
|
2019-05-27 10:40:38 +02:00
|
|
|
int exactmatch; /* True if key was found by fingerprint. */
|
|
|
|
int thiskeyonly; /* 0 = false, 1 = is primary key, 2 = is a subkey. */
|
2014-04-15 15:29:45 +02:00
|
|
|
|
|
|
|
*r_sec_avail = 0;
|
|
|
|
|
2019-09-09 14:34:09 +02:00
|
|
|
hd = keydb_new (ctrl);
|
2015-12-03 12:18:32 +01:00
|
|
|
if (!hd)
|
|
|
|
return gpg_error_from_syserror ();
|
2014-04-15 15:29:45 +02:00
|
|
|
|
|
|
|
/* Search the userid. */
|
|
|
|
err = classify_user_id (username, &desc, 1);
|
2019-03-14 14:55:06 +01:00
|
|
|
exactmatch = (desc.mode == KEYDB_SEARCH_MODE_FPR);
|
2019-05-27 10:40:38 +02:00
|
|
|
thiskeyonly = desc.exact;
|
2014-04-15 15:29:45 +02:00
|
|
|
if (!err)
|
|
|
|
err = keydb_search (hd, &desc, 1, NULL);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_error (_("key \"%s\" not found: %s\n"), username, gpg_strerror (err));
|
|
|
|
write_status_text (STATUS_DELETE_PROBLEM, "1");
|
|
|
|
goto leave;
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 15:29:45 +02:00
|
|
|
/* Read the keyblock. */
|
|
|
|
err = keydb_get_keyblock (hd, &keyblock);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_error (_("error reading keyblock: %s\n"), gpg_strerror (err) );
|
|
|
|
goto leave;
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 15:29:45 +02:00
|
|
|
/* Get the keyid from the keyblock. */
|
|
|
|
node = find_kbnode( keyblock, PKT_PUBLIC_KEY );
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
log_error ("Oops; key not found anymore!\n");
|
|
|
|
err = gpg_error (GPG_ERR_GENERAL);
|
|
|
|
goto leave;
|
|
|
|
}
|
2019-05-27 10:40:38 +02:00
|
|
|
|
|
|
|
/* If an operation only on a subkey is requested, find that subkey
|
|
|
|
* now. */
|
|
|
|
if (thiskeyonly)
|
|
|
|
{
|
|
|
|
kbnode_t tmpnode;
|
|
|
|
|
|
|
|
for (kbctx=NULL; (tmpnode = walk_kbnode (keyblock, &kbctx, 0)); )
|
|
|
|
{
|
|
|
|
if (!(tmpnode->pkt->pkttype == PKT_PUBLIC_KEY
|
|
|
|
|| tmpnode->pkt->pkttype == PKT_PUBLIC_SUBKEY))
|
|
|
|
continue;
|
|
|
|
if (exact_subkey_match_p (&desc, tmpnode))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!tmpnode)
|
|
|
|
{
|
|
|
|
log_error ("Oops; requested subkey not found anymore!\n");
|
|
|
|
err = gpg_error (GPG_ERR_GENERAL);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
/* Set NODE to this specific subkey or primary key. */
|
|
|
|
thiskeyonly = node == tmpnode? 1 : 2;
|
|
|
|
targetnode = tmpnode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
targetnode = node;
|
|
|
|
|
|
|
|
pk = targetnode->pkt->pkt.public_key;
|
2014-04-15 15:29:45 +02:00
|
|
|
keyid_from_pk (pk, keyid);
|
|
|
|
|
|
|
|
if (!secret && !force)
|
|
|
|
{
|
2019-09-09 14:34:09 +02:00
|
|
|
if (have_secret_key_with_kid (ctrl, keyid))
|
2014-04-15 15:29:45 +02:00
|
|
|
{
|
|
|
|
*r_sec_avail = 1;
|
|
|
|
err = gpg_error (GPG_ERR_EOF);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
err = 0;
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2019-09-09 14:34:09 +02:00
|
|
|
if (secret && !have_secret_key_with_kid (ctrl, keyid))
|
2014-09-18 16:00:34 +02:00
|
|
|
{
|
|
|
|
err = gpg_error (GPG_ERR_NOT_FOUND);
|
2015-12-03 12:25:37 +01:00
|
|
|
log_error (_("key \"%s\" not found\n"), username);
|
2014-09-18 16:00:34 +02:00
|
|
|
write_status_text (STATUS_DELETE_PROBLEM, "1");
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-15 15:29:45 +02:00
|
|
|
if (opt.batch && exactmatch)
|
2020-11-10 06:08:24 +01:00
|
|
|
{
|
2020-11-11 01:13:13 +01:00
|
|
|
if (secret && opt.pinentry_mode == PINENTRY_MODE_LOOPBACK
|
|
|
|
&& !opt.answer_yes)
|
2020-11-10 06:08:24 +01:00
|
|
|
log_error(_("can't do this in batch mode without \"--yes\"\n"));
|
|
|
|
else
|
|
|
|
okay++;
|
|
|
|
}
|
2014-04-15 15:29:45 +02:00
|
|
|
else if (opt.batch && secret)
|
|
|
|
{
|
|
|
|
log_error(_("can't do this in batch mode\n"));
|
|
|
|
log_info (_("(unless you specify the key by fingerprint)\n"));
|
|
|
|
}
|
|
|
|
else if (opt.batch && opt.answer_yes)
|
|
|
|
okay++;
|
|
|
|
else if (opt.batch)
|
|
|
|
{
|
|
|
|
log_error(_("can't do this in batch mode without \"--yes\"\n"));
|
|
|
|
log_info (_("(unless you specify the key by fingerprint)\n"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-21 12:54:47 +02:00
|
|
|
print_key_info (ctrl, NULL, 0, pk, secret);
|
2019-05-27 10:40:38 +02:00
|
|
|
tty_printf ("\n");
|
|
|
|
if (thiskeyonly == 1 && !secret)
|
|
|
|
{
|
|
|
|
/* We need to delete the entire public key despite the use
|
|
|
|
* of the thiskeyonly request. */
|
|
|
|
tty_printf (_("Note: The public primary key and all its subkeys"
|
|
|
|
" will be deleted.\n"));
|
|
|
|
}
|
|
|
|
else if (thiskeyonly == 2 && !secret)
|
|
|
|
{
|
|
|
|
tty_printf (_("Note: Only the shown public subkey"
|
|
|
|
" will be deleted.\n"));
|
|
|
|
}
|
|
|
|
if (thiskeyonly == 1 && secret)
|
|
|
|
{
|
|
|
|
tty_printf (_("Note: Only the secret part of the shown primary"
|
|
|
|
" key will be deleted.\n"));
|
|
|
|
}
|
|
|
|
else if (thiskeyonly == 2 && secret)
|
|
|
|
{
|
|
|
|
tty_printf (_("Note: Only the secret part of the shown subkey"
|
|
|
|
" will be deleted.\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thiskeyonly)
|
|
|
|
tty_printf ("\n");
|
2014-04-15 15:29:45 +02:00
|
|
|
|
|
|
|
yes = cpr_get_answer_is_yes
|
|
|
|
(secret? "delete_key.secret.okay": "delete_key.okay",
|
|
|
|
_("Delete this key from the keyring? (y/N) "));
|
|
|
|
|
|
|
|
if (!cpr_enabled() && secret && yes)
|
|
|
|
{
|
|
|
|
/* I think it is not required to check a passphrase; if the
|
|
|
|
* user is so stupid as to let others access his secret
|
|
|
|
* keyring (and has no backup) - it is up him to read some
|
|
|
|
* very basic texts about security. */
|
|
|
|
yes = cpr_get_answer_is_yes
|
|
|
|
("delete_key.secret.okay",
|
|
|
|
_("This is a secret key! - really delete? (y/N) "));
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
2014-04-15 15:29:45 +02:00
|
|
|
|
|
|
|
if (yes)
|
|
|
|
okay++;
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-15 15:29:45 +02:00
|
|
|
if (okay)
|
|
|
|
{
|
2011-04-29 12:01:52 +02:00
|
|
|
if (secret)
|
|
|
|
{
|
2014-04-15 16:40:48 +02:00
|
|
|
char *prompt;
|
|
|
|
gpg_error_t firsterr = 0;
|
|
|
|
char *hexgrip;
|
|
|
|
|
|
|
|
setup_main_keyids (keyblock);
|
|
|
|
for (kbctx=NULL; (node = walk_kbnode (keyblock, &kbctx, 0)); )
|
|
|
|
{
|
|
|
|
if (!(node->pkt->pkttype == PKT_PUBLIC_KEY
|
|
|
|
|| node->pkt->pkttype == PKT_PUBLIC_SUBKEY))
|
|
|
|
continue;
|
|
|
|
|
2019-05-27 10:40:38 +02:00
|
|
|
if (thiskeyonly && targetnode != node)
|
|
|
|
continue;
|
|
|
|
|
2020-01-17 07:11:02 +01:00
|
|
|
if (!agent_probe_secret_key (NULL, node->pkt->pkt.public_key))
|
2014-04-15 16:40:48 +02:00
|
|
|
continue; /* No secret key for that public (sub)key. */
|
|
|
|
|
2017-03-31 20:03:52 +02:00
|
|
|
prompt = gpg_format_keydesc (ctrl,
|
|
|
|
node->pkt->pkt.public_key,
|
2014-04-15 16:40:48 +02:00
|
|
|
FORMAT_KEYDESC_DELKEY, 1);
|
|
|
|
err = hexkeygrip_from_pk (node->pkt->pkt.public_key, &hexgrip);
|
2016-05-10 11:01:42 +02:00
|
|
|
/* NB: We require --yes to advise the agent not to
|
|
|
|
* request a confirmation. The rationale for this extra
|
|
|
|
* pre-caution is that since 2.1 the secret key may also
|
|
|
|
* be used for other protocols and thus deleting it from
|
|
|
|
* the gpg would also delete the key for other tools. */
|
2019-05-20 12:31:55 +02:00
|
|
|
if (!err && !opt.dry_run)
|
2016-05-10 11:01:42 +02:00
|
|
|
err = agent_delete_key (NULL, hexgrip, prompt,
|
|
|
|
opt.answer_yes);
|
2014-04-15 16:40:48 +02:00
|
|
|
xfree (prompt);
|
|
|
|
xfree (hexgrip);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_KEY_ON_CARD)
|
|
|
|
write_status_text (STATUS_DELETE_PROBLEM, "1");
|
|
|
|
log_error (_("deleting secret %s failed: %s\n"),
|
|
|
|
(node->pkt->pkttype == PKT_PUBLIC_KEY
|
|
|
|
? _("key"):_("subkey")),
|
|
|
|
gpg_strerror (err));
|
|
|
|
if (!firsterr)
|
|
|
|
firsterr = err;
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_CANCELED
|
|
|
|
|| gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
|
2015-11-09 05:20:13 +01:00
|
|
|
{
|
|
|
|
write_status_error ("delete_key.secret", err);
|
|
|
|
break;
|
|
|
|
}
|
2014-04-15 16:40:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err = firsterr;
|
|
|
|
if (firsterr)
|
|
|
|
goto leave;
|
2011-04-29 12:01:52 +02:00
|
|
|
}
|
2019-05-27 10:40:38 +02:00
|
|
|
else if (thiskeyonly == 2)
|
|
|
|
{
|
|
|
|
/* Delete the specified public subkey. */
|
|
|
|
for (kbctx=NULL; (node = walk_kbnode (keyblock, &kbctx, 0)); )
|
2019-10-15 14:30:57 +02:00
|
|
|
if (targetnode == node)
|
|
|
|
break;
|
|
|
|
log_assert (node);
|
|
|
|
delete_kbnode (node);
|
|
|
|
while ((node = walk_kbnode (keyblock, &kbctx, 0))
|
|
|
|
&& node->pkt->pkttype == PKT_SIGNATURE)
|
|
|
|
delete_kbnode (node);
|
2019-05-27 10:40:38 +02:00
|
|
|
|
|
|
|
commit_kbnode (&keyblock);
|
|
|
|
err = keydb_update_keyblock (ctrl, hd, keyblock);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_error (_("update failed: %s\n"), gpg_strerror (err));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
2011-04-29 12:01:52 +02:00
|
|
|
else
|
|
|
|
{
|
2019-05-27 10:40:38 +02:00
|
|
|
err = keydb_delete_keyblock (hd);
|
2014-04-15 15:29:45 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_error (_("deleting keyblock failed: %s\n"),
|
|
|
|
gpg_strerror (err));
|
|
|
|
goto leave;
|
|
|
|
}
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2011-04-29 12:01:52 +02:00
|
|
|
/* Note that the ownertrust being cleared will trigger a
|
|
|
|
revalidation_mark(). This makes sense - only deleting keys
|
|
|
|
that have ownertrust set should trigger this. */
|
2003-01-09 14:29:36 +01:00
|
|
|
|
2019-05-27 10:40:38 +02:00
|
|
|
if (!secret && pk && !opt.dry_run && thiskeyonly != 2
|
|
|
|
&& clear_ownertrusts (ctrl, pk))
|
2014-04-15 15:29:45 +02:00
|
|
|
{
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info (_("ownertrust information cleared\n"));
|
|
|
|
}
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 15:29:45 +02:00
|
|
|
leave:
|
|
|
|
keydb_release (hd);
|
|
|
|
release_kbnode (keyblock);
|
|
|
|
return err;
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2019-05-27 10:40:38 +02:00
|
|
|
|
|
|
|
/*
|
2003-01-09 14:29:36 +01:00
|
|
|
* Delete a public or secret key from a keyring.
|
|
|
|
*/
|
2014-04-15 15:29:45 +02:00
|
|
|
gpg_error_t
|
2017-03-31 20:03:52 +02:00
|
|
|
delete_keys (ctrl_t ctrl, strlist_t names, int secret, int allow_both)
|
2003-01-09 14:29:36 +01:00
|
|
|
{
|
2014-04-15 15:29:45 +02:00
|
|
|
gpg_error_t err;
|
|
|
|
int avail;
|
|
|
|
int force = (!allow_both && !secret && opt.expert);
|
|
|
|
|
|
|
|
/* Force allows us to delete a public key even if a secret key
|
|
|
|
exists. */
|
|
|
|
|
|
|
|
for ( ;names ; names=names->next )
|
|
|
|
{
|
2017-03-31 20:03:52 +02:00
|
|
|
err = do_delete_key (ctrl, names->d, secret, force, &avail);
|
2014-04-15 15:29:45 +02:00
|
|
|
if (err && avail)
|
|
|
|
{
|
|
|
|
if (allow_both)
|
|
|
|
{
|
2017-03-31 20:03:52 +02:00
|
|
|
err = do_delete_key (ctrl, names->d, 1, 0, &avail);
|
2014-04-15 15:29:45 +02:00
|
|
|
if (!err)
|
2017-03-31 20:03:52 +02:00
|
|
|
err = do_delete_key (ctrl, names->d, 0, 0, &avail);
|
2014-04-15 15:29:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log_error (_("there is a secret key for public key \"%s\"!\n"),
|
|
|
|
names->d);
|
|
|
|
log_info(_("use option \"--delete-secret-keys\" to delete"
|
|
|
|
" it first.\n"));
|
|
|
|
write_status_text (STATUS_DELETE_PROBLEM, "2");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
log_error ("%s: delete key failed: %s\n",
|
|
|
|
names->d, gpg_strerror (err));
|
|
|
|
return err;
|
|
|
|
}
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 15:29:45 +02:00
|
|
|
return 0;
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|