Commit 72aca63ba8 for strongswan.org

commit 72aca63ba844ca259f8030bdce77e83fbeb25e5c
Author: Tobias Brunner <tobias@strongswan.org>
Date:   Wed Mar 11 18:13:15 2026 +0100

    pki: Add support to generate ECDSA keys with non-NIST curves

diff --git a/src/pki/commands/gen.c b/src/pki/commands/gen.c
index 89a2fa8c85..a78bcfb321 100644
--- a/src/pki/commands/gen.c
+++ b/src/pki/commands/gen.c
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2026 Tobias Brunner
  * Copyright (C) 2009 Martin Willi
  * Copyright (C) 2014-2016 Andreas Steffen
  *
@@ -17,6 +18,57 @@

 #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
  */
@@ -26,7 +78,7 @@ static int gen()
 	key_type_t type = KEY_RSA;
 	u_int size = 0, shares = 0, threshold = 1;
 	private_key_t *key;
-	chunk_t encoding;
+	chunk_t encoding, curve = chunk_empty;
 	bool safe_primes = FALSE;
 	char *arg;

@@ -70,6 +122,13 @@ static int gen()
 					return command_usage("invalid key type");
 				}
 				continue;
+			case 'c':
+				chunk_free(&curve);
+				if (!get_curve(arg, &curve))
+				{
+					return command_usage("invalid elliptic curve");
+				}
+				continue;
 			case 'f':
 				if (!get_form(arg, &form, CRED_PRIVATE_KEY))
 				{
@@ -138,11 +197,17 @@ static int gen()
 		key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
 							BUILD_KEY_SIZE, size, BUILD_SAFE_PRIMES, BUILD_END);
 	}
+	else if (type == KEY_ECDSA && curve.len)
+	{
+		key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
+							BUILD_ECDSA_CURVE, curve, BUILD_END);
+	}
 	else
 	{
 		key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
 							BUILD_KEY_SIZE, size, BUILD_END);
 	}
+	chunk_free(&curve);
 	if (!key)
 	{
 		fprintf(stderr, "private key generation failed\n");
@@ -173,12 +238,14 @@ static void __attribute__ ((constructor))reg()
 {
 	command_register((command_t) {
 		gen, 'g', "gen", "generate a new private key",
-		{"[--type rsa|ecdsa|ed25519|ed448|mldsa44|mldsa65|mldsa87] [--size bits]",
-		 "[--safe-primes] [--shares n] [--threshold l] [--outform der|pem]"},
+		{"[--type rsa|ecdsa|ed25519|ed448|mldsa44|mldsa65|mldsa87]",
+		 "[--size bits] [--curve <name|oid>] [--safe-primes]",
+		 "[--shares n] [--threshold l] [--outform der|pem]"},
 		{
 			{"help",		'h', 0, "show usage information"},
 			{"type",		't', 1, "type of key, default: rsa"},
 			{"size",		's', 1, "keylength in bits, default: rsa 2048, ecdsa 384"},
+			{"curve",		'c', 1, "curve for ecdsa key (name or oid, --size is ignored)"},
 			{"safe-primes", 'p', 0, "generate rsa safe primes"},
 			{"shares",		'n', 1, "number of private rsa key shares"},
 			{"threshold",	'l', 1, "minimum number of participating rsa key shares"},
diff --git a/src/pki/man/pki---gen.1.in b/src/pki/man/pki---gen.1.in
index 1ea4cede8c..8b7b3acd8d 100644
--- a/src/pki/man/pki---gen.1.in
+++ b/src/pki/man/pki---gen.1.in
@@ -9,6 +9,7 @@ pki \-\-gen \- Generate a new RSA or ECDSA private key
 .SY pki\ \-\-gen
 .OP \-\-type type
 .OP \-\-size bits
+.OP \-\-curve name|oid
 .OP \-\-safe\-primes
 .OP \-\-shares n
 .OP \-\-threshold l
@@ -51,7 +52,13 @@ Type of key to generate. Either \fIrsa\fR, \fIecdsa\fR, \fIed25519\fR,
 .TP
 .BI "\-s, \-\-size " bits
 Key length in bits. Defaults to 2048 for \fIrsa\fR and 384 for \fIecdsa\fR.
-For \fIecdsa\fR only three values are currently supported: 256, 384 and 521.
+For \fIecdsa\fR, only three values are supported: 256, 384 and 521, which
+correspond to the three NIST curves of that size (ignored if \-\-curve is used).
+.TP
+.BI "\-c, \-\-curve " name|oid
+Elliptic curve to use for \fIecdsa\fR. See below for the list of known curves.
+Other curves may be used via OID (e.g. 1.3.132.0.34 for NIST P-384) if supported
+by the ECDSA implementation.
 .TP
 .BI "\-p, \-\-safe\-primes"
 Generate RSA safe primes.
@@ -69,6 +76,29 @@ Number of private RSA key shares.
 .BI "\-l, \-\-threshold " <l>
 Minimum number of participating RSA key shares.
 .
+.SH "KNOWN ELLIPTIC CURVES"
+.
+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"
+256-bit NIST curve P-256 (1.2.840.10045.3.1.7)
+.TP
+.B "p384, p-384, secp384r1"
+384-bit NIST curve P-384 (1.3.132.0.34)
+.TP
+.B "p521, p-521, secp521r1"
+521-bit NIST curve P-521 (1.3.132.0.35)
+.TP
+.B "bp256, brainpoolP256r1"
+256-bit Brainpool curve (1.3.36.3.3.2.8.1.1.7)
+.TP
+.B "bp384, brainpoolP384r1"
+384-bit Brainpool curve (1.3.36.3.3.2.8.1.1.11)
+.TP
+.B "bp512, brainpoolP512r1"
+512-bit Brainpool curve (1.3.36.3.3.2.8.1.1.13)
+.
 .SH "PROBLEMS ON HOSTS WITH LOW ENTROPY"
 .
 If the