Skip to content

Commit

Permalink
squash tools/pkcs15-tool.c
Browse files Browse the repository at this point in the history
Accept any case of curve names but pass corrected cast to lower level routines.

 On branch X25519-improvements-2
 Changes to be committed:
	modified:   tools/pkcs15-init.c
  • Loading branch information
dengert committed Jan 20, 2024
1 parent 8308ee7 commit 975a327
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/tools/pkcs15-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,23 +721,40 @@ static const struct alg_spec alg_types_sym[] = {
{ NULL, -1, 0 }
};

/* RSA can have a number , defaulr is 2048 */
/* EC require a curve name */
/* EDDSA and XEDDSA without a size require a size or curve name or OID */
/* other EDDSA and XEDDSA can be used alone */
static const struct alg_spec alg_types_asym[] = {
{ "rsa", SC_ALGORITHM_RSA, 1024 },
{ "rsa", SC_ALGORITHM_RSA, 2048 }, /* new default */
{ "gost2001", SC_ALGORITHM_GOSTR3410, SC_PKCS15_GOSTR3410_KEYSIZE },
{ "ec", SC_ALGORITHM_EC, 0 },
{ "eddsa", SC_ALGORITHM_EDDSA, 0 },
{ "EdDSA", SC_ALGORITHM_EDDSA, 0 }, /* RFC 8410 section 8 */
{ "xeddsa", SC_ALGORITHM_XEDDSA, 0 },
{ "ECDH", SC_ALGORITHM_XEDDSA, 0 }, /* RFC 8410 section 8 */
/* RFC 8410 */
{ "Ed25519", SC_ALGORITHM_EDDSA, 255 }, /* RFC 8410 and gunpg */
{ "Ed448", SC_ALGORITHM_EDDSA, 448 },
{ "X25519", SC_ALGORITHM_XEDDSA, 255 },
{ "X448", SC_ALGORITHM_XEDDSA, 448 },
/* used by Yubikey and GNUK */
{ "edwards25519", SC_ALGORITHM_EDDSA, 255 },
{ "curve25519", SC_ALGORITHM_XEDDSA, 255 },
/* gnupg */
{ "cv25519", SC_ALGORITHM_XEDDSA, 255 },

{ NULL, -1, 0 }
};

static int
parse_alg_spec(const struct alg_spec *types, const char *spec, unsigned int *keybits, struct sc_pkcs15_prkey *prkey)
{
int i, algorithm = -1;
int i, types_idx = -1, algorithm = -1;
char *end;

for (i = 0; types[i].spec; i++) {
if (!strncasecmp(spec, types[i].spec, strlen(types[i].spec))) {
types_idx = i; /* save index of types array */
algorithm = types[i].algorithm;
*keybits = types[i].keybits;
spec += strlen(types[i].spec);
Expand All @@ -751,13 +768,21 @@ parse_alg_spec(const struct alg_spec *types, const char *spec, unsigned int *key

if (*spec == '/' || *spec == '-' || *spec == ':')
spec++;

/* if we have eveything for EDDSA or XEDDSA */
if (*spec == 0x00 && *keybits && (algorithm == SC_ALGORITHM_EDDSA || SC_ALGORITHM_XEDDSA) && prkey) {
prkey->u.ec.params.named_curve = strdup(types[types_idx].spec); /* correct case */
*keybits = types[types_idx].keybits;
return algorithm;
}

if (*spec) {
if (isalpha((unsigned char)*spec)
&& (algorithm == SC_ALGORITHM_EC || algorithm == SC_ALGORITHM_EDDSA || SC_ALGORITHM_XEDDSA)
&& prkey) {
prkey->u.ec.params.named_curve = strdup(spec);
prkey->u.ec.params.named_curve = strdup(types[types_idx].spec); /* copy correct case */
} else {
/*TODO DEE could uses algorithm and keybits */
*keybits = strtoul(spec, &end, 10);
if (*end) {
util_error("Invalid number of key bits \"%s\"", spec);
Expand Down

0 comments on commit 975a327

Please sign in to comment.