Commit c9cbf86093 for strongswan.org

commit c9cbf86093cc2e3354c017a5dcd8fd6b395d5d7f
Author: Tobias Brunner <tobias@strongswan.org>
Date:   Tue Nov 17 16:54:26 2026 +0100

    vici: Determine builder part for a certificate/key blob

    This allows loading DER-encoded certificates and keys if the pem plugin
    is not loaded, which is the only one that handles BUILD_BLOB(_PEM).

    Empty X.509 flags must not be forwarded as e.g. the CRL loaded in the
    x509 plugin doesn't support them (the pem plugin filters them as well).

diff --git a/src/libcharon/plugins/vici/vici_authority.c b/src/libcharon/plugins/vici/vici_authority.c
index a766ed700d..f3c9796665 100644
--- a/src/libcharon/plugins/vici/vici_authority.c
+++ b/src/libcharon/plugins/vici/vici_authority.c
@@ -288,7 +288,7 @@ CALLBACK(parse_cacert, bool,
 	x509_t *x509;

 	cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
-										  BUILD_BLOB_PEM, v, BUILD_END);
+							  builder_part_for_blob(v), v, BUILD_END);
 	if (!cert)
 	{
 		return FALSE;
diff --git a/src/libcharon/plugins/vici/vici_config.c b/src/libcharon/plugins/vici/vici_config.c
index f3bf193d21..41b7c466f3 100644
--- a/src/libcharon/plugins/vici/vici_config.c
+++ b/src/libcharon/plugins/vici/vici_config.c
@@ -1353,7 +1353,7 @@ static bool parse_cert(auth_data_t *auth, auth_rule_t rule, chunk_t v)
 	else
 	{
 		cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
-								  BUILD_BLOB, v, BUILD_END);
+								  builder_part_for_blob(v), v, BUILD_END);
 	}
 	if (cert)
 	{
@@ -1406,7 +1406,8 @@ static bool parse_and_add_pubkeys(auth_data_t *auth, identification_t *id)
 	while (array_remove(auth->pubkeys, ARRAY_HEAD, &pubkey))
 	{
 		cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
-								  CERT_TRUSTED_PUBKEY, BUILD_BLOB, pubkey,
+								  CERT_TRUSTED_PUBKEY,
+								  builder_part_for_blob(pubkey), pubkey,
 								  id_usable ? BUILD_SUBJECT : BUILD_END,
 								  id, BUILD_END);
 		if (!cert || !add_cert(auth, AUTH_RULE_SUBJECT_CERT, cert))
diff --git a/src/libcharon/plugins/vici/vici_cred.c b/src/libcharon/plugins/vici/vici_cred.c
index b9d1e2bcef..af1ce9cc57 100644
--- a/src/libcharon/plugins/vici/vici_cred.c
+++ b/src/libcharon/plugins/vici/vici_cred.c
@@ -119,6 +119,7 @@ CALLBACK(load_cert, vici_message_t*,
 	certificate_t *cert;
 	certificate_type_t type;
 	x509_flag_t ext_flag, flag = X509_NONE;
+	builder_part_t part;
 	x509_t *x509;
 	chunk_t data;
 	char *str;
@@ -150,20 +151,27 @@ CALLBACK(load_cert, vici_message_t*,
 		return vici_create_reply("certificate data missing");
 	}

+	part = builder_part_for_blob(data);
+	if (type == CERT_ANY && part == BUILD_BLOB_ASN1_DER)
+	{	/* CERT_ANY is really only supported for PEM-encoded blobs (X.509 vs.
+		 * PGP), so we default to X.509 for DER-encoded blobs */
+		type = CERT_X509;
+	}
+
 	/* do not set CA flag externally */
 	ext_flag = (flag & X509_CA) ? X509_NONE : flag;

-	cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
-							  BUILD_BLOB, data,
-							  BUILD_X509_FLAG, ext_flag,
-							  BUILD_END);
+	cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, type, part, data,
+							  ext_flag ? BUILD_X509_FLAG : BUILD_END,
+							  ext_flag, BUILD_END);
 	if (!cert)
 	{
 		return vici_create_reply("parsing %N certificate failed",
-							certificate_type_names, type);
+								 certificate_type_names, type);
 	}
 	DBG1(DBG_CFG, "loaded certificate '%Y'", cert->get_subject(cert));

+	type = cert->get_type(cert);
 	if (type == CERT_X509)
 	{
 		x509 = (x509_t*)cert;
@@ -217,7 +225,7 @@ CALLBACK(load_key, vici_message_t*,
 		return vici_create_reply("key data missing");
 	}
 	key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
-							 BUILD_BLOB_PEM, data, BUILD_END);
+							 builder_part_for_blob(data), data, BUILD_END);
 	if (!key)
 	{
 		return vici_create_reply("parsing %N private key failed",
diff --git a/src/libstrongswan/credentials/builder.c b/src/libstrongswan/credentials/builder.c
index 9b28d7af1e..f41eb81b8b 100644
--- a/src/libstrongswan/credentials/builder.c
+++ b/src/libstrongswan/credentials/builder.c
@@ -17,6 +17,8 @@

 #include "builder.h"

+#include <asn1/asn1.h>
+
 ENUM(builder_part_names, BUILD_FROM_FILE, BUILD_END,
 	"BUILD_FROM_FILE",
 	"BUILD_AGENT_SOCKET",
@@ -84,3 +86,11 @@ ENUM(builder_part_names, BUILD_FROM_FILE, BUILD_END,
 	"BUILD_OCSP_RESPONSES",
 	"BUILD_END",
 );
+
+/*
+ * Described in header
+ */
+builder_part_t builder_part_for_blob(chunk_t blob)
+{
+	return is_asn1(blob) ? BUILD_BLOB_ASN1_DER : BUILD_BLOB;
+}
diff --git a/src/libstrongswan/credentials/builder.h b/src/libstrongswan/credentials/builder.h
index 41ed35e3b8..436999ee64 100644
--- a/src/libstrongswan/credentials/builder.h
+++ b/src/libstrongswan/credentials/builder.h
@@ -182,4 +182,13 @@ enum builder_part_t {
  */
 extern enum_name_t *builder_part_names;

+/**
+ * Determine the builder part for a blob to load a certificate or key based
+ * on its guessed encoding.
+ *
+ * @param blob		blob to check
+ * @return			builder part (BUILD_BLOB_ASN1_DER or BUILD_BLOB)
+ */
+builder_part_t builder_part_for_blob(chunk_t blob);
+
 #endif /** BUILDER_H_ @}*/