mirror of
git://git.gnupg.org/gnupg.git
synced 2025-01-03 12:11:33 +01:00
common: Add dedicated private key functions to name-value.c.
* common/name-value.c (struct name_value_container): Add field 'private_key_mode'. (my_error): New. Use instead of gpg_error. (nvc_new_private_key): New. (nve_release): Add arg 'private_key_mode'. (nvc_release): Call nve_release with private_key_mode flag. (nvc_delete): Ditto. (_nvc_add): Do no special case "Key:" in non-private_key_mode. (nvc_get_private_key): Return error in non-private_key_mode. (nvc_set_private_key): Ditto. (nvc_parse): Factor all code out to ... (do_nvc_parse): new. Add arg 'for_private_key'. (nvc_parse_private_key): New. * agent/findkey.c (write_extended_private_key): Replace nvc_parse by nvc_parse_private_key. (read_key_file): Ditto. * common/t-name-value.c (private_key_mode): New variable. (my_nvc_new): New. Replace all callers. (test_key_extraction): Take mode in account. (run_tests): Ditto. (run_modification_tests): Ditto. (parse): Ditto. (main): Add option --parse and rename --parse to --parse-key. -- Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
d74d23d860
commit
3ead21da80
@ -62,7 +62,7 @@ write_extended_private_key (char *fname, estream_t fp,
|
|||||||
int remove = 0;
|
int remove = 0;
|
||||||
int line;
|
int line;
|
||||||
|
|
||||||
err = nvc_parse (&pk, &line, fp);
|
err = nvc_parse_private_key (&pk, &line, fp);
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
log_error ("error parsing '%s' line %d: %s\n",
|
log_error ("error parsing '%s' line %d: %s\n",
|
||||||
@ -690,7 +690,7 @@ read_key_file (const unsigned char *grip, gcry_sexp_t *result)
|
|||||||
nvc_t pk;
|
nvc_t pk;
|
||||||
int line;
|
int line;
|
||||||
|
|
||||||
rc = nvc_parse (&pk, &line, fp);
|
rc = nvc_parse_private_key (&pk, &line, fp);
|
||||||
es_fclose (fp);
|
es_fclose (fp);
|
||||||
|
|
||||||
if (rc)
|
if (rc)
|
||||||
|
@ -47,6 +47,7 @@ struct name_value_container
|
|||||||
{
|
{
|
||||||
struct name_value_entry *first;
|
struct name_value_entry *first;
|
||||||
struct name_value_entry *last;
|
struct name_value_entry *last;
|
||||||
|
unsigned int private_key_mode:1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -75,6 +76,13 @@ my_error_from_syserror (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline gpg_error_t
|
||||||
|
my_error (gpg_err_code_t ec)
|
||||||
|
{
|
||||||
|
return gpg_err_make (default_errsource, ec);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Allocation and deallocation. */
|
/* Allocation and deallocation. */
|
||||||
@ -87,17 +95,31 @@ nvc_new (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Allocate a private key container structure for use with private keys. */
|
||||||
|
nvc_t
|
||||||
|
nvc_new_private_key (void)
|
||||||
|
{
|
||||||
|
nvc_t nvc = nvc_new ();
|
||||||
|
if (nvc)
|
||||||
|
nvc->private_key_mode = 1;
|
||||||
|
return nvc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
nve_release (nve_t entry)
|
nve_release (nve_t entry, int private_key_mode)
|
||||||
{
|
{
|
||||||
if (entry == NULL)
|
if (entry == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
xfree (entry->name);
|
xfree (entry->name);
|
||||||
if (entry->value)
|
if (entry->value && private_key_mode)
|
||||||
wipememory (entry->value, strlen (entry->value));
|
wipememory (entry->value, strlen (entry->value));
|
||||||
xfree (entry->value);
|
xfree (entry->value);
|
||||||
free_strlist_wipe (entry->raw_value);
|
if (private_key_mode)
|
||||||
|
free_strlist_wipe (entry->raw_value);
|
||||||
|
else
|
||||||
|
free_strlist (entry->raw_value);
|
||||||
xfree (entry);
|
xfree (entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +136,7 @@ nvc_release (nvc_t pk)
|
|||||||
for (e = pk->first; e; e = next)
|
for (e = pk->first; e; e = next)
|
||||||
{
|
{
|
||||||
next = e->next;
|
next = e->next;
|
||||||
nve_release (e);
|
nve_release (e, pk->private_key_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
xfree (pk);
|
xfree (pk);
|
||||||
@ -336,13 +358,16 @@ _nvc_add (nvc_t pk, char *name, char *value, strlist_t raw_value,
|
|||||||
|
|
||||||
if (name && ! valid_name (name))
|
if (name && ! valid_name (name))
|
||||||
{
|
{
|
||||||
err = gpg_error (GPG_ERR_INV_NAME);
|
err = my_error (GPG_ERR_INV_NAME);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name && ascii_strcasecmp (name, "Key:") == 0 && nvc_lookup (pk, "Key:"))
|
if (name
|
||||||
|
&& pk->private_key_mode
|
||||||
|
&& !ascii_strcasecmp (name, "Key:")
|
||||||
|
&& nvc_lookup (pk, "Key:"))
|
||||||
{
|
{
|
||||||
err = gpg_error (GPG_ERR_INV_NAME);
|
err = my_error (GPG_ERR_INV_NAME);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -486,7 +511,7 @@ nvc_delete (nvc_t pk, nve_t entry)
|
|||||||
else
|
else
|
||||||
pk->last = entry->prev;
|
pk->last = entry->prev;
|
||||||
|
|
||||||
nve_release (entry);
|
nve_release (entry, pk->private_key_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -549,9 +574,9 @@ nvc_get_private_key (nvc_t pk, gcry_sexp_t *retsexp)
|
|||||||
gpg_error_t err;
|
gpg_error_t err;
|
||||||
nve_t e;
|
nve_t e;
|
||||||
|
|
||||||
e = nvc_lookup (pk, "Key:");
|
e = pk->private_key_mode? nvc_lookup (pk, "Key:") : NULL;
|
||||||
if (e == NULL)
|
if (e == NULL)
|
||||||
return gpg_error (GPG_ERR_MISSING_KEY);
|
return my_error (GPG_ERR_MISSING_KEY);
|
||||||
|
|
||||||
err = assert_value (e);
|
err = assert_value (e);
|
||||||
if (err)
|
if (err)
|
||||||
@ -569,6 +594,9 @@ nvc_set_private_key (nvc_t pk, gcry_sexp_t sexp)
|
|||||||
char *raw, *clean, *p;
|
char *raw, *clean, *p;
|
||||||
size_t len, i;
|
size_t len, i;
|
||||||
|
|
||||||
|
if (!pk->private_key_mode)
|
||||||
|
return my_error (GPG_ERR_MISSING_KEY);
|
||||||
|
|
||||||
len = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0);
|
len = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0);
|
||||||
|
|
||||||
raw = xtrymalloc (len);
|
raw = xtrymalloc (len);
|
||||||
@ -620,11 +648,9 @@ nvc_set_private_key (nvc_t pk, gcry_sexp_t sexp)
|
|||||||
|
|
||||||
/* Parsing and serialization. */
|
/* Parsing and serialization. */
|
||||||
|
|
||||||
/* Parse STREAM and return a newly allocated private key container
|
static gpg_error_t
|
||||||
structure in RESULT. If ERRLINEP is given, the line number the
|
do_nvc_parse (nvc_t *result, int *errlinep, estream_t stream,
|
||||||
parser was last considering is stored there. */
|
int for_private_key)
|
||||||
gpg_error_t
|
|
||||||
nvc_parse (nvc_t *result, int *errlinep, estream_t stream)
|
|
||||||
{
|
{
|
||||||
gpg_error_t err = 0;
|
gpg_error_t err = 0;
|
||||||
gpgrt_ssize_t len;
|
gpgrt_ssize_t len;
|
||||||
@ -633,8 +659,7 @@ nvc_parse (nvc_t *result, int *errlinep, estream_t stream)
|
|||||||
char *name = NULL;
|
char *name = NULL;
|
||||||
strlist_t raw_value = NULL;
|
strlist_t raw_value = NULL;
|
||||||
|
|
||||||
|
*result = for_private_key? nvc_new_private_key () : nvc_new ();
|
||||||
*result = nvc_new ();
|
|
||||||
if (*result == NULL)
|
if (*result == NULL)
|
||||||
return my_error_from_syserror ();
|
return my_error_from_syserror ();
|
||||||
|
|
||||||
@ -680,7 +705,7 @@ nvc_parse (nvc_t *result, int *errlinep, estream_t stream)
|
|||||||
colon = strchr (buf, ':');
|
colon = strchr (buf, ':');
|
||||||
if (colon == NULL)
|
if (colon == NULL)
|
||||||
{
|
{
|
||||||
err = gpg_error (GPG_ERR_INV_VALUE);
|
err = my_error (GPG_ERR_INV_VALUE);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -727,6 +752,27 @@ nvc_parse (nvc_t *result, int *errlinep, estream_t stream)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Parse STREAM and return a newly allocated name value container
|
||||||
|
structure in RESULT. If ERRLINEP is given, the line number the
|
||||||
|
parser was last considering is stored there. */
|
||||||
|
gpg_error_t
|
||||||
|
nvc_parse (nvc_t *result, int *errlinep, estream_t stream)
|
||||||
|
{
|
||||||
|
return do_nvc_parse (result, errlinep, stream, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Parse STREAM and return a newly allocated name value container
|
||||||
|
structure in RESULT - assuming the extended private key format. If
|
||||||
|
ERRLINEP is given, the line number the parser was last considering
|
||||||
|
is stored there. */
|
||||||
|
gpg_error_t
|
||||||
|
nvc_parse_private_key (nvc_t *result, int *errlinep, estream_t stream)
|
||||||
|
{
|
||||||
|
return do_nvc_parse (result, errlinep, stream, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Write a representation of PK to STREAM. */
|
/* Write a representation of PK to STREAM. */
|
||||||
gpg_error_t
|
gpg_error_t
|
||||||
nvc_write (nvc_t pk, estream_t stream)
|
nvc_write (nvc_t pk, estream_t stream)
|
||||||
|
@ -40,10 +40,14 @@ typedef struct name_value_entry *nve_t;
|
|||||||
|
|
||||||
/* Memory management, and dealing with entries. */
|
/* Memory management, and dealing with entries. */
|
||||||
|
|
||||||
/* Allocate a private key container structure. */
|
/* Allocate a name value container structure. */
|
||||||
nvc_t nvc_new (void);
|
nvc_t nvc_new (void);
|
||||||
|
|
||||||
/* Release a private key container structure. */
|
/* Allocate a name value container structure for use with the extended
|
||||||
|
* private key format. */
|
||||||
|
nvc_t nvc_new_private_key (void);
|
||||||
|
|
||||||
|
/* Release a name value container structure. */
|
||||||
void nvc_release (nvc_t pk);
|
void nvc_release (nvc_t pk);
|
||||||
|
|
||||||
/* Get the name. */
|
/* Get the name. */
|
||||||
@ -103,6 +107,13 @@ gpg_error_t nvc_set_private_key (nvc_t pk, gcry_sexp_t sexp);
|
|||||||
parser was last considering is stored there. */
|
parser was last considering is stored there. */
|
||||||
gpg_error_t nvc_parse (nvc_t *result, int *errlinep, estream_t stream);
|
gpg_error_t nvc_parse (nvc_t *result, int *errlinep, estream_t stream);
|
||||||
|
|
||||||
|
/* Parse STREAM and return a newly allocated name value container
|
||||||
|
structure in RESULT - assuming the extended private key format. If
|
||||||
|
ERRLINEP is given, the line number the parser was last considering
|
||||||
|
is stored there. */
|
||||||
|
gpg_error_t nvc_parse_private_key (nvc_t *result, int *errlinep,
|
||||||
|
estream_t stream);
|
||||||
|
|
||||||
/* Write a representation of PK to STREAM. */
|
/* Write a representation of PK to STREAM. */
|
||||||
gpg_error_t nvc_write (nvc_t pk, estream_t stream);
|
gpg_error_t nvc_write (nvc_t pk, estream_t stream);
|
||||||
|
|
||||||
|
@ -29,6 +29,18 @@
|
|||||||
#include "name-value.h"
|
#include "name-value.h"
|
||||||
|
|
||||||
static int verbose;
|
static int verbose;
|
||||||
|
static int private_key_mode;
|
||||||
|
|
||||||
|
|
||||||
|
static nvc_t
|
||||||
|
my_nvc_new (void)
|
||||||
|
{
|
||||||
|
if (private_key_mode)
|
||||||
|
return nvc_new_private_key ();
|
||||||
|
else
|
||||||
|
return nvc_new ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
test_getting_values (nvc_t pk)
|
test_getting_values (nvc_t pk)
|
||||||
@ -55,14 +67,22 @@ test_key_extraction (nvc_t pk)
|
|||||||
gpg_error_t err;
|
gpg_error_t err;
|
||||||
gcry_sexp_t key;
|
gcry_sexp_t key;
|
||||||
|
|
||||||
err = nvc_get_private_key (pk, &key);
|
if (private_key_mode)
|
||||||
assert (err == 0);
|
{
|
||||||
assert (key);
|
err = nvc_get_private_key (pk, &key);
|
||||||
|
assert (err == 0);
|
||||||
|
assert (key);
|
||||||
|
|
||||||
if (verbose)
|
if (verbose)
|
||||||
gcry_sexp_dump (key);
|
gcry_sexp_dump (key);
|
||||||
|
|
||||||
gcry_sexp_release (key);
|
gcry_sexp_release (key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
err = nvc_get_private_key (pk, &key);
|
||||||
|
assert (gpg_err_code (err) == GPG_ERR_MISSING_KEY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -240,7 +260,10 @@ run_tests (void)
|
|||||||
0, dummy_realloc, dummy_free, "r");
|
0, dummy_realloc, dummy_free, "r");
|
||||||
assert (source);
|
assert (source);
|
||||||
|
|
||||||
err = nvc_parse (&pk, NULL, source);
|
if (private_key_mode)
|
||||||
|
err = nvc_parse_private_key (&pk, NULL, source);
|
||||||
|
else
|
||||||
|
err = nvc_parse (&pk, NULL, source);
|
||||||
assert (err == 0);
|
assert (err == 0);
|
||||||
assert (pk);
|
assert (pk);
|
||||||
|
|
||||||
@ -272,7 +295,7 @@ run_modification_tests (void)
|
|||||||
gcry_sexp_t key;
|
gcry_sexp_t key;
|
||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
pk = nvc_new ();
|
pk = my_nvc_new ();
|
||||||
assert (pk);
|
assert (pk);
|
||||||
|
|
||||||
nvc_set (pk, "Foo:", "Bar");
|
nvc_set (pk, "Foo:", "Bar");
|
||||||
@ -354,20 +377,29 @@ run_modification_tests (void)
|
|||||||
xfree (buf);
|
xfree (buf);
|
||||||
nvc_release (pk);
|
nvc_release (pk);
|
||||||
|
|
||||||
pk = nvc_new ();
|
pk = my_nvc_new ();
|
||||||
assert (pk);
|
assert (pk);
|
||||||
|
|
||||||
err = gcry_sexp_build (&key, NULL, "(hello world)");
|
err = gcry_sexp_build (&key, NULL, "(hello world)");
|
||||||
assert (err == 0);
|
assert (err == 0);
|
||||||
assert (key);
|
assert (key);
|
||||||
|
|
||||||
err = nvc_set_private_key (pk, key);
|
if (private_key_mode)
|
||||||
gcry_sexp_release (key);
|
{
|
||||||
assert (err == 0);
|
err = nvc_set_private_key (pk, key);
|
||||||
buf = nvc_to_string (pk);
|
gcry_sexp_release (key);
|
||||||
assert (strcmp (buf, "Key: (hello world)\n") == 0);
|
assert (err == 0);
|
||||||
xfree (buf);
|
|
||||||
nvc_release (pk);
|
buf = nvc_to_string (pk);
|
||||||
|
assert (strcmp (buf, "Key: (hello world)\n") == 0);
|
||||||
|
xfree (buf);
|
||||||
|
nvc_release (pk);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
err = nvc_set_private_key (pk, key);
|
||||||
|
assert (gpg_err_code (err) == GPG_ERR_MISSING_KEY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -403,7 +435,7 @@ convert (const char *fname)
|
|||||||
exit (1);
|
exit (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pk = nvc_new ();
|
pk = my_nvc_new ();
|
||||||
assert (pk);
|
assert (pk);
|
||||||
|
|
||||||
err = nvc_set_private_key (pk, key);
|
err = nvc_set_private_key (pk, key);
|
||||||
@ -437,7 +469,10 @@ parse (const char *fname)
|
|||||||
exit (1);
|
exit (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
err = nvc_parse (&pk_a, &line, source);
|
if (private_key_mode)
|
||||||
|
err = nvc_parse_private_key (&pk_a, &line, source);
|
||||||
|
else
|
||||||
|
err = nvc_parse (&pk_a, &line, source);
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
fprintf (stderr, "failed to parse %s line %d: %s\n",
|
fprintf (stderr, "failed to parse %s line %d: %s\n",
|
||||||
@ -448,14 +483,14 @@ parse (const char *fname)
|
|||||||
buf = nvc_to_string (pk_a);
|
buf = nvc_to_string (pk_a);
|
||||||
xfree (buf);
|
xfree (buf);
|
||||||
|
|
||||||
pk_b = nvc_new ();
|
pk_b = my_nvc_new ();
|
||||||
assert (pk_b);
|
assert (pk_b);
|
||||||
|
|
||||||
for (e = nvc_first (pk_a); e; e = nve_next (e))
|
for (e = nvc_first (pk_a); e; e = nve_next (e))
|
||||||
{
|
{
|
||||||
gcry_sexp_t key = NULL;
|
gcry_sexp_t key = NULL;
|
||||||
|
|
||||||
if (strcasecmp (nve_name (e), "Key:") == 0)
|
if (private_key_mode && !strcasecmp (nve_name (e), "Key:"))
|
||||||
{
|
{
|
||||||
err = nvc_get_private_key (pk_a, &key);
|
err = nvc_get_private_key (pk_a, &key);
|
||||||
if (err)
|
if (err)
|
||||||
@ -487,7 +522,8 @@ print_usage (void)
|
|||||||
fprintf (stderr,
|
fprintf (stderr,
|
||||||
"usage: t-private-keys [--verbose]"
|
"usage: t-private-keys [--verbose]"
|
||||||
" [--convert <private-key-file>"
|
" [--convert <private-key-file>"
|
||||||
" || --parse <extended-private-key-file>]\n");
|
" || --parse-key <extended-private-key-file>"
|
||||||
|
" || --parse <file> ]\n");
|
||||||
exit (2);
|
exit (2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -495,7 +531,7 @@ print_usage (void)
|
|||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
enum { TEST, CONVERT, PARSE } command = TEST;
|
enum { TEST, CONVERT, PARSE, PARSEKEY } command = TEST;
|
||||||
|
|
||||||
if (argc)
|
if (argc)
|
||||||
{ argc--; argv++; }
|
{ argc--; argv++; }
|
||||||
@ -513,6 +549,14 @@ main (int argc, char **argv)
|
|||||||
print_usage ();
|
print_usage ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (argc && !strcmp (argv[0], "--parse-key"))
|
||||||
|
{
|
||||||
|
command = PARSEKEY;
|
||||||
|
argc--; argv++;
|
||||||
|
if (argc != 1)
|
||||||
|
print_usage ();
|
||||||
|
}
|
||||||
|
|
||||||
if (argc && !strcmp (argv[0], "--parse"))
|
if (argc && !strcmp (argv[0], "--parse"))
|
||||||
{
|
{
|
||||||
command = PARSE;
|
command = PARSE;
|
||||||
@ -524,6 +568,9 @@ main (int argc, char **argv)
|
|||||||
switch (command)
|
switch (command)
|
||||||
{
|
{
|
||||||
case TEST:
|
case TEST:
|
||||||
|
run_tests ();
|
||||||
|
run_modification_tests ();
|
||||||
|
private_key_mode = 1;
|
||||||
run_tests ();
|
run_tests ();
|
||||||
run_modification_tests ();
|
run_modification_tests ();
|
||||||
break;
|
break;
|
||||||
@ -532,6 +579,11 @@ main (int argc, char **argv)
|
|||||||
convert (*argv);
|
convert (*argv);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case PARSEKEY:
|
||||||
|
private_key_mode = 1;
|
||||||
|
parse (*argv);
|
||||||
|
break;
|
||||||
|
|
||||||
case PARSE:
|
case PARSE:
|
||||||
parse (*argv);
|
parse (*argv);
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user