Commit a2534160e1 for strongswan.org
commit a2534160e1edd4b6e1dcf9c259d597f33c1bc400
Author: Tobias Brunner <tobias@strongswan.org>
Date: Wed Mar 11 18:11:07 2026 +0100
botan: Add support for non-NIST elliptic curves for ECDSA
Botan doesn't provide an API to handle binary OIDs, so we convert them
to dotted notation. It also requires a name to identify the curve, so
we do another conversion.
Because the OID API is required, which is only available since 3.8.0, we
use a fallback to support at least the NIST curves with older versions.
diff --git a/configure.ac b/configure.ac
index 96f5563656..c7c0ea5cfb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1151,7 +1151,7 @@ if test x$botan = xtrue; then
AC_SUBST(botan_LIBS)
saved_LIBS=$LIBS
LIBS="$botan_LIBS"
- AC_CHECK_FUNCS(botan_rng_init_custom botan_pubkey_ecc_key_used_explicit_encoding)
+ AC_CHECK_FUNCS(botan_rng_init_custom botan_pubkey_ecc_key_used_explicit_encoding botan_oid_from_string)
LIBS=$saved_LIBS
fi
diff --git a/src/libstrongswan/plugins/botan/botan_ec_private_key.c b/src/libstrongswan/plugins/botan/botan_ec_private_key.c
index 79b7e81e29..2b043382a5 100644
--- a/src/libstrongswan/plugins/botan/botan_ec_private_key.c
+++ b/src/libstrongswan/plugins/botan/botan_ec_private_key.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2018 Tobias Brunner
+ * Copyright (C) 2018-2026 Tobias Brunner
*
* Copyright (C) 2018 René Korthaus
* Copyright (C) 2018 Konstantinos Kolelis
@@ -57,11 +57,6 @@ struct private_botan_ec_private_key_t {
*/
botan_privkey_t key;
- /**
- * OID of the curve
- */
- int oid;
-
/**
* Reference count
*/
@@ -236,7 +231,7 @@ METHOD(private_key_t, destroy, void,
/**
* Internal generic constructor
*/
-static private_botan_ec_private_key_t *create_empty(int oid)
+static private_botan_ec_private_key_t *create_empty()
{
private_botan_ec_private_key_t *this;
@@ -257,21 +252,33 @@ static private_botan_ec_private_key_t *create_empty(int oid)
.destroy = _destroy,
},
},
- .oid = oid,
.ref = 1,
);
return this;
}
+#ifdef HAVE_BOTAN_OID_FROM_STRING
+/**
+ * View callback that copies the given string.
+ */
+static int botan_view_str_alloc(botan_view_ctx ctx, const char *str, size_t len)
+{
+ char **out = (char**)ctx;
+
+ *out = strdup(str);
+ return 0;
+}
+#endif /* HAVE_BOTAN_OID_FROM_STRING */
+
/*
* Described in header
*/
-botan_ec_private_key_t *botan_ec_private_key_adopt(botan_privkey_t key, int oid)
+botan_ec_private_key_t *botan_ec_private_key_adopt(botan_privkey_t key)
{
private_botan_ec_private_key_t *this;
- this = create_empty(oid);
+ this = create_empty();
this->key = key;
return &this->public;
@@ -284,9 +291,9 @@ botan_ec_private_key_t *botan_ec_private_key_gen(key_type_t type, va_list args)
{
private_botan_ec_private_key_t *this;
botan_rng_t rng;
+ chunk_t curve = chunk_empty;
u_int key_size = 0;
- int oid;
- const char *curve;
+ char *curvename = NULL;
while (TRUE)
{
@@ -295,6 +302,9 @@ botan_ec_private_key_t *botan_ec_private_key_gen(key_type_t type, va_list args)
case BUILD_KEY_SIZE:
key_size = va_arg(args, u_int);
continue;
+ case BUILD_ECDSA_CURVE:
+ curve = va_arg(args, chunk_t);
+ continue;
case BUILD_END:
break;
default:
@@ -303,47 +313,78 @@ botan_ec_private_key_t *botan_ec_private_key_gen(key_type_t type, va_list args)
break;
}
- if (!key_size)
+ if (curve.len)
{
- return NULL;
- }
+ if (asn1_unwrap(&curve, &curve) == ASN1_OID)
+ {
+#ifdef HAVE_BOTAN_OID_FROM_STRING
+ char *dotted = asn1_oid_to_string(curve);
+ botan_asn1_oid_t oid;
- switch (key_size)
+ if (dotted)
+ {
+ if (!botan_oid_from_string(&oid, dotted))
+ {
+ botan_oid_view_name(oid, &curvename, botan_view_str_alloc);
+ botan_oid_destroy(oid);
+ }
+ free(dotted);
+ }
+#else /* HAVE_BOTAN_OID_FROM_STRING */
+ switch (asn1_known_oid(curve))
+ {
+ case OID_PRIME256V1:
+ curvename = strdup("secp256r1");
+ break;
+ case OID_SECP384R1:
+ curvename = strdup("secp384r1");
+ break;
+ case OID_SECP521R1:
+ curvename = strdup("secp521r1");
+ break;
+ default:
+ break;
+ }
+#endif /* HAVE_BOTAN_OID_FROM_STRING */
+ }
+ }
+ else if (key_size)
{
- case 256:
- curve = "secp256r1";
- oid = OID_PRIME256V1;
- break;
- case 384:
- curve = "secp384r1";
- oid = OID_SECP384R1;
- break;
- case 521:
- curve = "secp521r1";
- oid = OID_SECP521R1;
- break;
- default:
- DBG1(DBG_LIB, "EC private key size %d not supported via botan",
- key_size);
- return NULL;
+ switch (key_size)
+ {
+ case 256:
+ curvename = strdup("secp256r1");
+ break;
+ case 384:
+ curvename = strdup("secp384r1");
+ break;
+ case 521:
+ curvename = strdup("secp521r1");
+ break;
+ default:
+ DBG1(DBG_LIB, "EC private key size %d not supported via botan",
+ key_size);
+ break;
+ }
}
-
- if (!botan_get_rng(&rng, RNG_TRUE))
+ if (!curvename || !botan_get_rng(&rng, RNG_TRUE))
{
+ free(curvename);
return NULL;
}
- this = create_empty(oid);
+ this = create_empty();
- if (botan_privkey_create(&this->key, "ECDSA", curve, rng))
+ if (botan_privkey_create(&this->key, "ECDSA", curvename, rng))
{
DBG1(DBG_LIB, "EC private key generation failed");
botan_rng_destroy(rng);
+ free(curvename);
free(this);
return NULL;
}
-
botan_rng_destroy(rng);
+ free(curvename);
return &this->public;
}
@@ -356,7 +397,6 @@ botan_ec_private_key_t *botan_ec_private_key_load(key_type_t type, va_list args)
chunk_t params = chunk_empty, key = chunk_empty;
chunk_t alg_id = chunk_empty, pkcs8 = chunk_empty;
botan_rng_t rng;
- int oid = OID_UNKNOWN;
while (TRUE)
{
@@ -386,9 +426,9 @@ botan_ec_private_key_t *botan_ec_private_key_load(key_type_t type, va_list args)
/* if ECParameters is passed, just use it */
alg_id = asn1_algorithmIdentifier_params(OID_EC_PUBLICKEY,
chunk_clone(params));
- if (asn1_unwrap(¶ms, ¶ms) == ASN1_OID)
- {
- oid = asn1_known_oid(params);
+ if (asn1_unwrap(¶ms, ¶ms) != ASN1_OID)
+ { /* only accept named curves */
+ chunk_free(&alg_id);
}
}
else
@@ -406,18 +446,13 @@ botan_ec_private_key_t *botan_ec_private_key_load(key_type_t type, va_list args)
asn1_unwrap(&unwrap, &inner) == ASN1_CONTEXT_C_0 &&
asn1_unwrap(&inner, &inner) == ASN1_OID)
{
- oid = asn1_known_oid(inner);
- if (oid != OID_UNKNOWN)
- {
- alg_id = asn1_algorithmIdentifier_params(OID_EC_PUBLICKEY,
- asn1_simple_object(ASN1_OID, inner));
- }
+ alg_id = asn1_algorithmIdentifier_params(OID_EC_PUBLICKEY,
+ asn1_simple_object(ASN1_OID, inner));
}
}
- if (oid == OID_UNKNOWN)
+ if (!alg_id.ptr)
{
- chunk_free(&alg_id);
return NULL;
}
@@ -426,7 +461,7 @@ botan_ec_private_key_t *botan_ec_private_key_load(key_type_t type, va_list args)
alg_id,
asn1_wrap(ASN1_OCTET_STRING, "c", key));
- this = create_empty(oid);
+ this = create_empty();
if (!botan_get_rng(&rng, RNG_STRONG))
{
diff --git a/src/libstrongswan/plugins/botan/botan_ec_private_key.h b/src/libstrongswan/plugins/botan/botan_ec_private_key.h
index 62fdf44082..188a4f5d2b 100644
--- a/src/libstrongswan/plugins/botan/botan_ec_private_key.h
+++ b/src/libstrongswan/plugins/botan/botan_ec_private_key.h
@@ -53,7 +53,7 @@ struct botan_ec_private_key_t {
/**
* Generate a ECDSA private key using Botan.
*
- * Accepts the BUILD_KEY_SIZE argument.
+ * Accepts a BUILD_ECDSA_CURVE or BUILD_KEY_SIZE argument.
*
* @param type type of the key, must be KEY_ECDSA
* @param args builder_part_t argument list
@@ -77,10 +77,8 @@ botan_ec_private_key_t *botan_ec_private_key_load(key_type_t type,
* Load a ECDSA private key by adopting a botan_privkey_t object.
*
* @param key private key object (adopted)
- * @param oid EC curve OID
* @return loaded key, NULL on failure
*/
-botan_ec_private_key_t *botan_ec_private_key_adopt(botan_privkey_t key,
- int oid);
+botan_ec_private_key_t *botan_ec_private_key_adopt(botan_privkey_t key);
#endif /** BOTAN_EC_PRIVATE_KEY_H_ @}*/
diff --git a/src/libstrongswan/plugins/botan/botan_util_keys.c b/src/libstrongswan/plugins/botan/botan_util_keys.c
index 710d992cfa..2ffa40d53f 100644
--- a/src/libstrongswan/plugins/botan/botan_util_keys.c
+++ b/src/libstrongswan/plugins/botan/botan_util_keys.c
@@ -161,28 +161,6 @@ public_key_t *botan_public_key_load(key_type_t type, va_list args)
return this;
}
-#ifdef BOTAN_HAS_ECDSA
-/**
- * Determine the curve OID from a PKCS#8 structure
- */
-static int determine_ec_oid(chunk_t pkcs8)
-{
- int oid = OID_UNKNOWN;
- chunk_t inner, params = chunk_empty;
-
- if (asn1_unwrap(&pkcs8, &pkcs8) == ASN1_SEQUENCE &&
- asn1_unwrap(&pkcs8, &inner) == ASN1_INTEGER &&
- asn1_parse_integer_uint64(inner) == 0 &&
- asn1_parse_algorithmIdentifier(pkcs8, 0, ¶ms) == OID_EC_PUBLICKEY &&
- params.len &&
- asn1_unwrap(¶ms, ¶ms) == ASN1_OID)
- {
- oid = asn1_known_oid(params);
- }
- return oid;
-}
-#endif
-
#ifdef BOTAN_HAS_ML_DSA
/**
* Determine the ML-DSA private key type from a PKCS#8 structure
@@ -263,11 +241,7 @@ private_key_t *botan_private_key_load(key_type_t type, va_list args)
#ifdef BOTAN_HAS_ECDSA
if (streq(name, "ECDSA") && (type == KEY_ANY || type == KEY_ECDSA))
{
- int oid = determine_ec_oid(blob);
- if (oid != OID_UNKNOWN)
- {
- this = (private_key_t*)botan_ec_private_key_adopt(key, oid);
- }
+ this = (private_key_t*)botan_ec_private_key_adopt(key);
}
else
#endif