Commit 2847bbe11b for strongswan.org

commit 2847bbe11b27114910243815aca62a1d4e9baccd
Author: Tobias Brunner <tobias@strongswan.org>
Date:   Fri Nov 13 13:37:14 2026 +0100

    public-key: Add helper to convert a curve name/OID to a DER-encoded OID

diff --git a/src/libstrongswan/credentials/keys/public_key.c b/src/libstrongswan/credentials/keys/public_key.c
index 4f8d0e405d..160892284b 100644
--- a/src/libstrongswan/credentials/keys/public_key.c
+++ b/src/libstrongswan/credentials/keys/public_key.c
@@ -445,6 +445,55 @@ enumerator_t *signature_schemes_for_key(key_type_t type, int size)
 	return &this->public;
 }

+/**
+ * Known elliptic curves.
+ */
+static const struct {
+	const char *name;
+	int oid;
+} known_curves[] = {
+	{ "p256", OID_PRIME256V1, },
+	{ "p384", OID_SECP384R1, },
+	{ "p521", OID_SECP521R1, },
+	{ "p-256", OID_PRIME256V1, },
+	{ "p-384", OID_SECP384R1, },
+	{ "p-521", OID_SECP521R1, },
+	{ "secp256r1", OID_PRIME256V1, },
+	{ "secp384r1", OID_SECP384R1, },
+	{ "secp521r1", OID_SECP521R1, },
+	{ "prime256v1", OID_PRIME256V1, },
+	{ "bp256", OID_BRAINPOOLP256R1, },
+	{ "bp384", OID_BRAINPOOLP384R1, },
+	{ "bp512", OID_BRAINPOOLP512R1, },
+	{ "brainpoolP256r1", OID_BRAINPOOLP256R1, },
+	{ "brainpoolP384r1", OID_BRAINPOOLP384R1, },
+	{ "brainpoolP512r1", OID_BRAINPOOLP512R1, },
+};
+
+/*
+ * Described in header
+ */
+chunk_t ecdsa_curve_from_string(char *str)
+{
+	chunk_t oid;
+	int i;
+
+	if (strchr(str, '.'))
+	{
+		oid = asn1_oid_from_string(str);
+		return oid.len ? asn1_wrap(ASN1_OID, "m", oid) : chunk_empty;
+	}
+
+	for (i = 0; i < countof(known_curves); i++)
+	{
+		if (strcaseeq(str, known_curves[i].name))
+		{
+			return asn1_build_known_oid(known_curves[i].oid);
+		}
+	}
+	return chunk_empty;
+}
+
 /*
  * Described in header
  */
diff --git a/src/libstrongswan/credentials/keys/public_key.h b/src/libstrongswan/credentials/keys/public_key.h
index 09ecaac20a..6e4a2ee8a5 100644
--- a/src/libstrongswan/credentials/keys/public_key.h
+++ b/src/libstrongswan/credentials/keys/public_key.h
@@ -309,6 +309,14 @@ key_type_t key_type_from_oid(int oid);
  */
 signature_scheme_t signature_scheme_from_oid(int oid);

+/**
+ * Convert a curve name or OID in dotted notation to a DER-encoded OID.
+ *
+ * @param str			name or OID
+ * @return				DER-encoded OID (allocated), chunk_empty on error
+ */
+chunk_t ecdsa_curve_from_string(char *str);
+
 /**
  * Extract a DER-encoded curve OID from a subjectPublicKeyInfo structure.
  *
diff --git a/src/pki/commands/gen.c b/src/pki/commands/gen.c
index a78bcfb321..f40df62379 100644
--- a/src/pki/commands/gen.c
+++ b/src/pki/commands/gen.c
@@ -18,57 +18,6 @@

 #include "pki.h"

-#include <asn1/asn1.h>
-#include <asn1/oid.h>
-
-/**
- * Known elliptic curves.
- */
-static const struct {
-	const char *name;
-	int oid;
-} known_curves[] = {
-	{ "p256", OID_PRIME256V1, },
-	{ "p384", OID_SECP384R1, },
-	{ "p521", OID_SECP521R1, },
-	{ "p-256", OID_PRIME256V1, },
-	{ "p-384", OID_SECP384R1, },
-	{ "p-521", OID_SECP521R1, },
-	{ "secp256r1", OID_PRIME256V1, },
-	{ "secp384r1", OID_SECP384R1, },
-	{ "secp521r1", OID_SECP521R1, },
-	{ "bp256", OID_BRAINPOOLP256R1, },
-	{ "bp384", OID_BRAINPOOLP384R1, },
-	{ "bp512", OID_BRAINPOOLP512R1, },
-	{ "brainpoolP256r1", OID_BRAINPOOLP256R1, },
-	{ "brainpoolP384r1", OID_BRAINPOOLP384R1, },
-	{ "brainpoolP512r1", OID_BRAINPOOLP512R1, },
-};
-
-/**
- * Try to find an elliptic curve with the given name or parse it as an OID.
- */
-static bool get_curve(char *name, chunk_t *curve)
-{
-	int i;
-
-	if (strchr(name, '.'))
-	{
-		*curve = asn1_wrap(ASN1_OID, "m", asn1_oid_from_string(name));
-		return TRUE;
-	}
-
-	for (i = 0; i < countof(known_curves); i++)
-	{
-		if (strcaseeq(name, known_curves[i].name))
-		{
-			*curve = asn1_build_known_oid(known_curves[i].oid);
-			return TRUE;
-		}
-	}
-	return FALSE;
-}
-
 /**
  * Generate a private key
  */
@@ -124,7 +73,8 @@ static int gen()
 				continue;
 			case 'c':
 				chunk_free(&curve);
-				if (!get_curve(arg, &curve))
+				curve = ecdsa_curve_from_string(arg);
+				if (!curve.len)
 				{
 					return command_usage("invalid elliptic curve");
 				}
diff --git a/src/pki/man/pki---gen.1.in b/src/pki/man/pki---gen.1.in
index 8b7b3acd8d..96bc083d68 100644
--- a/src/pki/man/pki---gen.1.in
+++ b/src/pki/man/pki---gen.1.in
@@ -81,7 +81,7 @@ Minimum number of participating RSA key shares.
 Below is a list of the known elliptic curves that can be used for ECDSA
 keys. The names/aliases are matched case-insensitive.
 .TP
-.B "p256, p-256, secp256r1"
+.B "p256, p-256, secp256r1, prime256v1"
 256-bit NIST curve P-256 (1.2.840.10045.3.1.7)
 .TP
 .B "p384, p-384, secp384r1"