From 87216d7ed1913bc842a7df9b792603cba70286d4 Mon Sep 17 00:00:00 2001 From: Weijun Wang Date: Tue, 8 Oct 2024 16:30:57 -0400 Subject: [PATCH] resolve Sean's comments; some small utility method updates --- .../sun/security/provider/NamedSignature.java | 26 +++++++++---------- .../classes/sun/security/util/KeyUtil.java | 8 +++--- .../sun/security/util/SignatureUtil.java | 8 +++--- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/java.base/share/classes/sun/security/provider/NamedSignature.java b/src/java.base/share/classes/sun/security/provider/NamedSignature.java index e50dc7bc779dc..0ba2b87582c21 100644 --- a/src/java.base/share/classes/sun/security/provider/NamedSignature.java +++ b/src/java.base/share/classes/sun/security/provider/NamedSignature.java @@ -55,12 +55,12 @@ public abstract class NamedSignature extends SignatureSpi { private final ByteArrayOutputStream bout = new ByteArrayOutputStream(); // init with... - private String name = null; - private byte[] secKey = null; - private byte[] pubKey = null; + private String name; + private byte[] secKey; + private byte[] pubKey; - private Object sk2 = null; - private Object pk2 = null; + private Object sk2; + private Object pk2; /// Creates a new `NamedSignature` object. /// @@ -118,7 +118,7 @@ protected byte[] engineSign() throws SignatureException { bout.reset(); return implSign(name, secKey, sk2, msg, appRandom); } else { - throw new IllegalStateException("No private key"); + throw new SignatureException("No private key"); } } @@ -129,7 +129,7 @@ protected boolean engineVerify(byte[] sig) throws SignatureException { bout.reset(); return implVerify(name, pubKey, pk2, msg, sig); } else { - throw new IllegalStateException("No public key"); + throw new SignatureException("No public key"); } } @@ -137,13 +137,13 @@ protected boolean engineVerify(byte[] sig) throws SignatureException { @SuppressWarnings("deprecation") protected void engineSetParameter(String param, Object value) throws InvalidParameterException { - throw new UnsupportedOperationException("setParameter() not supported"); + throw new InvalidParameterException("setParameter() not supported"); } @Override @SuppressWarnings("deprecation") protected Object engineGetParameter(String param) throws InvalidParameterException { - throw new UnsupportedOperationException("getParameter() not supported"); + throw new InvalidParameterException("getParameter() not supported"); } @Override @@ -188,9 +188,9 @@ protected abstract boolean implVerify(String name, byte[] pk, Object pk2, /// User-defined function to validate a public key. /// - /// This method will be called in `initVerify`. This gives provider a chance to + /// This method will be called in `initVerify`. This gives the provider a chance to /// reject the key so an `InvalidKeyException` can be thrown earlier. - /// An implementation can optional return a "parsed key" as an `Object` value. + /// An implementation can optionally return a "parsed key" as an `Object` value. /// This object will be passed into the [#implVerify] method along with the raw key. /// /// The default implementation returns `null`. @@ -205,9 +205,9 @@ protected Object implCheckPublicKey(String name, byte[] pk) throws InvalidKeyExc /// User-defined function to validate a private key. /// - /// This method will be called in `initSign`. This gives provider a chance to + /// This method will be called in `initSign`. This gives the provider a chance to /// reject the key so an `InvalidKeyException` can be thrown earlier. - /// An implementation can optional return a "parsed key" as an `Object` value. + /// An implementation can optionally return a "parsed key" as an `Object` value. /// This object will be passed into the [#implSign] method along with the raw key. /// /// The default implementation returns `null`. 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 6884b9b201a39..d057bb689e99e 100644 --- a/src/java.base/share/classes/sun/security/util/KeyUtil.java +++ b/src/java.base/share/classes/sun/security/util/KeyUtil.java @@ -184,13 +184,13 @@ public static final int getKeySize(AlgorithmParameters parameters) { */ public static final String fullDisplayAlgName(Key key) { String result = key.getAlgorithm(); - if (key instanceof ECKey) { - ECParameterSpec paramSpec = ((ECKey) key).getParams(); + if (key instanceof AsymmetricKey ak) { + AlgorithmParameterSpec paramSpec = ak.getParams(); if (paramSpec instanceof NamedCurve nc) { result += " (" + nc.getNameAndAliases()[0] + ")"; + } else if (paramSpec instanceof NamedParameterSpec nps) { + result = nps.getName(); } - } else if (key instanceof EdECKey) { - result = ((EdECKey) key).getParams().getName(); } return result; } diff --git a/src/java.base/share/classes/sun/security/util/SignatureUtil.java b/src/java.base/share/classes/sun/security/util/SignatureUtil.java index d6d73a4349a3f..b46c1d3da0c2d 100644 --- a/src/java.base/share/classes/sun/security/util/SignatureUtil.java +++ b/src/java.base/share/classes/sun/security/util/SignatureUtil.java @@ -274,7 +274,7 @@ public static String extractDigestAlgFromDwithE(String signatureAlgorithm) { return signatureAlgorithm.substring(0, with); } else { throw new IllegalArgumentException( - "Unknown algorithm: " + signatureAlgorithm); + "Cannot extract digest algorithm from " + signatureAlgorithm); } } @@ -495,8 +495,10 @@ public static String getDefaultSigAlgForKey(PrivateKey k) { case "EDDSA" -> k instanceof EdECPrivateKey ? ((EdECPrivateKey) k).getParams().getName() : kAlg; - default -> kAlg; // All modern signature algorithms, - // RSASSA-PSS, ED25519, ED448, HSS/LMS, etc + default -> kAlg.contains("KEM") ? null : kAlg; + // All modern signature algorithms use the same name across + // key algorithms and signature algorithms, for example, + // RSASSA-PSS, ED25519, ED448, HSS/LMS, etc }; }