Skip to content

Commit

Permalink
null check as asserts, and better exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweij committed Oct 7, 2024
1 parent b55323b commit 986db83
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/java.base/share/classes/sun/security/provider/NamedKEM.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ public abstract class NamedKEM implements KEMSpi {
/// @param fname the family name
/// @param pnames the standard parameter set names, at least one is needed.
protected NamedKEM(String fname, String... pnames) {
this.fname = Objects.requireNonNull(fname);
if (fname == null) {
throw new AssertionError("fname cannot be null");
}
if (pnames == null || pnames.length == 0) {
throw new AssertionError("pnames cannot be null or empty");
}
this.fname = fname;
this.pnames = pnames;
}

Expand All @@ -69,7 +72,8 @@ public EncapsulatorSpi engineNewEncapsulator(PublicKey publicKey,
AlgorithmParameterSpec spec, SecureRandom secureRandom)
throws InvalidAlgorithmParameterException, InvalidKeyException {
if (spec != null) {
throw new InvalidAlgorithmParameterException("No params needed");
throw new InvalidAlgorithmParameterException(
"The " + fname + " algorithm does not take any parameters");
}
// translate also check the key
var nk = (NamedX509Key) new NamedKeyFactory(fname, pnames)
Expand All @@ -84,7 +88,8 @@ public DecapsulatorSpi engineNewDecapsulator(
PrivateKey privateKey, AlgorithmParameterSpec spec)
throws InvalidAlgorithmParameterException, InvalidKeyException {
if (spec != null) {
throw new InvalidAlgorithmParameterException("No params needed");
throw new InvalidAlgorithmParameterException(
"The " + fname + " algorithm does not take any parameters");
}
// translate also check the key
var nk = (NamedPKCS8Key) new NamedKeyFactory(fname, pnames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ public class NamedKeyFactory extends KeyFactorySpi {
/// @param fname the family name
/// @param pnames the standard parameter set names, at least one is needed.
protected NamedKeyFactory(String fname, String... pnames) {
this.fname = Objects.requireNonNull(fname);
if (fname == null) {
throw new AssertionError("fname cannot be null");
}
if (pnames == null || pnames.length == 0) {
throw new AssertionError("pnames cannot be null or empty");
}
this.fname = fname;
this.pnames = pnames;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,13 @@ public abstract class NamedKeyPairGenerator extends KeyPairGeneratorSpi {
/// @param pnames supported parameter set names, at least one is needed.
/// If multiple, the first one becomes the default parameter set name.
protected NamedKeyPairGenerator(String fname, String... pnames) {
this.fname = Objects.requireNonNull(fname);
if (fname == null) {
throw new AssertionError("fname cannot be null");
}
if (pnames == null || pnames.length == 0) {
throw new AssertionError("pnames cannot be null or empty");
}
this.fname = fname;
this.pnames = pnames;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ public abstract class NamedSignature extends SignatureSpi {
/// @param fname the family name
/// @param pnames the standard parameter set names, at least one is needed.
protected NamedSignature(String fname, String... pnames) {
this.fname = Objects.requireNonNull(fname);
if (fname == null) {
throw new AssertionError("fname cannot be null");
}
if (pnames == null || pnames.length == 0) {
throw new AssertionError("pnames cannot be null or empty");
}
this.fname = fname;
this.pnames = pnames;
}

Expand Down Expand Up @@ -147,7 +150,8 @@ protected Object engineGetParameter(String param) throws InvalidParameterExcepti
protected void engineSetParameter(AlgorithmParameterSpec params)
throws InvalidAlgorithmParameterException {
if (params != null) {
throw new InvalidAlgorithmParameterException("No params needed");
throw new InvalidAlgorithmParameterException(
"The " + fname + " algorithm does not take any parameters");
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/jdk/sun/security/provider/NamedEdDSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*/

import jdk.test.lib.Asserts;
import jdk.test.lib.Utils;
import sun.security.ec.ed.EdDSAOperations;
import sun.security.ec.ed.EdDSAParameters;
import sun.security.ec.point.AffinePoint;
Expand Down Expand Up @@ -230,5 +231,9 @@ static void test(Provider p1, Provider p2, Provider p3) throws Exception {
Asserts.assertTrue(s2.verify(sig1));
s2.initVerify(pk);
Asserts.assertTrue(s2.verify(sig2));
// No parameters defined
s1.setParameter(null);
Utils.runAndCheckException(() -> s1.setParameter(NamedParameterSpec.ED448),
InvalidAlgorithmParameterException.class);
}
}

0 comments on commit 986db83

Please sign in to comment.