Commit 9d8757399a for strongswan.org
commit 9d8757399a8e802524fc29da4ad5e1474d65a80f
Author: Tobias Brunner <tobias@strongswan.org>
Date: Wed Nov 18 10:13:16 2026 +0100
public-key: Add helper to extract curve OID from subjectPublicKeyInfo
diff --git a/src/libstrongswan/credentials/keys/public_key.c b/src/libstrongswan/credentials/keys/public_key.c
index 3852b2882d..4f8d0e405d 100644
--- a/src/libstrongswan/credentials/keys/public_key.c
+++ b/src/libstrongswan/credentials/keys/public_key.c
@@ -445,6 +445,27 @@ enumerator_t *signature_schemes_for_key(key_type_t type, int size)
return &this->public;
}
+/*
+ * Described in header
+ */
+chunk_t ecdsa_curve_from_spki(chunk_t spki)
+{
+ chunk_t inner, params;
+
+ if (asn1_unwrap(&spki, &spki) == ASN1_SEQUENCE &&
+ asn1_unwrap(&spki, &spki) == ASN1_SEQUENCE &&
+ asn1_unwrap(&spki, &inner) == ASN1_OID &&
+ asn1_known_oid(inner) == OID_EC_PUBLICKEY)
+ {
+ params = spki;
+ if (asn1_unwrap(&spki, &inner) == ASN1_OID)
+ {
+ return chunk_clone(params);
+ }
+ }
+ return chunk_empty;
+}
+
/*
* Defined in header.
*/
diff --git a/src/libstrongswan/credentials/keys/public_key.h b/src/libstrongswan/credentials/keys/public_key.h
index 5e066f5580..09ecaac20a 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);
+/**
+ * Extract a DER-encoded curve OID from a subjectPublicKeyInfo structure.
+ *
+ * @param spki subjectPublicKeyInfo structure
+ * @return DER-encoded OID (allocated), chunk_empty on error
+ */
+chunk_t ecdsa_curve_from_spki(chunk_t spki);
+
/**
* Conversion of signature scheme to ASN.1 signature OID.
*
diff --git a/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.c b/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.c
index b578d11ef8..11fffe8684 100644
--- a/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.c
+++ b/src/libstrongswan/plugins/pkcs1/pkcs1_encoder.c
@@ -86,23 +86,14 @@ static bool build_pub_modulus(chunk_t *encoding, va_list args)
*/
static bool build_pub_curve(chunk_t *encoding, va_list args)
{
- chunk_t outer, inner, curve;
+ chunk_t spki;
- if (cred_encoding_args(args, CRED_PART_ECDSA_PUB_ASN1_DER, &outer,
+ if (cred_encoding_args(args, CRED_PART_ECDSA_PUB_ASN1_DER, &spki,
CRED_PART_END) ||
- cred_encoding_args(args, CRED_PART_PUB_ASN1_DER, &outer, CRED_PART_END))
+ cred_encoding_args(args, CRED_PART_PUB_ASN1_DER, &spki, CRED_PART_END))
{
- if (asn1_unwrap(&outer, &outer) == ASN1_SEQUENCE &&
- asn1_unwrap(&outer, &outer) == ASN1_SEQUENCE &&
- asn1_unwrap(&outer, &inner) == ASN1_OID)
- {
- curve = outer;
- if (asn1_unwrap(&outer, &inner) == ASN1_OID)
- {
- *encoding = chunk_clone(curve);
- return TRUE;
- }
- }
+ *encoding = ecdsa_curve_from_spki(spki);
+ return encoding->len != 0;
}
return FALSE;
}