Commit c576b7aa4e for strongswan.org
commit c576b7aa4e5eb311b1318b92b3ad765e3e9366c0
Author: Andreas Steffen <andreas.steffen@strongswan.org>
Date: Fri Jul 11 21:02:58 2025 +0200
openssl: Add ML-DSA support
diff --git a/src/libstrongswan/plugins/openssl/Makefile.am b/src/libstrongswan/plugins/openssl/Makefile.am
index 636851a8ea..30edffa000 100644
--- a/src/libstrongswan/plugins/openssl/Makefile.am
+++ b/src/libstrongswan/plugins/openssl/Makefile.am
@@ -39,6 +39,8 @@ libstrongswan_openssl_la_SOURCES = \
openssl_x_diffie_hellman.c openssl_x_diffie_hellman.h \
openssl_ed_private_key.c openssl_ed_private_key.h \
openssl_ed_public_key.c openssl_ed_public_key.h \
+ openssl_ml_dsa_private_key.c openssl_ml_dsa_private_key.h \
+ openssl_ml_dsa_public_key.c openssl_ml_dsa_public_key.h \
openssl_xof.c openssl_xof.h \
openssl_kem.c openssl_kem.h
diff --git a/src/libstrongswan/plugins/openssl/openssl_ed_private_key.c b/src/libstrongswan/plugins/openssl/openssl_ed_private_key.c
index e676fe8977..b6974ca1a4 100644
--- a/src/libstrongswan/plugins/openssl/openssl_ed_private_key.c
+++ b/src/libstrongswan/plugins/openssl/openssl_ed_private_key.c
@@ -68,7 +68,6 @@ int asn1_unwrap(chunk_t*, chunk_t*);
/* from ed public key */
int openssl_ed_key_type(key_type_t type);
int openssl_ed_keysize(key_type_t type);
-bool openssl_ed_fingerprint(EVP_PKEY *key, cred_encoding_type_t type, chunk_t *fp);
METHOD(private_key_t, sign, bool,
private_private_key_t *this, signature_scheme_t scheme,
@@ -156,7 +155,7 @@ METHOD(private_key_t, get_fingerprint, bool,
private_private_key_t *this, cred_encoding_type_t type,
chunk_t *fingerprint)
{
- return openssl_ed_fingerprint(this->key, type, fingerprint);
+ return openssl_fingerprint(this->key, type, fingerprint);
}
METHOD(private_key_t, get_encoding, bool,
diff --git a/src/libstrongswan/plugins/openssl/openssl_ed_public_key.c b/src/libstrongswan/plugins/openssl/openssl_ed_public_key.c
index d6ea2be312..0467c3d79a 100644
--- a/src/libstrongswan/plugins/openssl/openssl_ed_public_key.c
+++ b/src/libstrongswan/plugins/openssl/openssl_ed_public_key.c
@@ -133,56 +133,10 @@ METHOD(public_key_t, get_keysize, int,
return openssl_ed_keysize(this->type);
}
-/**
- * Calculate fingerprint from an EdDSA key, also used in ed private key.
- */
-bool openssl_ed_fingerprint(EVP_PKEY *key, cred_encoding_type_t type,
- chunk_t *fp)
-{
- hasher_t *hasher;
- chunk_t blob;
-
- if (lib->encoding->get_cache(lib->encoding, type, key, fp))
- {
- return TRUE;
- }
- switch (type)
- {
- case KEYID_PUBKEY_SHA1:
- if (!EVP_PKEY_get_raw_public_key(key, NULL, &blob.len))
- {
- return FALSE;
- }
- blob = chunk_alloc(blob.len);
- if (!EVP_PKEY_get_raw_public_key(key, blob.ptr, &blob.len))
- {
- return FALSE;
- }
- break;
- case KEYID_PUBKEY_INFO_SHA1:
- blob = openssl_i2chunk(PUBKEY, key);
- break;
- default:
- return FALSE;
- }
- hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
- if (!hasher || !hasher->allocate_hash(hasher, blob, fp))
- {
- DBG1(DBG_LIB, "SHA1 not supported, fingerprinting failed");
- DESTROY_IF(hasher);
- chunk_free(&blob);
- return FALSE;
- }
- hasher->destroy(hasher);
- chunk_free(&blob);
- lib->encoding->cache(lib->encoding, type, key, fp);
- return TRUE;
-}
-
METHOD(public_key_t, get_fingerprint, bool,
private_public_key_t *this, cred_encoding_type_t type, chunk_t *fingerprint)
{
- return openssl_ed_fingerprint(this->key, type, fingerprint);
+ return openssl_fingerprint(this->key, type, fingerprint);
}
METHOD(public_key_t, get_encoding, bool,
diff --git a/src/libstrongswan/plugins/openssl/openssl_ml_dsa_private_key.c b/src/libstrongswan/plugins/openssl/openssl_ml_dsa_private_key.c
new file mode 100644
index 0000000000..12e0d2c248
--- /dev/null
+++ b/src/libstrongswan/plugins/openssl/openssl_ml_dsa_private_key.c
@@ -0,0 +1,383 @@
+/*
+ * Copyright (C) 2025 Andreas Steffen
+ *
+ * Copyright (C) secunet Security Networks AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include <openssl/evp.h>
+
+#if OPENSSL_VERSION_NUMBER >= 0x30500000L && !defined(OPENSSL_NO_ML_DSA)
+#include <openssl/core_names.h>
+#include <openssl/params.h>
+#include <openssl/provider.h>
+
+#include "openssl_ml_dsa_private_key.h"
+#include "openssl_util.h"
+
+#include <utils/debug.h>
+
+typedef struct private_private_key_t private_private_key_t;
+
+#define ML_DSA_SEED_LEN 32
+
+/**
+ * Private data
+ */
+struct private_private_key_t {
+
+ /**
+ * Public interface
+ */
+ private_key_t public;
+
+ /**
+ * Key object
+ */
+ EVP_PKEY *key;
+
+ /**
+ * Key type
+ */
+ key_type_t type;
+
+ /**
+ * reference count
+ */
+ refcount_t ref;
+};
+
+/* from openssl_ml_dsa public key */
+char *openssl_ml_dsa_alg_name(key_type_t type);
+key_type_t openssl_ml_dsa_evp_pkey_key_type(EVP_PKEY *key);
+
+METHOD(private_key_t, sign, bool,
+ private_private_key_t *this, signature_scheme_t scheme,
+ void *params, chunk_t data, chunk_t *signature)
+{
+ pqc_params_t pqc_params;
+ OSSL_PARAM ossl_params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END};
+ EVP_PKEY_CTX *ctx = NULL;
+ EVP_SIGNATURE *sig_alg = NULL;
+ int deterministic;
+ bool success = FALSE;
+
+ if ((this->type == KEY_ML_DSA_44 && scheme != SIGN_ML_DSA_44) ||
+ (this->type == KEY_ML_DSA_65 && scheme != SIGN_ML_DSA_65) ||
+ (this->type == KEY_ML_DSA_87 && scheme != SIGN_ML_DSA_87))
+ {
+ DBG1(DBG_LIB, "signature scheme %N not supported by %N key",
+ signature_scheme_names, scheme, key_type_names, this->type);
+ return FALSE;
+ }
+
+ /* set PQC signature params */
+ if (!pqc_params_create(params, &pqc_params))
+ {
+ return FALSE;
+ }
+ deterministic = pqc_params.deterministic ? 1 : 0;
+ ossl_params[0] = OSSL_PARAM_construct_int(
+ OSSL_SIGNATURE_PARAM_DETERMINISTIC,
+ &deterministic);
+ if (pqc_params.ctx.len)
+ {
+ ossl_params[1] = OSSL_PARAM_construct_octet_string(
+ OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
+ pqc_params.ctx.ptr, pqc_params.ctx.len);
+ }
+
+ ctx = EVP_PKEY_CTX_new_from_pkey(NULL, this->key, NULL);
+ if (!ctx)
+ {
+ goto error;
+ }
+ sig_alg = EVP_SIGNATURE_fetch(NULL, openssl_ml_dsa_alg_name(this->type), NULL);
+
+ if (EVP_PKEY_sign_message_init(ctx, sig_alg, ossl_params) <= 0)
+ {
+ goto error;
+ }
+
+ if (EVP_PKEY_sign(ctx, NULL, &signature->len, data.ptr, data.len) <= 0)
+ {
+ goto error;
+ }
+
+ *signature = chunk_alloc(signature->len);
+
+ if (EVP_PKEY_sign(ctx, signature->ptr, &signature->len,
+ data.ptr, data.len) <= 0)
+ {
+ chunk_free(signature);
+ goto error;
+ }
+ success = TRUE;
+
+error:
+ pqc_params_free(&pqc_params);
+ EVP_SIGNATURE_free(sig_alg);
+ EVP_PKEY_CTX_free(ctx);
+
+ return success;
+}
+
+METHOD(private_key_t, decrypt, bool,
+ private_private_key_t *this, encryption_scheme_t scheme,
+ void *params, chunk_t crypto, chunk_t *plain)
+{
+ DBG1(DBG_LIB, "ML-DSA private key decryption not implemented");
+ return FALSE;
+}
+
+METHOD(private_key_t, get_keysize, int,
+ private_private_key_t *this)
+{
+ return BITS_PER_BYTE * get_public_key_size(this->type);
+}
+
+METHOD(private_key_t, get_type, key_type_t,
+ private_private_key_t *this)
+{
+ return this->type;
+}
+
+METHOD(private_key_t, get_public_key, public_key_t*,
+ private_private_key_t *this)
+{
+ public_key_t *public;
+ u_char buf[2592];
+ chunk_t key = {buf, sizeof(buf)};
+
+ if (!EVP_PKEY_get_octet_string_param(this->key, OSSL_PKEY_PARAM_PUB_KEY,
+ buf, sizeof(buf), &key.len))
+ {
+ return NULL;
+ }
+ public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, this->type,
+ BUILD_BLOB, key, BUILD_END);
+ return public;
+}
+
+METHOD(private_key_t, get_fingerprint, bool,
+ private_private_key_t *this, cred_encoding_type_t type,
+ chunk_t *fingerprint)
+{
+ return openssl_fingerprint(this->key, type, fingerprint);
+}
+
+METHOD(private_key_t, get_encoding, bool,
+ private_private_key_t *this, cred_encoding_type_t type, chunk_t *encoding)
+{
+ switch (type)
+ {
+ case PRIVKEY_ASN1_DER:
+ case PRIVKEY_PEM:
+ {
+ bool success = TRUE;
+
+ *encoding = openssl_i2chunk(PrivateKey, this->key);
+
+ if (type == PRIVKEY_PEM)
+ {
+ chunk_t asn1_encoding = *encoding;
+
+ success = lib->encoding->encode(lib->encoding, PRIVKEY_PEM,
+ NULL, encoding, CRED_PART_PRIV_ASN1_DER,
+ asn1_encoding, CRED_PART_END);
+ chunk_clear(&asn1_encoding);
+ }
+
+ return success;
+ }
+ default:
+ return FALSE;
+ }
+}
+
+METHOD(private_key_t, get_ref, private_key_t*,
+ private_private_key_t *this)
+{
+ ref_get(&this->ref);
+ return &this->public;
+}
+
+METHOD(private_key_t, destroy, void,
+ private_private_key_t *this)
+{
+ if (ref_put(&this->ref))
+ {
+ lib->encoding->clear_cache(lib->encoding, this->key);
+ EVP_PKEY_free(this->key);
+ free(this);
+ }
+}
+
+/**
+ * Internal generic constructor
+ */
+static private_key_t *create_internal(key_type_t type, EVP_PKEY *key)
+{
+ private_private_key_t *this;
+
+ INIT(this,
+ .public = {
+ .get_type = _get_type,
+ .sign = _sign,
+ .decrypt = _decrypt,
+ .get_keysize = _get_keysize,
+ .get_public_key = _get_public_key,
+ .equals = private_key_equals,
+ .belongs_to = private_key_belongs_to,
+ .get_fingerprint = _get_fingerprint,
+ .has_fingerprint = private_key_has_fingerprint,
+ .get_encoding = _get_encoding,
+ .get_ref = _get_ref,
+ .destroy = _destroy,
+ },
+ .type = type,
+ .key = key,
+ .ref = 1,
+ );
+
+ return &this->public;
+}
+
+/*
+ * Described in header
+ */
+private_key_t *openssl_ml_dsa_private_key_create(EVP_PKEY *key, bool engine)
+{
+ key_type_t type;
+
+ type = openssl_ml_dsa_evp_pkey_key_type(key);
+ if (type == KEY_ANY)
+ {
+ EVP_PKEY_free(key);
+ return NULL;
+ }
+ return create_internal(type, key);
+}
+
+/**
+ * Create an ML-DSA private_key_t instance from a seed
+ */
+static private_key_t *create_instance(key_type_t type, chunk_t keyseed)
+{
+ EVP_PKEY_CTX *ctx = NULL;
+ EVP_PKEY *key = NULL;
+
+ ctx = EVP_PKEY_CTX_new_from_name(NULL, openssl_ml_dsa_alg_name(type), NULL);
+ if (!ctx || EVP_PKEY_keygen_init(ctx) <= 0)
+ {
+ DBG1(DBG_LIB, "failed to create ctx");
+ goto end;
+ }
+
+ if (keyseed.ptr)
+ {
+ OSSL_PARAM params[] = {
+ OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED,
+ keyseed.ptr, keyseed.len),
+ OSSL_PARAM_END
+ };
+
+ if (!EVP_PKEY_CTX_set_params(ctx, params))
+ {
+ DBG1(DBG_LIB, "failed to set keyseed");
+ goto end;
+ }
+ }
+
+ if (EVP_PKEY_generate(ctx, &key) <= 0)
+ {
+ DBG1(DBG_LIB, "failed to generate ML-DSA private key");
+ goto end;
+ }
+
+end:
+ EVP_PKEY_CTX_free(ctx);
+ return key ? create_internal(type, key) : NULL;
+}
+
+/*
+ * Described in header
+ */
+private_key_t *openssl_ml_dsa_private_key_gen(key_type_t type, va_list args)
+{
+ if (type != KEY_ML_DSA_44 && type != KEY_ML_DSA_65 && type != KEY_ML_DSA_87)
+ {
+ return NULL;
+ }
+ while (TRUE)
+ {
+ switch (va_arg(args, builder_part_t))
+ {
+ case BUILD_KEY_SIZE:
+ /* just ignore the key size */
+ va_arg(args, u_int);
+ continue;
+ case BUILD_END:
+ break;
+ default:
+ return NULL;
+ }
+ break;
+ }
+
+ return create_instance(type, chunk_empty);
+}
+
+/*
+ * Described in header
+ */
+private_key_t *openssl_ml_dsa_private_key_load(key_type_t type, va_list args)
+{
+ chunk_t priv = chunk_empty;
+
+ if (type != KEY_ML_DSA_44 && type != KEY_ML_DSA_65 && type != KEY_ML_DSA_87)
+ {
+ return NULL;
+ }
+
+ while (TRUE)
+ {
+ switch (va_arg(args, builder_part_t))
+ {
+ case BUILD_BLOB:
+ priv = va_arg(args, chunk_t);
+ continue;
+ case BUILD_END:
+ break;
+ default:
+ return NULL;
+ }
+ break;
+ }
+
+ if (priv.len == ML_DSA_SEED_LEN + 2 &&
+ priv.ptr[0] == 0x80 && priv.ptr[1] == ML_DSA_SEED_LEN)
+ {
+ priv = chunk_skip(priv, 2);
+ }
+ if (priv.len != ML_DSA_SEED_LEN)
+ {
+ DBG1(DBG_LIB, "error: the size of the loaded ML-DSA private key seed "
+ "is %zu bytes instead of %u bytes", priv.len, ML_DSA_SEED_LEN);
+ return NULL;
+ }
+
+ return create_instance(type, priv);
+}
+
+
+#endif /* OPENSSL_NO_ML_DSA*/
diff --git a/src/libstrongswan/plugins/openssl/openssl_ml_dsa_private_key.h b/src/libstrongswan/plugins/openssl/openssl_ml_dsa_private_key.h
new file mode 100644
index 0000000000..9a9077d43d
--- /dev/null
+++ b/src/libstrongswan/plugins/openssl/openssl_ml_dsa_private_key.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2025 Andreas Steffen
+ *
+ * Copyright (C) secunet Security Networks AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup openssl_ml_dsa_private_key openssl_ml_dsa_private_key
+ * @{ @ingroup openssl_p
+ */
+
+#ifndef OPENSSL_ML_DSA_PRIVATE_KEY_H_
+#define OPENSSL_ML_DSA_PRIVATE_KEY_H_
+
+#include <openssl/evp.h>
+
+#include <credentials/builder.h>
+#include <credentials/keys/private_key.h>
+
+/**
+ * Generate an ML-DSA private key using OpenSSL.
+ *
+ * @param type type of the key, must be one of KEY_ML_DSA_44/65/87
+ * @param args builder_part_t argument list
+ * @return generated key, NULL on failure
+ */
+private_key_t *openssl_ml_dsa_private_key_gen(key_type_t type, va_list args);
+
+/**
+ * Load an ML-DSA private key using OpenSSL.
+ *
+ * Accepts a BUILD_BLOB argument.
+ *
+ * @param type type of the key, must be one of KEY_ML_DSA_44/65/87
+ * @param args builder_part_t argument list
+ * @return loaded key, NULL on failure
+ */
+private_key_t *openssl_ml_dsa_private_key_load(key_type_t type, va_list args);
+
+/**
+ * Wrap an EVP_PKEY object of type ML-DSA-44/65/87.
+ *
+ * @param key EVP_PKEY object (adopted)
+ * @param engine whether the key was loaded via an engine
+ * @return generated key, NULL on failure
+ */
+private_key_t *openssl_ml_dsa_private_key_create(EVP_PKEY *key, bool engine);
+
+#endif /** OPENSSL_ML_DSA_PRIVATE_KEY_H_ @}*/
diff --git a/src/libstrongswan/plugins/openssl/openssl_ml_dsa_public_key.c b/src/libstrongswan/plugins/openssl/openssl_ml_dsa_public_key.c
new file mode 100644
index 0000000000..6f736aa0dc
--- /dev/null
+++ b/src/libstrongswan/plugins/openssl/openssl_ml_dsa_public_key.c
@@ -0,0 +1,313 @@
+/*
+ * Copyright (C) 2025 Andreas Steffen
+ *
+ * Copyright (C) secunet Security Networks AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include <openssl/evp.h>
+
+#if OPENSSL_VERSION_NUMBER >= 0x30500000L && !defined(OPENSSL_NO_ML_DSA)
+#include <openssl/x509.h>
+#include <openssl/core_names.h>
+#include <openssl/params.h>
+
+#include "openssl_ml_dsa_public_key.h"
+#include "openssl_util.h"
+
+#include <utils/debug.h>
+
+typedef struct private_public_key_t private_public_key_t;
+
+/**
+ * Private data
+ */
+struct private_public_key_t {
+
+ /**
+ * Public interface
+ */
+ public_key_t public;
+
+ /**
+ * Key object
+ */
+ EVP_PKEY *key;
+
+ /**
+ * Key type
+ */
+ key_type_t type;
+
+ /**
+ * Reference counter
+ */
+ refcount_t ref;
+};
+
+/**
+ * Map a key type to an algorithm name (also used by private key)
+ */
+char *openssl_ml_dsa_alg_name(key_type_t type)
+{
+ switch (type)
+ {
+ case KEY_ML_DSA_44:
+ return "ML-DSA-44";
+ case KEY_ML_DSA_65:
+ return "ML-DSA-65";
+ case KEY_ML_DSA_87:
+ return "ML-DSA-87";
+ default:
+ return NULL;
+ }
+}
+
+/**
+ * Determine the ML-DSA key type for the given key (also used by private key)
+ */
+key_type_t openssl_ml_dsa_evp_pkey_key_type(EVP_PKEY *key)
+{
+ if (EVP_PKEY_is_a(key, "ML-DSA-44"))
+ {
+ return KEY_ML_DSA_44;
+ }
+ else if (EVP_PKEY_is_a(key, "ML-DSA-65"))
+ {
+ return KEY_ML_DSA_65;
+ }
+ else if (EVP_PKEY_is_a(key, "ML-DSA-87"))
+ {
+ return KEY_ML_DSA_87;
+ }
+ return KEY_ANY;
+}
+
+/**
+ * Map a key type to an EVP key type
+ */
+static int openssl_ml_dsa_key_type(key_type_t type)
+{
+ switch (type)
+ {
+ case KEY_ML_DSA_44:
+ return EVP_PKEY_ML_DSA_44;
+ case KEY_ML_DSA_65:
+ return EVP_PKEY_ML_DSA_65;
+ case KEY_ML_DSA_87:
+ return EVP_PKEY_ML_DSA_87;
+ default:
+ return 0;
+ }
+}
+
+METHOD(public_key_t, get_type, key_type_t,
+ private_public_key_t *this)
+{
+ return this->type;
+}
+
+METHOD(public_key_t, verify, bool,
+ private_public_key_t *this, signature_scheme_t scheme,
+ void *params, chunk_t data, chunk_t signature)
+{
+ pqc_params_t pqc_params;
+ EVP_PKEY_CTX *ctx = NULL;
+ EVP_SIGNATURE *sig_alg = NULL;
+ OSSL_PARAM ossl_params[] = { OSSL_PARAM_END, OSSL_PARAM_END};
+ bool success = FALSE;
+
+ if ((this->type == KEY_ML_DSA_44 && scheme != SIGN_ML_DSA_44) ||
+ (this->type == KEY_ML_DSA_65 && scheme != SIGN_ML_DSA_65) ||
+ (this->type == KEY_ML_DSA_87 && scheme != SIGN_ML_DSA_87))
+ {
+ DBG1(DBG_LIB, "signature scheme %N not supported by %N key",
+ signature_scheme_names, scheme, key_type_names, this->type);
+ return FALSE;
+ }
+
+ /* set PQC signature params */
+ if (!pqc_params_create(params, &pqc_params))
+ {
+ return FALSE;
+ }
+ if (pqc_params.ctx.len)
+ {
+ ossl_params[0] = OSSL_PARAM_construct_octet_string(
+ OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
+ pqc_params.ctx.ptr, pqc_params.ctx.len);
+ }
+
+ ctx = EVP_PKEY_CTX_new_from_pkey(NULL, this->key, NULL);
+ if (!ctx)
+ {
+ goto error;
+ }
+ sig_alg = EVP_SIGNATURE_fetch(NULL, openssl_ml_dsa_alg_name(this->type), NULL);
+
+ if (EVP_PKEY_verify_message_init(ctx, sig_alg, ossl_params) <= 0)
+ {
+ goto error;
+ }
+
+ if (EVP_PKEY_verify(ctx, signature.ptr, signature.len, data.ptr, data.len) <= 0)
+ {
+ goto error;
+ }
+ success = TRUE;
+
+error:
+ pqc_params_free(&pqc_params);
+ EVP_SIGNATURE_free(sig_alg);
+ EVP_PKEY_CTX_free(ctx);
+
+ return success;
+}
+
+METHOD(public_key_t, encrypt, bool,
+ private_public_key_t *this, encryption_scheme_t scheme,
+ void *params, chunk_t crypto, chunk_t *plain)
+{
+ DBG1(DBG_LIB, "encryption scheme %N not supported", encryption_scheme_names,
+ scheme);
+ return FALSE;
+}
+
+METHOD(public_key_t, get_keysize, int,
+ private_public_key_t *this)
+{
+ return BITS_PER_BYTE * get_public_key_size(this->type);
+}
+
+METHOD(public_key_t, get_fingerprint, bool,
+ private_public_key_t *this, cred_encoding_type_t type, chunk_t *fingerprint)
+{
+ return openssl_fingerprint(this->key, type, fingerprint);
+}
+
+METHOD(public_key_t, get_encoding, bool,
+ private_public_key_t *this, cred_encoding_type_t type, chunk_t *encoding)
+{
+ bool success = TRUE;
+
+ *encoding = openssl_i2chunk(PUBKEY, this->key);
+
+ if (type != PUBKEY_SPKI_ASN1_DER)
+ {
+ chunk_t asn1_encoding = *encoding;
+
+ success = lib->encoding->encode(lib->encoding, type,
+ NULL, encoding, CRED_PART_PUB_ASN1_DER,
+ asn1_encoding, CRED_PART_END);
+ chunk_clear(&asn1_encoding);
+ }
+ return success;
+}
+
+METHOD(public_key_t, get_ref, public_key_t*,
+ private_public_key_t *this)
+{
+ ref_get(&this->ref);
+ return &this->public;
+}
+
+METHOD(public_key_t, destroy, void,
+ private_public_key_t *this)
+{
+ if (ref_put(&this->ref))
+ {
+ lib->encoding->clear_cache(lib->encoding, this->key);
+ EVP_PKEY_free(this->key);
+ free(this);
+ }
+}
+
+/**
+ * Generic private constructor
+ */
+static private_public_key_t *create_empty(key_type_t type)
+{
+ private_public_key_t *this;
+
+ INIT(this,
+ .public = {
+ .get_type = _get_type,
+ .verify = _verify,
+ .encrypt = _encrypt,
+ .get_keysize = _get_keysize,
+ .equals = public_key_equals,
+ .get_fingerprint = _get_fingerprint,
+ .has_fingerprint = public_key_has_fingerprint,
+ .get_encoding = _get_encoding,
+ .get_ref = _get_ref,
+ .destroy = _destroy,
+ },
+ .type = type,
+ .ref = 1,
+ );
+
+ return this;
+}
+
+/*
+ * Described in header
+ */
+public_key_t *openssl_ml_dsa_public_key_load(key_type_t type, va_list args)
+{
+ private_public_key_t *this;
+ chunk_t pkcs1 = chunk_empty, blob = chunk_empty;
+ EVP_PKEY *key = NULL;
+
+ while (TRUE)
+ {
+ switch (va_arg(args, builder_part_t))
+ {
+ case BUILD_BLOB:
+ blob = va_arg(args, chunk_t);
+ continue;
+ case BUILD_BLOB_ASN1_DER:
+ pkcs1 = va_arg(args, chunk_t);
+ continue;
+ case BUILD_END:
+ break;
+ default:
+ return NULL;
+ }
+ break;
+ }
+
+ if (blob.len)
+ {
+ key = EVP_PKEY_new_raw_public_key(openssl_ml_dsa_key_type(type), NULL,
+ blob.ptr, blob.len);
+ }
+ else if (pkcs1.len)
+ {
+ key = d2i_PUBKEY(NULL, (const u_char**)&pkcs1.ptr, pkcs1.len);
+ type = openssl_ml_dsa_evp_pkey_key_type(key);
+ if (key && type == KEY_ANY)
+ {
+ EVP_PKEY_free(key);
+ return NULL;
+ }
+ }
+ if (!key)
+ {
+ return NULL;
+ }
+ this = create_empty(type);
+ this->key = key;
+
+ return &this->public;
+}
+
+#endif /* OPENSSL_VERSION_NUMBER */
diff --git a/src/libstrongswan/plugins/openssl/openssl_ml_dsa_public_key.h b/src/libstrongswan/plugins/openssl/openssl_ml_dsa_public_key.h
new file mode 100644
index 0000000000..82185a19a8
--- /dev/null
+++ b/src/libstrongswan/plugins/openssl/openssl_ml_dsa_public_key.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2025 Andreas Steffen
+ *
+ * Copyright (C) secunet Security Networks AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup openssl_ml_dsa_public_key openssl_ml_dsa_public_key
+ * @{ @ingroup openssl_p
+ */
+
+#ifndef OPENSSL_ML_DSA_PUBLIC_KEY_H_
+#define OPENSSL_ML_DSA_PUBLIC_KEY_H_
+
+#include <credentials/builder.h>
+#include <credentials/keys/public_key.h>
+
+/**
+ * Load an ML-DSA public key using OpenSSL.
+ *
+ * Accepts a BUILD_BLOB or BUILD_BLOB_ASN1_DER argument.
+ *
+ * @param type type of the key, must be one of KEY_ML_DSA_44/65/87
+ * @param args builder_part_t argument list
+ * @return loaded key, NULL on failure
+ */
+public_key_t *openssl_ml_dsa_public_key_load(key_type_t type, va_list args);
+
+#endif /** OPENSSL_ML_DSA_PUBLIC_KEY_H_ @}*/
diff --git a/src/libstrongswan/plugins/openssl/openssl_plugin.c b/src/libstrongswan/plugins/openssl/openssl_plugin.c
index 3b3e6a418d..b5ac88d0bb 100644
--- a/src/libstrongswan/plugins/openssl/openssl_plugin.c
+++ b/src/libstrongswan/plugins/openssl/openssl_plugin.c
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2008-2020 Tobias Brunner
* Copyright (C) 2008 Martin Willi
+ * Copyright (C) 2025 Andreas Steffen
*
* Copyright (C) secunet Security Networks AG
*
@@ -31,6 +32,7 @@
#endif
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/provider.h>
+#include <openssl/core_names.h>
#endif
#include "openssl_plugin.h"
@@ -56,6 +58,8 @@
#include "openssl_x_diffie_hellman.h"
#include "openssl_ed_public_key.h"
#include "openssl_ed_private_key.h"
+#include "openssl_ml_dsa_private_key.h"
+#include "openssl_ml_dsa_public_key.h"
#include "openssl_xof.h"
#include "openssl_kem.h"
@@ -286,7 +290,6 @@ static private_key_t *openssl_private_key_load(key_type_t type, va_list args)
}
break;
}
-
if (blob.ptr)
{
key = d2i_AutoPrivateKey(NULL, (const u_char**)&blob.ptr, blob.len);
@@ -669,6 +672,29 @@ METHOD(plugin_t, get_features, int,
PLUGIN_REGISTER(HASHER, return_null),
PLUGIN_PROVIDE(HASHER, HASH_IDENTITY),
#endif /* OPENSSL_VERSION_NUMBER && !OPENSSL_NO_EC && !OPENSSL_IS_AWSLC */
+#if (OPENSSL_VERSION_NUMBER >= 0x30500000L && !defined(OPENSSL_NO_ML_DSA))
+ /* ML-DSA private/public key loading */
+ PLUGIN_REGISTER(PUBKEY, openssl_ml_dsa_public_key_load, TRUE),
+ PLUGIN_PROVIDE(PUBKEY, KEY_ML_DSA_44),
+ PLUGIN_PROVIDE(PUBKEY, KEY_ML_DSA_65),
+ PLUGIN_PROVIDE(PUBKEY, KEY_ML_DSA_87),
+ PLUGIN_PROVIDE(PUBKEY, KEY_ANY),
+ PLUGIN_REGISTER(PRIVKEY, openssl_ml_dsa_private_key_load, TRUE),
+ PLUGIN_PROVIDE(PRIVKEY, KEY_ML_DSA_44),
+ PLUGIN_PROVIDE(PRIVKEY, KEY_ML_DSA_65),
+ PLUGIN_PROVIDE(PRIVKEY, KEY_ML_DSA_87),
+ PLUGIN_REGISTER(PRIVKEY_GEN, openssl_ml_dsa_private_key_gen, FALSE),
+ PLUGIN_PROVIDE(PRIVKEY_GEN, KEY_ML_DSA_44),
+ PLUGIN_PROVIDE(PRIVKEY_GEN, KEY_ML_DSA_65),
+ PLUGIN_PROVIDE(PRIVKEY_GEN, KEY_ML_DSA_87),
+ PLUGIN_PROVIDE(PRIVKEY_SIGN, SIGN_ML_DSA_44),
+ PLUGIN_PROVIDE(PRIVKEY_SIGN, SIGN_ML_DSA_65),
+ PLUGIN_PROVIDE(PRIVKEY_SIGN, SIGN_ML_DSA_87),
+ PLUGIN_PROVIDE(PUBKEY_VERIFY, SIGN_ML_DSA_44),
+ PLUGIN_PROVIDE(PUBKEY_VERIFY, SIGN_ML_DSA_65),
+ PLUGIN_PROVIDE(PUBKEY_VERIFY, SIGN_ML_DSA_87),
+#endif /* OPENSSL_NO_ML_DSA */
+
/* generic key loader */
PLUGIN_REGISTER(PRIVKEY, openssl_private_key_load, TRUE),
PLUGIN_PROVIDE(PRIVKEY, KEY_ANY),
@@ -791,6 +817,30 @@ static int concat_ossl_providers(OSSL_PROVIDER *provider, void *cbdata)
}
#endif
+#if (OPENSSL_VERSION_NUMBER >= 0x30500000L && !defined(OPENSSL_NO_ML_DSA))
+/**
+ * Callback to add ml-dsa parameters to loaded providers
+ */
+static int add_ml_dsa_ossl_params(OSSL_PROVIDER *provider, void *cbdata)
+{
+ /* prefer seed-only format but allow priv-only if we loaded a priv-only
+ * key and need to re-encode it again (e.g. in swanctl after decrypting
+ * a PKCS#8 file) */
+ if (!OSSL_PROVIDER_add_conf_parameter(provider,
+ OSSL_PKEY_PARAM_ML_DSA_RETAIN_SEED, "yes") ||
+ !OSSL_PROVIDER_add_conf_parameter(provider,
+ OSSL_PKEY_PARAM_ML_DSA_PREFER_SEED, "yes") ||
+ !OSSL_PROVIDER_add_conf_parameter(provider,
+ OSSL_PKEY_PARAM_ML_DSA_OUTPUT_FORMATS,
+ "seed-only priv-only"))
+ {
+ DBG1(DBG_LIB, "failed to set ml-dsa parameters in '%s' provider",
+ OSSL_PROVIDER_get0_name(provider));
+ }
+ return 1;
+}
+#endif /* OPENSSL_NO_ML_DSA */
+
/*
* see header file
*/
@@ -878,6 +928,9 @@ PLUGIN_DEFINE(openssl)
OSSL_PROVIDER_do_all(NULL, concat_ossl_providers, &data);
dbg(DBG_LIB, strpfx(lib->ns, "charon") ? 1 : 2,
"providers loaded by OpenSSL:%s", data.names);
+#if (OPENSSL_VERSION_NUMBER >= 0x30500000L && !defined(OPENSSL_NO_ML_DSA))
+ OSSL_PROVIDER_do_all(NULL, add_ml_dsa_ossl_params, NULL);
+#endif /* OPENSSL_NO_ML_DSA */
#endif /* OPENSSL_VERSION_NUMBER */
#ifdef OPENSSL_FIPS
diff --git a/src/libstrongswan/plugins/openssl/openssl_util.c b/src/libstrongswan/plugins/openssl/openssl_util.c
index c09d2a38d9..212a598265 100644
--- a/src/libstrongswan/plugins/openssl/openssl_util.c
+++ b/src/libstrongswan/plugins/openssl/openssl_util.c
@@ -20,6 +20,7 @@
#include <utils/debug.h>
#include <openssl/bn.h>
+#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
@@ -31,6 +32,7 @@
#include "openssl_ec_private_key.h"
#include "openssl_ed_private_key.h"
#include "openssl_rsa_private_key.h"
+#include "openssl_ml_dsa_private_key.h"
/* these were added with 1.1.0 when ASN1_OBJECT was made opaque */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
@@ -118,6 +120,21 @@ bool openssl_fingerprint(EVP_PKEY *key, cred_encoding_type_t type, chunk_t *fp)
switch (type)
{
case KEYID_PUBKEY_SHA1:
+#if OPENSSL_VERSION_NUMBER >= 0x1010100fL
+ /* prefer this as AWS-LC returns a SPKI for keys that don't have a
+ * legacy encoding (e.g. EdDSA) and it's required for ML-DSA */
+ if (EVP_PKEY_get_raw_public_key(key, NULL, &enc.len))
+ {
+ enc = chunk_alloc(enc.len);
+ if (!EVP_PKEY_get_raw_public_key(key, enc.ptr, &enc.len))
+ {
+ chunk_free(&enc);
+ return FALSE;
+ }
+ break;
+ }
+ ERR_clear_error();
+#endif
enc = openssl_i2chunk(PublicKey, key);
break;
case KEYID_PUBKEY_INFO_SHA1:
@@ -126,6 +143,10 @@ bool openssl_fingerprint(EVP_PKEY *key, cred_encoding_type_t type, chunk_t *fp)
default:
return FALSE;
}
+ if (!enc.ptr)
+ {
+ return FALSE;
+ }
hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
if (!hasher || !hasher->allocate_hash(hasher, enc, fp))
{
@@ -165,6 +186,16 @@ private_key_t *openssl_wrap_private_key(EVP_PKEY *key, bool engine)
return openssl_ed_private_key_create(key, engine);
#endif /* OPENSSL_VERSION_NUMBER && !OPENSSL_NO_EC && !OPENSSL_IS_AWSLC */
default:
+#if (OPENSSL_VERSION_NUMBER >= 0x30500000L && !defined(OPENSSL_NO_ML_DSA))
+ /* OpenSSL does not provide an ID for ML-DSA keys, which is
+ * apparently a legacy feature */
+ if (EVP_PKEY_is_a(key, "ML-DSA-44") ||
+ EVP_PKEY_is_a(key, "ML-DSA-65") ||
+ EVP_PKEY_is_a(key, "ML-DSA-87"))
+ {
+ return openssl_ml_dsa_private_key_create(key, FALSE);
+ }
+#endif
EVP_PKEY_free(key);
break;
}