This is an automated email from the ASF dual-hosted git repository.

amarkevich pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/main by this push:
     new 61584d476e1 cxf-rt-security: allow CryptoUtils#loadCertificate without 
alias (#3754)
61584d476e1 is described below

commit 61584d476e195efefab260a5e64480a34f3cfa77
Author: Alexey Markevich <[email protected]>
AuthorDate: Mon Nov 23 15:58:41 2026 +0100

    cxf-rt-security: allow CryptoUtils#loadCertificate without alias (#3754)
    
    Use the sole entry when the keystore contains exactly one.
    Cleanup unused keystores.
---
 etc/rat.unknowns.txt                               |   1 -
 .../apache/cxf/rt/security/crypto/CryptoUtils.java |  15 ++++--
 .../transport/https/resources/abigcompany_ca.pem   |   0
 .../cxf/transport/https/resources/celtix.p12       | Bin 3000 -> 0 bytes
 .../transport/https/resources/celtixp12truststore  | Bin 984 -> 0 bytes
 .../cxf/transport/https/resources/defaultkeystore  | Bin 1392 -> 0 bytes
 .../transport/https/resources/defaulttruststore    | Bin 689 -> 0 bytes
 .../apache/cxf/systest/crypto/CryptoUtilsTest.java |  58 +++++++++++++++++++++
 .../org/apache/cxf/systest/crypto/somealias.p12    | Bin 0 -> 2638 bytes
 9 files changed, 70 insertions(+), 4 deletions(-)

diff --git a/etc/rat.unknowns.txt b/etc/rat.unknowns.txt
index ec655cece46..b1e583ae8fd 100644
--- a/etc/rat.unknowns.txt
+++ b/etc/rat.unknowns.txt
@@ -191,7 +191,6 @@ part of the work around CXF-1000:
  !????? 
./distribution/src/main/release/samples/wsdl_first_https/certs/wibble-ra-cert.pem
  !????? 
./distribution/src/main/release/samples/wsdl_first_https/certs/wibble.chain
  !????? 
./distribution/src/main/release/samples/wsdl_first_https/src/demo/hw_https/resources/abigcompany_ca.pem
 
- !????? 
./rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/abigcompany_ca.pem
  !????? ./rt/ws/security/src/test/java/META-INF/cxf/key.rsa
  !????? 
./rt/ws/security/src/test/java/org/apache/cxf/ws/security/wss4j/signed.xml
  !????? 
./rt/ws/security/src/test/java/org/apache/cxf/ws/security/wss4j/signed_invalid.xml
diff --git 
a/rt/security/src/main/java/org/apache/cxf/rt/security/crypto/CryptoUtils.java 
b/rt/security/src/main/java/org/apache/cxf/rt/security/crypto/CryptoUtils.java
index 77fdb757a0e..8fbbfd772bb 100644
--- 
a/rt/security/src/main/java/org/apache/cxf/rt/security/crypto/CryptoUtils.java
+++ 
b/rt/security/src/main/java/org/apache/cxf/rt/security/crypto/CryptoUtils.java
@@ -50,6 +50,8 @@ import java.security.spec.ECPublicKeySpec;
 import java.security.spec.RSAPrivateCrtKeySpec;
 import java.security.spec.RSAPrivateKeySpec;
 import java.security.spec.RSAPublicKeySpec;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.crypto.Cipher;
 import javax.crypto.KeyGenerator;
@@ -59,6 +61,7 @@ import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 import javax.security.auth.DestroyFailedException;
 
+import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.Base64UrlUtility;
 import org.apache.cxf.common.util.Base64Utility;
 import org.apache.cxf.common.util.CompressionUtils;
@@ -71,6 +74,8 @@ import org.apache.cxf.helpers.JavaUtils;
  */
 public final class CryptoUtils {
 
+    private static final Logger LOG = LogUtils.getL7dLogger(CryptoUtils.class);
+
     static {
         if (JavaUtils.isFIPSEnabled()) {
             String securityProviderName = JavaUtils.getFIPSSecurityProvider();
@@ -723,9 +728,13 @@ public final class CryptoUtils {
     public static Certificate loadCertificate(KeyStore keyStore, String alias) 
{
         try {
             if (alias == null) {
-                throw new SecurityException("No keystore alias was defined");
-            }
-            if (!keyStore.containsAlias(alias)) {
+                if (keyStore.size() == 1) {
+                    alias = keyStore.aliases().nextElement();
+                    LOG.log(Level.FINE, "No keystore alias specified; use 
single available {}", alias);
+                } else {
+                    throw new SecurityException("No keystore alias was 
defined");
+                }
+            } else if (!keyStore.containsAlias(alias)) {
                 throw new SecurityException("No alias exists in the keystore 
for: " + alias);
             }
             return keyStore.getCertificate(alias);
diff --git 
a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/abigcompany_ca.pem
 
b/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/abigcompany_ca.pem
deleted file mode 100644
index e69de29bb2d..00000000000
diff --git 
a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/celtix.p12
 
b/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/celtix.p12
deleted file mode 100644
index 40a13b2f111..00000000000
Binary files 
a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/celtix.p12
 and /dev/null differ
diff --git 
a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/celtixp12truststore
 
b/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/celtixp12truststore
deleted file mode 100644
index 28018612ec2..00000000000
Binary files 
a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/celtixp12truststore
 and /dev/null differ
diff --git 
a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/defaultkeystore
 
b/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/defaultkeystore
deleted file mode 100644
index 7076562543f..00000000000
Binary files 
a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/defaultkeystore
 and /dev/null differ
diff --git 
a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/defaulttruststore
 
b/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/defaulttruststore
deleted file mode 100644
index fb3f94d9e51..00000000000
Binary files 
a/rt/transports/http/src/test/java/org/apache/cxf/transport/https/resources/defaulttruststore
 and /dev/null differ
diff --git 
a/systests/rs-security/src/test/java/org/apache/cxf/systest/crypto/CryptoUtilsTest.java
 
b/systests/rs-security/src/test/java/org/apache/cxf/systest/crypto/CryptoUtilsTest.java
new file mode 100644
index 00000000000..ad7ee820a44
--- /dev/null
+++ 
b/systests/rs-security/src/test/java/org/apache/cxf/systest/crypto/CryptoUtilsTest.java
@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.systest.crypto;
+
+import java.io.InputStream;
+
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
+import org.apache.cxf.rt.security.crypto.CryptoUtils;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CryptoUtilsTest {
+
+    @Test
+    public void testLoadCertificateNoAlias() throws Exception {
+        try (InputStream is = 
ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", this.getClass())) {
+            CryptoUtils.loadCertificate(is, "password".toCharArray(), null, 
null);
+            Assert.fail();
+        } catch (SecurityException e) {
+            // expected
+        }
+    }
+
+    /*
+    keytool -genkeypair \
+      -alias somealias \
+      -keyalg RSA -keysize 2048 \
+      -validity 365 \
+      -dname "CN=example.com, O=MyOrg, C=PL" \
+      -storetype PKCS12 \
+      -keystore somealias.p12 \
+      -storepass changeit
+    */
+    @Test
+    public void testLoadCertificateNoAliasSingle() throws Exception {
+        try (InputStream is = 
this.getClass().getResourceAsStream("somealias.p12")) {
+            Assert.assertNotNull(CryptoUtils.loadCertificate(is, 
"changeit".toCharArray(), null, null));
+        }
+    }
+
+}
diff --git 
a/systests/rs-security/src/test/java/org/apache/cxf/systest/crypto/somealias.p12
 
b/systests/rs-security/src/test/java/org/apache/cxf/systest/crypto/somealias.p12
new file mode 100644
index 00000000000..a42c6361d39
Binary files /dev/null and 
b/systests/rs-security/src/test/java/org/apache/cxf/systest/crypto/somealias.p12
 differ

Reply via email to