8325164: Named groups and signature schemes unavailable with SunPKCS11 in FIPS mode

Reviewed-by: valeriep
This commit is contained in:
Daniel Jeliński 2024-03-13 19:09:52 +00:00
parent eb45d5bd64
commit 8f9899b23e
10 changed files with 66 additions and 67 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -29,7 +29,6 @@ import java.security.*;
import java.security.spec.AlgorithmParameterSpec; import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.ECGenParameterSpec; import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec; import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.InvalidParameterSpecException; import java.security.spec.InvalidParameterSpecException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
@ -37,7 +36,7 @@ import java.util.Optional;
import sun.security.jca.JCAUtil; import sun.security.jca.JCAUtil;
import sun.security.util.ECUtil; import sun.security.util.ECUtil;
import sun.security.util.math.*; import sun.security.util.math.*;
import sun.security.ec.point.*;
import static sun.security.util.SecurityProviderConstants.DEF_EC_KEY_SIZE; import static sun.security.util.SecurityProviderConstants.DEF_EC_KEY_SIZE;
import static sun.security.ec.ECOperations.IntermediateValueException; import static sun.security.ec.ECOperations.IntermediateValueException;
@ -74,7 +73,7 @@ public final class ECKeyPairGenerator extends KeyPairGeneratorSpi {
public void initialize(int keySize, SecureRandom random) { public void initialize(int keySize, SecureRandom random) {
checkKeySize(keySize); checkKeySize(keySize);
this.params = ECUtil.getECParameterSpec(null, keySize); this.params = ECUtil.getECParameterSpec(keySize);
if (params == null) { if (params == null) {
throw new InvalidParameterException( throw new InvalidParameterException(
"No EC parameters available for key size " + keySize + " bits"); "No EC parameters available for key size " + keySize + " bits");
@ -91,14 +90,14 @@ public final class ECKeyPairGenerator extends KeyPairGeneratorSpi {
if (params instanceof ECParameterSpec) { if (params instanceof ECParameterSpec) {
ECParameterSpec ecParams = (ECParameterSpec) params; ECParameterSpec ecParams = (ECParameterSpec) params;
ecSpec = ECUtil.getECParameterSpec(null, ecParams); ecSpec = ECUtil.getECParameterSpec(ecParams);
if (ecSpec == null) { if (ecSpec == null) {
throw new InvalidAlgorithmParameterException( throw new InvalidAlgorithmParameterException(
"Curve not supported: " + params); "Curve not supported: " + params);
} }
} else if (params instanceof ECGenParameterSpec) { } else if (params instanceof ECGenParameterSpec) {
String name = ((ECGenParameterSpec) params).getName(); String name = ((ECGenParameterSpec) params).getName();
ecSpec = ECUtil.getECParameterSpec(null, name); ecSpec = ECUtil.getECParameterSpec(name);
if (ecSpec == null) { if (ecSpec == null) {
throw new InvalidAlgorithmParameterException( throw new InvalidAlgorithmParameterException(
"Unknown curve name: " + name); "Unknown curve name: " + name);
@ -120,7 +119,7 @@ public final class ECKeyPairGenerator extends KeyPairGeneratorSpi {
throws InvalidAlgorithmParameterException { throws InvalidAlgorithmParameterException {
// Check if ecSpec is a valid curve // Check if ecSpec is a valid curve
AlgorithmParameters ecParams = ECUtil.getECParameters(null); AlgorithmParameters ecParams = ECUtil.getECParameters();
try { try {
ecParams.init(ecSpec); ecParams.init(ecSpec);
} catch (InvalidParameterSpecException ex) { } catch (InvalidParameterSpecException ex) {

View File

@ -140,21 +140,16 @@ public final class ECUtil {
return (ECPrivateKey)keyFactory.generatePrivate(keySpec); return (ECPrivateKey)keyFactory.generatePrivate(keySpec);
} }
public static AlgorithmParameters getECParameters(Provider p) { public static AlgorithmParameters getECParameters() {
try { try {
if (p != null) {
return AlgorithmParameters.getInstance("EC", p);
}
return AlgorithmParameters.getInstance("EC"); return AlgorithmParameters.getInstance("EC");
} catch (NoSuchAlgorithmException nsae) { } catch (NoSuchAlgorithmException nsae) {
throw new RuntimeException(nsae); throw new RuntimeException(nsae);
} }
} }
public static byte[] encodeECParameterSpec(Provider p, public static byte[] encodeECParameterSpec(ECParameterSpec spec) {
ECParameterSpec spec) { AlgorithmParameters parameters = getECParameters();
AlgorithmParameters parameters = getECParameters(p);
try { try {
parameters.init(spec); parameters.init(spec);
@ -170,9 +165,8 @@ public final class ECUtil {
} }
} }
public static ECParameterSpec getECParameterSpec(Provider p, public static ECParameterSpec getECParameterSpec(ECParameterSpec spec) {
ECParameterSpec spec) { AlgorithmParameters parameters = getECParameters();
AlgorithmParameters parameters = getECParameters(p);
try { try {
parameters.init(spec); parameters.init(spec);
@ -182,10 +176,9 @@ public final class ECUtil {
} }
} }
public static ECParameterSpec getECParameterSpec(Provider p, public static ECParameterSpec getECParameterSpec(byte[] params)
byte[] params)
throws IOException { throws IOException {
AlgorithmParameters parameters = getECParameters(p); AlgorithmParameters parameters = getECParameters();
parameters.init(params); parameters.init(params);
@ -196,8 +189,8 @@ public final class ECUtil {
} }
} }
public static ECParameterSpec getECParameterSpec(Provider p, String name) { public static ECParameterSpec getECParameterSpec(String name) {
AlgorithmParameters parameters = getECParameters(p); AlgorithmParameters parameters = getECParameters();
try { try {
parameters.init(new ECGenParameterSpec(name)); parameters.init(new ECGenParameterSpec(name));
@ -207,8 +200,8 @@ public final class ECUtil {
} }
} }
public static ECParameterSpec getECParameterSpec(Provider p, int keySize) { public static ECParameterSpec getECParameterSpec(int keySize) {
AlgorithmParameters parameters = getECParameters(p); AlgorithmParameters parameters = getECParameters();
try { try {
parameters.init(new ECKeySizeParameterSpec(keySize)); parameters.init(new ECKeySizeParameterSpec(keySize));
@ -219,9 +212,9 @@ public final class ECUtil {
} }
public static String getCurveName(Provider p, ECParameterSpec spec) { public static String getCurveName(ECParameterSpec spec) {
ECGenParameterSpec nameSpec; ECGenParameterSpec nameSpec;
AlgorithmParameters parameters = getECParameters(p); AlgorithmParameters parameters = getECParameters();
try { try {
parameters.init(spec); parameters.init(spec);

View File

@ -153,7 +153,7 @@ public final class KeyUtil {
// Note: the ECGenParameterSpec case should be covered by the // Note: the ECGenParameterSpec case should be covered by the
// ECParameterSpec case above. // ECParameterSpec case above.
// See ECUtil.getECParameterSpec(Provider, String). // See ECUtil.getECParameterSpec(String).
break; break;
case "DiffieHellman": case "DiffieHellman":

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -64,25 +64,25 @@ final class P11ECKeyFactory extends P11KeyFactory {
} }
static ECParameterSpec getECParameterSpec(String name) { static ECParameterSpec getECParameterSpec(String name) {
return ECUtil.getECParameterSpec(getSunECProvider(), name); return ECUtil.getECParameterSpec(name);
} }
static ECParameterSpec getECParameterSpec(int keySize) { static ECParameterSpec getECParameterSpec(int keySize) {
return ECUtil.getECParameterSpec(getSunECProvider(), keySize); return ECUtil.getECParameterSpec(keySize);
} }
// Check that spec is a known supported curve and convert it to our // Check that spec is a known supported curve and convert it to our
// ECParameterSpec subclass. If not possible, return null. // ECParameterSpec subclass. If not possible, return null.
static ECParameterSpec getECParameterSpec(ECParameterSpec spec) { static ECParameterSpec getECParameterSpec(ECParameterSpec spec) {
return ECUtil.getECParameterSpec(getSunECProvider(), spec); return ECUtil.getECParameterSpec(spec);
} }
static ECParameterSpec decodeParameters(byte[] params) throws IOException { static ECParameterSpec decodeParameters(byte[] params) throws IOException {
return ECUtil.getECParameterSpec(getSunECProvider(), params); return ECUtil.getECParameterSpec(params);
} }
static byte[] encodeParameters(ECParameterSpec params) { static byte[] encodeParameters(ECParameterSpec params) {
return ECUtil.encodeECParameterSpec(getSunECProvider(), params); return ECUtil.encodeECParameterSpec(params);
} }
static ECPoint decodePoint(byte[] encoded, EllipticCurve curve) throws IOException { static ECPoint decodePoint(byte[] encoded, EllipticCurve curve) throws IOException {
@ -220,7 +220,7 @@ final class P11ECKeyFactory extends P11KeyFactory {
private PublicKey generatePublic(ECPoint point, ECParameterSpec params) private PublicKey generatePublic(ECPoint point, ECParameterSpec params)
throws PKCS11Exception { throws PKCS11Exception {
byte[] encodedParams = byte[] encodedParams =
ECUtil.encodeECParameterSpec(getSunECProvider(), params); ECUtil.encodeECParameterSpec(params);
byte[] encodedPoint = byte[] encodedPoint =
ECUtil.encodePoint(point, params.getCurve()); ECUtil.encodePoint(point, params.getCurve());
@ -254,7 +254,7 @@ final class P11ECKeyFactory extends P11KeyFactory {
private PrivateKey generatePrivate(BigInteger s, ECParameterSpec params) private PrivateKey generatePrivate(BigInteger s, ECParameterSpec params)
throws PKCS11Exception { throws PKCS11Exception {
byte[] encodedParams = byte[] encodedParams =
ECUtil.encodeECParameterSpec(getSunECProvider(), params); ECUtil.encodeECParameterSpec(params);
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] { CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY), new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC), new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -1378,7 +1378,7 @@ final class P11KeyStore extends KeyStoreSpi {
byte[] encodedParams = attrs[0].getByteArray(); byte[] encodedParams = attrs[0].getByteArray();
try { try {
ECParameterSpec params = ECParameterSpec params =
ECUtil.getECParameterSpec(null, encodedParams); ECUtil.getECParameterSpec(encodedParams);
keyLength = params.getCurve().getField().getFieldSize(); keyLength = params.getCurve().getField().getFieldSize();
} catch (IOException e) { } catch (IOException e) {
// we do not want to accept key with unsupported parameters // we do not want to accept key with unsupported parameters
@ -1776,7 +1776,7 @@ final class P11KeyStore extends KeyStoreSpi {
} }
byte[] encodedParams = byte[] encodedParams =
ECUtil.encodeECParameterSpec(null, ecKey.getParams()); ECUtil.encodeECParameterSpec(ecKey.getParams());
attrs = new CK_ATTRIBUTE[] { attrs = new CK_ATTRIBUTE[] {
ATTR_TOKEN_TRUE, ATTR_TOKEN_TRUE,
ATTR_CLASS_PKEY, ATTR_CLASS_PKEY,

View File

@ -776,7 +776,7 @@ final class P11PSSSignature extends SignatureSpi {
protected AlgorithmParameters engineGetParameters() { protected AlgorithmParameters engineGetParameters() {
if (this.sigParams != null) { if (this.sigParams != null) {
try { try {
AlgorithmParameters ap = AlgorithmParameters.getInstance("RSASSA-PSS"); AlgorithmParameters ap = AlgorithmParameters.getInstance("RSASSA-PSS", token.provider);
ap.init(this.sigParams); ap.init(this.sigParams);
return ap; return ap;
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {

View File

@ -42,7 +42,9 @@ import javax.security.auth.callback.PasswordCallback;
import com.sun.crypto.provider.ChaCha20Poly1305Parameters; import com.sun.crypto.provider.ChaCha20Poly1305Parameters;
import com.sun.crypto.provider.DHParameters;
import jdk.internal.misc.InnocuousThread; import jdk.internal.misc.InnocuousThread;
import sun.security.rsa.PSSParameters;
import sun.security.util.Debug; import sun.security.util.Debug;
import sun.security.util.ResourcesMgr; import sun.security.util.ResourcesMgr;
import static sun.security.util.SecurityConstants.PROVIDER_VER; import static sun.security.util.SecurityConstants.PROVIDER_VER;
@ -707,6 +709,14 @@ public final class SunPKCS11 extends AuthProvider {
"com.sun.crypto.provider.ChaCha20Poly1305Parameters", "com.sun.crypto.provider.ChaCha20Poly1305Parameters",
m(CKM_CHACHA20_POLY1305)); m(CKM_CHACHA20_POLY1305));
dA(AGP, "RSASSA-PSS",
"sun.security.rsa.PSSParameters",
m(CKM_RSA_PKCS_PSS));
dA(AGP, "DiffieHellman",
"com.sun.crypto.provider.DHParameters",
m(CKM_DH_PKCS_DERIVE));
d(KA, "DH", P11KeyAgreement, d(KA, "DH", P11KeyAgreement,
dhAlias, dhAlias,
m(CKM_DH_PKCS_DERIVE)); m(CKM_DH_PKCS_DERIVE));
@ -1496,6 +1506,10 @@ public final class SunPKCS11 extends AuthProvider {
return new sun.security.util.GCMParameters(); return new sun.security.util.GCMParameters();
} else if (algorithm == "ChaCha20-Poly1305") { } else if (algorithm == "ChaCha20-Poly1305") {
return new ChaCha20Poly1305Parameters(); // from SunJCE return new ChaCha20Poly1305Parameters(); // from SunJCE
} else if (algorithm == "RSASSA-PSS") {
return new PSSParameters(); // from SunRsaSign
} else if (algorithm == "DiffieHellman") {
return new DHParameters(); // from SunJCE
} else { } else {
throw new NoSuchAlgorithmException("Unsupported algorithm: " throw new NoSuchAlgorithmException("Unsupported algorithm: "
+ algorithm); + algorithm);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,7 +27,7 @@ import java.security.interfaces.*;
/* /*
* @test * @test
* @bug 8080462 8226651 8242332 * @bug 8080462 8226651 8242332 8325164
* @summary testing interoperability of PSS signatures of PKCS11 provider * @summary testing interoperability of PSS signatures of PKCS11 provider
* against SunRsaSign provider * against SunRsaSign provider
* @library /test/lib .. * @library /test/lib ..
@ -51,9 +51,12 @@ public class SigInteropPSS2 extends PKCS11Test {
@Override @Override
public void main(Provider p) throws Exception { public void main(Provider p) throws Exception {
Provider sunRsaSign = Security.getProvider("SunRsaSign");
Security.removeProvider("SunRsaSign");
Signature sigPkcs11; Signature sigPkcs11;
Signature sigSunRsaSign = Signature sigSunRsaSign =
Signature.getInstance("RSASSA-PSS", "SunRsaSign"); Signature.getInstance("RSASSA-PSS", sunRsaSign);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p); KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
kpg.initialize(3072); kpg.initialize(3072);

View File

@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8029661 * @bug 8029661 8325164
* @summary Test TLS 1.2 * @summary Test TLS 1.2
* @modules java.base/sun.security.internal.spec * @modules java.base/sun.security.internal.spec
* java.base/sun.security.util * java.base/sun.security.util
@ -412,6 +412,18 @@ public final class FipsModeTLS12 extends SecmodTest {
ssle = sslCtx.createSSLEngine("localhost", 443); ssle = sslCtx.createSSLEngine("localhost", 443);
ssle.setUseClientMode(client); ssle.setUseClientMode(client);
SSLParameters sslParameters = ssle.getSSLParameters(); SSLParameters sslParameters = ssle.getSSLParameters();
// verify that FFDHE named groups are available
boolean ffdheAvailable = Arrays.stream(sslParameters.getNamedGroups())
.anyMatch(ng -> ng.startsWith("ffdhe"));
if (!ffdheAvailable) {
throw new RuntimeException("No FFDHE named groups available");
}
// verify that ECDHE named groups are available
boolean ecdheAvailable = Arrays.stream(sslParameters.getNamedGroups())
.anyMatch(ng -> ng.startsWith("secp"));
if (!ecdheAvailable) {
throw new RuntimeException("No ECDHE named groups available");
}
ssle.setSSLParameters(sslParameters); ssle.setSSLParameters(sslParameters);
return ssle; return ssle;
@ -426,28 +438,6 @@ public final class FipsModeTLS12 extends SecmodTest {
// 1. SunPKCS11 (with an NSS FIPS mode backend) // 1. SunPKCS11 (with an NSS FIPS mode backend)
// 2. SUN (to handle X.509 certificates) // 2. SUN (to handle X.509 certificates)
// 3. SunJSSE (for a TLS engine) // 3. SunJSSE (for a TLS engine)
//
// RSASSA-PSS algorithm is not currently supported in SunPKCS11
// but in SUN provider. As a result, it can be negotiated by the
// TLS engine. The problem is that SunPKCS11 keys are sensitive
// in FIPS mode and cannot be used in a SUN algorithm (conversion
// fails as plain values cannot be extracted).
//
// To workaround this issue, we disable RSASSA-PSS algorithm for
// TLS connections. Once JDK-8222937 is fixed, this workaround can
// (and should) be removed.
//
// On a final note, the list of disabled TLS algorithms
// (jdk.tls.disabledAlgorithms) has to be updated at this point,
// before it is read in sun.security.ssl.SSLAlgorithmConstraints
// class initialization.
String disabledAlgorithms =
Security.getProperty("jdk.tls.disabledAlgorithms");
if (disabledAlgorithms.length() > 0) {
disabledAlgorithms += ", ";
}
disabledAlgorithms += "RSASSA-PSS";
Security.setProperty("jdk.tls.disabledAlgorithms", disabledAlgorithms);
if (initSecmod() == false) { if (initSecmod() == false) {
return; return;

View File

@ -74,7 +74,7 @@ public final class ECKeyPairGenerator extends KeyPairGeneratorSpi {
" has been patched. Key size " + keySize + " has been patched. Key size " + keySize +
" is not supported"); " is not supported");
} }
ECParameterSpec ecParams = ECUtil.getECParameterSpec(null, keySize); ECParameterSpec ecParams = ECUtil.getECParameterSpec(keySize);
try { try {
return new KeyPair(new ECPublicKeyImpl(new ECPoint(x, y), ecParams), return new KeyPair(new ECPublicKeyImpl(new ECPoint(x, y), ecParams),
new ECPrivateKeyImpl(s, ecParams)); new ECPrivateKeyImpl(s, ecParams));