Commit 3c0047919e for strongswan.org

commit 3c0047919eba420f6de08e3cf4be31cbb2f6fab0
Author: Tobias Brunner <tobias@strongswan.org>
Date:   Wed Mar 11 17:08:46 2026 +0100

    openssl: Add support for non-NIST elliptic curves for ECDSA

diff --git a/src/libstrongswan/plugins/openssl/openssl_ec_private_key.c b/src/libstrongswan/plugins/openssl/openssl_ec_private_key.c
index 9f9319024b..f4802b6fb9 100644
--- a/src/libstrongswan/plugins/openssl/openssl_ec_private_key.c
+++ b/src/libstrongswan/plugins/openssl/openssl_ec_private_key.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2016 Tobias Brunner
+ * Copyright (C) 2008-2026 Tobias Brunner
  * Copyright (C) 2009 Martin Willi
  *
  * Copyright (C) secunet Security Networks AG
@@ -338,7 +338,9 @@ openssl_ec_private_key_t *openssl_ec_private_key_gen(key_type_t type,
 {
 	private_openssl_ec_private_key_t *this;
 	EVP_PKEY *key = NULL;
+	chunk_t curve = chunk_empty;
 	u_int key_size = 0;
+	int nid = NID_undef;

 	while (TRUE)
 	{
@@ -347,6 +349,9 @@ openssl_ec_private_key_t *openssl_ec_private_key_gen(key_type_t type,
 			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:
@@ -354,45 +359,46 @@ openssl_ec_private_key_t *openssl_ec_private_key_gen(key_type_t type,
 		}
 		break;
 	}
-	if (!key_size)
+
+	if (curve.len)
 	{
-		return NULL;
+		ASN1_OBJECT *obj = d2i_ASN1_OBJECT(NULL, (const u_char**)&curve.ptr,
+										   curve.len);
+		if (obj)
+		{
+			nid = OBJ_obj2nid(obj);
+			ASN1_OBJECT_free(obj);
+		}
 	}
-
-#if OPENSSL_VERSION_NUMBER >= 0x30000000L
-	switch (key_size)
+	else if (key_size)
 	{
-		case 256:
-			key = EVP_EC_gen("P-256");
-			break;
-		case 384:
-			key = EVP_EC_gen("P-384");
-			break;
-		case 521:
-			key = EVP_EC_gen("P-521");
-			break;
-		default:
-			DBG1(DBG_LIB, "EC private key size %d not supported", key_size);
-			return NULL;
+		switch (key_size)
+		{
+			case 256:
+				nid = NID_X9_62_prime256v1;
+				break;
+			case 384:
+				nid = NID_secp384r1;
+				break;
+			case 521:
+				nid = NID_secp521r1;
+				break;
+			default:
+				DBG1(DBG_LIB, "EC private key size %d not supported", key_size);
+				break;
+		}
 	}
-#else /* OPENSSL_VERSION_NUMBER */
-	EC_KEY *ec;

-	switch (key_size)
+	if (nid == NID_undef)
 	{
-		case 256:
-			ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
-			break;
-		case 384:
-			ec = EC_KEY_new_by_curve_name(NID_secp384r1);
-			break;
-		case 521:
-			ec = EC_KEY_new_by_curve_name(NID_secp521r1);
-			break;
-		default:
-			DBG1(DBG_LIB, "EC private key size %d not supported", key_size);
-			return NULL;
+		return NULL;
 	}
+
+#if OPENSSL_VERSION_NUMBER >= 0x30000000L
+	key = EVP_EC_gen(OBJ_nid2sn(nid));
+#else /* OPENSSL_VERSION_NUMBER */
+	EC_KEY *ec = EC_KEY_new_by_curve_name(nid);
+
 	if (ec && EC_KEY_generate_key(ec) == 1)
 	{
 		key = EVP_PKEY_new();
diff --git a/src/libstrongswan/plugins/openssl/openssl_ec_private_key.h b/src/libstrongswan/plugins/openssl/openssl_ec_private_key.h
index feab32d8d1..6224db6504 100644
--- a/src/libstrongswan/plugins/openssl/openssl_ec_private_key.h
+++ b/src/libstrongswan/plugins/openssl/openssl_ec_private_key.h
@@ -43,7 +43,7 @@ struct openssl_ec_private_key_t {
 /**
  * Generate a ECDSA private key using OpenSSL.
  *
- * 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