From 03bf8e967adb2dd13329ba1089deb419d49e55c0 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 8 Feb 2019 12:10:45 +0100 Subject: [PATCH] common: Provide function to get public key algo names in our format. * tools/card-tool-misc.c (pubkey_algo_string): Move to ... * common/sexputil.c (pubkey_algo_string): here. -- The new gpg format for public key algorithms is useful at other places as well. Thus we make this new function available. Note that the code we use in gpg is not based on s-expressions and thus a new function was required. Signed-off-by: Werner Koch --- common/sexputil.c | 58 ++++++++++++++++++++++++++++++++++++++++++ common/util.h | 1 + tools/card-tool-misc.c | 58 ------------------------------------------ tools/card-tool.h | 2 -- 4 files changed, 59 insertions(+), 60 deletions(-) diff --git a/common/sexputil.c b/common/sexputil.c index 02e52d0ed..d3020e169 100644 --- a/common/sexputil.c +++ b/common/sexputil.c @@ -577,3 +577,61 @@ get_pk_algo_from_canon_sexp (const unsigned char *keydata, size_t keydatalen) gcry_sexp_release (sexp); return algo; } + + +/* Given the public key S_PKEY, return a new buffer with a descriptive + * string for its algorithm. This function may return NULL on memory + * error. */ +char * +pubkey_algo_string (gcry_sexp_t s_pkey) +{ + const char *prefix; + gcry_sexp_t l1; + char *algoname; + int algo; + char *result; + + l1 = gcry_sexp_find_token (s_pkey, "public-key", 0); + if (!l1) + return xtrystrdup ("E_no_key"); + { + gcry_sexp_t l_tmp = gcry_sexp_cadr (l1); + gcry_sexp_release (l1); + l1 = l_tmp; + } + algoname = gcry_sexp_nth_string (l1, 0); + gcry_sexp_release (l1); + if (!algoname) + return xtrystrdup ("E_no_algo"); + + algo = gcry_pk_map_name (algoname); + switch (algo) + { + case GCRY_PK_RSA: prefix = "rsa"; break; + case GCRY_PK_ELG: prefix = "elg"; break; + case GCRY_PK_DSA: prefix = "dsa"; break; + case GCRY_PK_ECC: prefix = ""; break; + default: prefix = NULL; break; + } + + if (prefix && *prefix) + result = xtryasprintf ("%s%u", prefix, gcry_pk_get_nbits (s_pkey)); + else if (prefix) + { + const char *curve = gcry_pk_get_curve (s_pkey, 0, NULL); + const char *name = openpgp_oid_to_curve + (openpgp_curve_to_oid (curve, NULL), 0); + + if (name) + result = xtrystrdup (name); + else if (curve) + result = xtryasprintf ("X_%s", curve); + else + result = xtrystrdup ("E_unknown"); + } + else + result = xtryasprintf ("X_algo_%d", algo); + + xfree (algoname); + return result; +} diff --git a/common/util.h b/common/util.h index 863f9e36f..d5bb225a7 100644 --- a/common/util.h +++ b/common/util.h @@ -189,6 +189,7 @@ gpg_error_t get_rsa_pk_from_canon_sexp (const unsigned char *keydata, int get_pk_algo_from_key (gcry_sexp_t key); int get_pk_algo_from_canon_sexp (const unsigned char *keydata, size_t keydatalen); +char *pubkey_algo_string (gcry_sexp_t s_pkey); /*-- convert.c --*/ int hex2bin (const char *string, void *buffer, size_t length); diff --git a/tools/card-tool-misc.c b/tools/card-tool-misc.c index 5e0461cb8..06fcb6705 100644 --- a/tools/card-tool-misc.c +++ b/tools/card-tool-misc.c @@ -77,61 +77,3 @@ hex_to_buffer (const char *string, size_t *r_length) *r_length = n; return buffer; } - - - -/* Given the public key S_PKEY, return a new buffer with a descriptive - * string for its algorithm. This function always returns a string. */ -char * -pubkey_algo_string (gcry_sexp_t s_pkey) -{ - const char *prefix; - gcry_sexp_t l1; - char *algoname; - int algo; - char *result; - - l1 = gcry_sexp_find_token (s_pkey, "public-key", 0); - if (!l1) - return xstrdup ("E_no_key"); - { - gcry_sexp_t l_tmp = gcry_sexp_cadr (l1); - gcry_sexp_release (l1); - l1 = l_tmp; - } - algoname = gcry_sexp_nth_string (l1, 0); - gcry_sexp_release (l1); - if (!algoname) - return xstrdup ("E_no_algo"); - - algo = gcry_pk_map_name (algoname); - switch (algo) - { - case GCRY_PK_RSA: prefix = "rsa"; break; - case GCRY_PK_ELG: prefix = "elg"; break; - case GCRY_PK_DSA: prefix = "dsa"; break; - case GCRY_PK_ECC: prefix = ""; break; - default: prefix = NULL; break; - } - - if (prefix && *prefix) - result = xasprintf ("%s%u", prefix, gcry_pk_get_nbits (s_pkey)); - else if (prefix) - { - const char *curve = gcry_pk_get_curve (s_pkey, 0, NULL); - const char *name = openpgp_oid_to_curve - (openpgp_curve_to_oid (curve, NULL), 0); - - if (name) - result = xstrdup (name); - else if (curve) - result = xasprintf ("X_%s", curve); - else - result = xstrdup ("E_unknown"); - } - else - result = xasprintf ("X_algo_%d", algo); - - xfree (algoname); - return result; -} diff --git a/tools/card-tool.h b/tools/card-tool.h index 9daf7e498..f49f253ce 100644 --- a/tools/card-tool.h +++ b/tools/card-tool.h @@ -192,8 +192,6 @@ gpg_error_t test_get_matching_keys (const char *hexgrip); /*-- card-tool-misc.c --*/ key_info_t find_kinfo (card_info_t info, const char *keyref); void *hex_to_buffer (const char *string, size_t *r_length); -char *pubkey_algo_string (gcry_sexp_t s_pkey); - /*-- card-call-scd.c --*/ void release_card_info (card_info_t info);