paramSpec)
throws InvalidParameterSpecException {
- if (GCMParameterSpec.class.isAssignableFrom(paramSpec)) {
+ if (paramSpec.isAssignableFrom(GCMParameterSpec.class)) {
return paramSpec.cast(new GCMParameterSpec(tLen * 8, iv));
} else {
throw new InvalidParameterSpecException
diff --git a/src/java.base/share/classes/sun/security/util/KeyUtil.java b/src/java.base/share/classes/sun/security/util/KeyUtil.java
index a84aa1529a6b..1806fc7c46ad 100644
--- a/src/java.base/share/classes/sun/security/util/KeyUtil.java
+++ b/src/java.base/share/classes/sun/security/util/KeyUtil.java
@@ -25,8 +25,12 @@
package sun.security.util;
+import java.security.AccessController;
import java.security.AlgorithmParameters;
import java.security.Key;
+import java.security.PrivilegedAction;
+import java.security.PrivateKey;
+import java.security.PublicKey;
import java.security.InvalidKeyException;
import java.security.interfaces.ECKey;
import java.security.interfaces.EdECKey;
@@ -192,6 +196,28 @@ public static final int getKeySize(AlgorithmParameters parameters) {
return -1;
}
+ /**
+ * Returns the algorithm name of the given key object. If an EC key is
+ * specified, returns the algorithm name and its named curve.
+ *
+ * @param key the key object, cannot be null
+ * @return the algorithm name of the given key object, or return in the
+ * form of "EC (named curve)" if the given key object is an EC key
+ */
+ public static final String fullDisplayAlgName(Key key) {
+ String result = key.getAlgorithm();
+ if (key instanceof PrivateKey || key instanceof PublicKey) {
+ AlgorithmParameterSpec paramSpec = getParams(key);
+ if (paramSpec instanceof NamedCurve) {
+ NamedCurve nc = (NamedCurve)paramSpec;
+ result += " (" + nc.getNameAndAliases()[0] + ")";
+ } else if (paramSpec instanceof NamedParameterSpec nps) {
+ result = nps.getName();
+ }
+ }
+ return result;
+ }
+
/**
* Returns whether the key is valid or not.
*
@@ -421,5 +447,25 @@ public static byte[] trimZeroes(byte[] b) {
return t;
}
+ @SuppressWarnings({"deprecation", "removal"})
+ public static AlgorithmParameterSpec getParams(Key key) {
+ try {
+ var m = key.getClass().getMethod("getParams");
+ if (!m.isAccessible()) {
+ PrivilegedAction