g10: Change ask_curve so that it can be used outside.

* g10/call-agent.h (struct key_attr): New.
* g10/keygen.c (ask_curve): Return const char *.  No allocation.
(quick_generate_keypair): Follow the change.
(generate_keypair, generate_subkeypair): Likewise.
(parse_algo_usage_expire): Return const char *.

--

This change is intended for using ask_curve from card-util.c.

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2018-03-28 18:44:45 +09:00
parent 96918346be
commit e610d51f0d
2 changed files with 23 additions and 30 deletions

View File

@ -19,6 +19,13 @@
#ifndef GNUPG_G10_CALL_AGENT_H
#define GNUPG_G10_CALL_AGENT_H
struct key_attr {
int algo; /* Algorithm identifier. */
union {
unsigned int nbits; /* Supported keysize. */
const char *curve; /* Name of curve. */
};
};
struct agent_card_info_s
{
@ -57,13 +64,7 @@ struct agent_card_info_s
int is_v2; /* True if this is a v2 card. */
int chvmaxlen[3]; /* Maximum allowed length of a CHV. */
int chvretry[3]; /* Allowed retries for the CHV; 0 = blocked. */
struct { /* Array with key attributes. */
int algo; /* Algorithm identifier. */
union {
unsigned int nbits; /* Supported keysize. */
const char *curve; /* Name of curve. */
};
} key_attr[3];
struct key_attr key_attr[3];
struct {
unsigned int ki:1; /* Key import available. */
unsigned int aac:1; /* Algorithm attributes are changeable. */

View File

@ -141,8 +141,8 @@ static gpg_error_t parse_algo_usage_expire (ctrl_t ctrl, int for_subkey,
const char *algostr, const char *usagestr,
const char *expirestr,
int *r_algo, unsigned int *r_usage,
u32 *r_expire,
unsigned int *r_nbits, char **r_curve);
u32 *r_expire, unsigned int *r_nbits,
const char **r_curve);
static void do_generate_keypair (ctrl_t ctrl, struct para_data_s *para,
struct output_control_s *outctrl, int card );
static int write_keyblock (iobuf_t out, kbnode_t node);
@ -2233,9 +2233,9 @@ ask_keysize (int algo, unsigned int primary_keysize)
/* Ask for the curve. ALGO is the selected algorithm which this
function may adjust. Returns a malloced string with the name of
the curve. BOTH tells that gpg creates a primary and subkey. */
static char *
function may adjust. Returns a const string of the name of the
curve. */
static const char *
ask_curve (int *algo, int *subkey_algo)
{
/* NB: We always use a complete algo list so that we have stable
@ -2267,7 +2267,7 @@ ask_curve (int *algo, int *subkey_algo)
#undef MY_USE_ECDSADH
int idx;
char *answer;
char *result = NULL;
const char *result = NULL;
gcry_sexp_t keyparms;
tty_printf (_("Please select which elliptic curve you want:\n"));
@ -2358,16 +2358,16 @@ ask_curve (int *algo, int *subkey_algo)
if (subkey_algo && *subkey_algo == PUBKEY_ALGO_ECDSA)
*subkey_algo = PUBKEY_ALGO_EDDSA;
*algo = PUBKEY_ALGO_EDDSA;
result = xstrdup (curves[idx].eddsa_curve);
result = curves[idx].eddsa_curve;
}
else
result = xstrdup (curves[idx].name);
result = curves[idx].name;
break;
}
}
if (!result)
result = xstrdup (curves[0].name);
result = curves[0].name;
return result;
}
@ -4058,7 +4058,7 @@ quick_generate_keypair (ctrl_t ctrl, const char *uid, const char *algostr,
unsigned int use;
u32 expire;
unsigned int nbits;
char *curve;
const char *curve;
err = parse_algo_usage_expire (ctrl, 0, algostr, usagestr, expirestr,
&algo, &use, &expire, &nbits, &curve);
@ -4253,7 +4253,7 @@ generate_keypair (ctrl_t ctrl, int full, const char *fname,
}
else
{
char *curve = NULL;
const char *curve = NULL;
if (subkey_algo)
{
@ -4316,8 +4316,7 @@ generate_keypair (ctrl_t ctrl, int full, const char *fname,
{
/* Need to switch to a different curve for the
encryption key. */
xfree (curve);
curve = xstrdup ("Curve25519");
curve = "Curve25519";
}
r = xmalloc_clear (sizeof *r + strlen (curve));
r->key = pSUBKEYCURVE;
@ -4377,8 +4376,6 @@ generate_keypair (ctrl_t ctrl, int full, const char *fname,
r->next = para;
para = r;
}
xfree (curve);
}
}
else /* Default key generation. */
@ -4921,7 +4918,7 @@ parse_algo_usage_expire (ctrl_t ctrl, int for_subkey,
const char *algostr, const char *usagestr,
const char *expirestr,
int *r_algo, unsigned int *r_usage, u32 *r_expire,
unsigned int *r_nbits, char **r_curve)
unsigned int *r_nbits, const char **r_curve)
{
gpg_error_t err;
int algo;
@ -4979,11 +4976,7 @@ parse_algo_usage_expire (ctrl_t ctrl, int for_subkey,
return gpg_error (GPG_ERR_INV_VALUE);
if (curve)
{
*r_curve = xtrystrdup (curve);
if (!*r_curve)
return gpg_error_from_syserror ();
}
*r_curve = curve;
*r_algo = algo;
*r_usage = use;
*r_expire = expire;
@ -5008,7 +5001,7 @@ generate_subkeypair (ctrl_t ctrl, kbnode_t keyblock, const char *algostr,
unsigned int use;
u32 expire;
unsigned int nbits = 0;
char *curve = NULL;
const char *curve = NULL;
u32 cur_time;
char *key_from_hexgrip = NULL;
char *hexgrip = NULL;
@ -5160,7 +5153,6 @@ generate_subkeypair (ctrl_t ctrl, kbnode_t keyblock, const char *algostr,
leave:
xfree (key_from_hexgrip);
xfree (curve);
xfree (hexgrip);
xfree (serialno);
xfree (cache_nonce);