summaryrefslogtreecommitdiffstats
path: root/g10
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2021-04-12 17:50:17 +0200
committerWerner Koch <wk@gnupg.org>2021-04-12 17:50:17 +0200
commitd984de172c29cd9a56e3a397bdb573519ff74e92 (patch)
treef801985e0ad82952638a305573176a42c839db1a /g10
parentscd:p15: Match private keys with certificates also by labels. (diff)
downloadgnupg2-d984de172c29cd9a56e3a397bdb573519ff74e92.tar.xz
gnupg2-d984de172c29cd9a56e3a397bdb573519ff74e92.zip
gpg: Minor restructuring of a function.
-- This is for easier reading and future changing.
Diffstat (limited to 'g10')
-rw-r--r--g10/getkey.c71
-rw-r--r--g10/sign.c2
2 files changed, 41 insertions, 32 deletions
diff --git a/g10/getkey.c b/g10/getkey.c
index bc9e82638..70405c89d 100644
--- a/g10/getkey.c
+++ b/g10/getkey.c
@@ -3857,66 +3857,75 @@ lookup (ctrl_t ctrl, getkey_ctx_t ctx, int want_secret,
}
+/* If a default key has been specified, return that key. If a card
+ * based key is also available as indicated by FPR_CARD not being
+ * NULL, return that key if suitable. */
gpg_error_t
get_seckey_default_or_card (ctrl_t ctrl, PKT_public_key *pk,
const byte *fpr_card, size_t fpr_len)
{
gpg_error_t err;
strlist_t namelist = NULL;
+ const char *def_secret_key;
- const char *def_secret_key = parse_def_secret_key (ctrl);
+ def_secret_key = parse_def_secret_key (ctrl);
if (def_secret_key)
add_to_strlist (&namelist, def_secret_key);
else if (fpr_card)
{
- int rc = get_pubkey_byfprint (ctrl, pk, NULL, fpr_card, fpr_len);
+ err = get_pubkey_byfprint (ctrl, pk, NULL, fpr_card, fpr_len);
/* The key on card can be not suitable for requested usage. */
- if (rc == GPG_ERR_UNUSABLE_PUBKEY)
+ if (gpg_err_code (err) == GPG_ERR_UNUSABLE_PUBKEY)
fpr_card = NULL; /* Fallthrough as no card. */
else
- return rc;
+ return err; /* Success or other error. */
}
- if (!fpr_card
- || (def_secret_key && def_secret_key[strlen (def_secret_key)-1] == '!'))
- err = key_byname (ctrl, NULL, namelist, pk, 1, 0, NULL, NULL);
+ if (!fpr_card || (def_secret_key && *def_secret_key
+ && def_secret_key[strlen (def_secret_key)-1] == '!'))
+ {
+ err = key_byname (ctrl, NULL, namelist, pk, 1, 0, NULL, NULL);
+ }
else
{ /* Default key is specified and card key is also available. */
kbnode_t k, keyblock = NULL;
err = key_byname (ctrl, NULL, namelist, pk, 1, 0, &keyblock, NULL);
- if (!err)
- for (k = keyblock; k; k = k->next)
- {
- PKT_public_key *pk_candidate;
- char fpr[MAX_FINGERPRINT_LEN];
-
- if (k->pkt->pkttype != PKT_PUBLIC_KEY
- &&k->pkt->pkttype != PKT_PUBLIC_SUBKEY)
- continue;
-
- pk_candidate = k->pkt->pkt.public_key;
- if (!pk_candidate->flags.valid)
- continue;
- if (!((pk_candidate->pubkey_usage & USAGE_MASK) & pk->req_usage))
- continue;
- fingerprint_from_pk (pk_candidate, fpr, NULL);
- if (!memcmp (fpr_card, fpr, fpr_len))
- {
- release_public_key_parts (pk);
- copy_public_key (pk, pk_candidate);
- break;
- }
- }
+ if (err)
+ goto leave;
+ for (k = keyblock; k; k = k->next)
+ {
+ PKT_public_key *pk_candidate;
+ char fpr[MAX_FINGERPRINT_LEN];
+
+ if (k->pkt->pkttype != PKT_PUBLIC_KEY
+ &&k->pkt->pkttype != PKT_PUBLIC_SUBKEY)
+ continue;
+
+ pk_candidate = k->pkt->pkt.public_key;
+ if (!pk_candidate->flags.valid)
+ continue;
+ if (!((pk_candidate->pubkey_usage & USAGE_MASK) & pk->req_usage))
+ continue;
+ fingerprint_from_pk (pk_candidate, fpr, NULL);
+ if (!memcmp (fpr_card, fpr, fpr_len))
+ {
+ release_public_key_parts (pk);
+ copy_public_key (pk, pk_candidate);
+ break;
+ }
+ }
release_kbnode (keyblock);
}
+ leave:
free_strlist (namelist);
-
return err;
}
+
+
/*********************************************
*********** User ID printing helpers *******
diff --git a/g10/sign.c b/g10/sign.c
index b1684386d..80e6ce2e8 100644
--- a/g10/sign.c
+++ b/g10/sign.c
@@ -997,7 +997,7 @@ write_signature_packets (ctrl_t ctrl,
/*
- * Sign the files whose names are in FILENAME usingall secret keys
+ * Sign the files whose names are in FILENAME using all secret keys
* which can be taken from LOCUSR, if this is NULL, use the default
* secret key.
* If DETACHED has the value true, make a detached signature.