Skip to content

Commit

Permalink
resolve Sean's comments; some small utility method updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweij committed Oct 8, 2024
1 parent 986db83 commit 87216d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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");
}
}

Expand All @@ -129,21 +129,21 @@ 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");
}
}

@Override
@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
Expand Down Expand Up @@ -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`.
Expand All @@ -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`.
Expand Down
8 changes: 4 additions & 4 deletions src/java.base/share/classes/sun/security/util/KeyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
};
}

Expand Down

0 comments on commit 87216d7

Please sign in to comment.