Skip to content

8244336: Restrict algorithms at JCE layer #26377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.sun.crypto.provider;

import java.io.ByteArrayOutputStream;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.SignatureSpi;
import java.security.InvalidKeyException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidParameterException;
import java.security.ProviderException;
import java.security.SignatureException;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
* NONEwithRSA Signature implementation using RSA/ECB/PKCS1Padding Cipher
* implementation.
*
* This is mostly refactored from the private static CipherAdapter class
* in the java.security.Signature class
*/
public final class RSACipherAdaptor extends SignatureSpi {

private final RSACipher c;
private ByteArrayOutputStream verifyBuf;

public RSACipherAdaptor() {
c = new RSACipher();
}

protected void engineInitVerify(PublicKey publicKey)
throws InvalidKeyException {
c.engineInit(Cipher.DECRYPT_MODE, publicKey, null);
if (verifyBuf == null) {
verifyBuf = new ByteArrayOutputStream(128);
} else {
verifyBuf.reset();
}
}

protected void engineInitSign(PrivateKey privateKey)
throws InvalidKeyException {
c.engineInit(Cipher.ENCRYPT_MODE, privateKey, null);
verifyBuf = null;
}

protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
throws InvalidKeyException {
c.engineInit(Cipher.ENCRYPT_MODE, privateKey, random);
verifyBuf = null;
}

protected void engineUpdate(byte b) throws SignatureException {
engineUpdate(new byte[] {b}, 0, 1);
}

protected void engineUpdate(byte[] b, int off, int len)
throws SignatureException {
if (verifyBuf != null) {
verifyBuf.write(b, off, len);
} else {
byte[] out = c.engineUpdate(b, off, len);
if ((out != null) && (out.length != 0)) {
throw new SignatureException
("Cipher unexpectedly returned data");
}
}
}

protected byte[] engineSign() throws SignatureException {
try {
return c.engineDoFinal(null, 0, 0);
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new SignatureException("doFinal() failed", e);
}
}

protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
try {
byte[] out = c.engineDoFinal(sigBytes, 0, sigBytes.length);
byte[] data = verifyBuf.toByteArray();
verifyBuf.reset();
return MessageDigest.isEqual(out, data);
} catch (BadPaddingException e) {
// e.g. wrong public key used
// return false rather than throwing exception
return false;
} catch (IllegalBlockSizeException e) {
throw new SignatureException("doFinal() failed", e);
}
}

protected void engineSetParameter(AlgorithmParameterSpec params)
throws InvalidAlgorithmParameterException {
if (params != null) {
throw new InvalidParameterException("Parameters not supported");
}
}

@SuppressWarnings("deprecation")
protected void engineSetParameter(String param, Object value)
throws InvalidParameterException {
throw new InvalidParameterException("Parameters not supported");
}

@SuppressWarnings("deprecation")
protected Object engineGetParameter(String param)
throws InvalidParameterException {
throw new InvalidParameterException("Parameters not supported");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public SunJCE() {
void putEntries() {
// reuse attribute map and reset before each reuse
HashMap<String, String> attrs = new HashMap<>(3);
attrs.put("SupportedKeyClasses",
"java.security.interfaces.RSAPublicKey" +
"|java.security.interfaces.RSAPrivateKey");
ps("Signature", "NONEwithRSA",
"com.sun.crypto.provider.RSACipherAdaptor", null, attrs);
// continue adding cipher specific attributes
attrs.put("SupportedModes", "ECB");
attrs.put("SupportedPaddings", "NOPADDING|PKCS1PADDING|OAEPPADDING"
+ "|OAEPWITHMD5ANDMGF1PADDING"
Expand All @@ -147,9 +153,6 @@ void putEntries() {
+ "|OAEPWITHSHA-512ANDMGF1PADDING"
+ "|OAEPWITHSHA-512/224ANDMGF1PADDING"
+ "|OAEPWITHSHA-512/256ANDMGF1PADDING");
attrs.put("SupportedKeyClasses",
"java.security.interfaces.RSAPublicKey" +
"|java.security.interfaces.RSAPrivateKey");
ps("Cipher", "RSA",
"com.sun.crypto.provider.RSACipher", null, attrs);

Expand Down
67 changes: 61 additions & 6 deletions src/java.base/share/classes/java/security/KeyStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import javax.security.auth.callback.*;

import sun.security.util.Debug;
import sun.security.util.CryptoAlgorithmConstraints;

/**
* This class represents a storage facility for cryptographic
Expand Down Expand Up @@ -841,12 +842,19 @@ private String getProviderName() {
* the {@link Security#getProviders() Security.getProviders()} method.
*
* @implNote
* The JDK Reference Implementation additionally uses the
* {@code jdk.security.provider.preferred}
* The JDK Reference Implementation additionally uses
* <ul>
* <li>the {@code jdk.security.provider.preferred}
* {@link Security#getProperty(String) Security} property to determine
* the preferred provider order for the specified algorithm. This
* the preferred provider order for the specified keystore type. This
* may be different from the order of providers returned by
* {@link Security#getProviders() Security.getProviders()}.
* </li>
* <li>the {@code jdk.crypto.disabledAlgorithms}
* {@link Security#getProperty(String) Security} property to determine
* if the specified keystore type is allowed.
* </li>
* </ul>
*
* @param type the type of keystore.
* See the KeyStore section in the <a href=
Expand All @@ -869,6 +877,11 @@ public static KeyStore getInstance(String type)
throws KeyStoreException
{
Objects.requireNonNull(type, "null type name");

if (!CryptoAlgorithmConstraints.permits("KEYSTORE", type)) {
throw new KeyStoreException(type + " is disabled");
}

try {
Object[] objs = Security.getImpl(type, "KeyStore", (String)null);
return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
Expand All @@ -888,6 +901,12 @@ public static KeyStore getInstance(String type)
* <p> Note that the list of registered providers may be retrieved via
* the {@link Security#getProviders() Security.getProviders()} method.
*
* @implNote
* The JDK Reference Implementation additionally uses
* the {@code jdk.crypto.disabledAlgorithms}
* {@link Security#getProperty(String) Security} property to determine
* if the specified keystore type is allowed.
*
* @param type the type of keystore.
* See the KeyStore section in the <a href=
* "{@docRoot}/../specs/security/standard-names.html#keystore-types">
Expand Down Expand Up @@ -917,8 +936,15 @@ public static KeyStore getInstance(String type, String provider)
throws KeyStoreException, NoSuchProviderException
{
Objects.requireNonNull(type, "null type name");
if (provider == null || provider.isEmpty())

if (provider == null || provider.isEmpty()) {
throw new IllegalArgumentException("missing provider");
}

if (!CryptoAlgorithmConstraints.permits("KEYSTORE", type)) {
throw new KeyStoreException(type + " is disabled");
}

try {
Object[] objs = Security.getImpl(type, "KeyStore", provider);
return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
Expand All @@ -935,6 +961,12 @@ public static KeyStore getInstance(String type, String provider)
* object is returned. Note that the specified provider object
* does not have to be registered in the provider list.
*
* @implNote
* The JDK Reference Implementation additionally uses
* the {@code jdk.crypto.disabledAlgorithms}
* {@link Security#getProperty(String) Security} property to determine
* if the specified keystore type is allowed.
*
* @param type the type of keystore.
* See the KeyStore section in the <a href=
* "{@docRoot}/../specs/security/standard-names.html#keystore-types">
Expand Down Expand Up @@ -963,8 +995,15 @@ public static KeyStore getInstance(String type, Provider provider)
throws KeyStoreException
{
Objects.requireNonNull(type, "null type name");
if (provider == null)

if (provider == null) {
throw new IllegalArgumentException("missing provider");
}

if (!CryptoAlgorithmConstraints.permits("KEYSTORE", type)) {
throw new KeyStoreException(type + " is disabled");
}

try {
Object[] objs = Security.getImpl(type, "KeyStore", provider);
return new KeyStore((KeyStoreSpi)objs[0], (Provider)objs[1], type);
Expand Down Expand Up @@ -1677,6 +1716,13 @@ public final void setEntry(String alias, Entry entry,
* <p> Note that the list of registered providers may be retrieved via
* the {@link Security#getProviders() Security.getProviders()} method.
*
* @implNote
* The JDK Reference Implementation additionally uses
* the {@code jdk.crypto.disabledAlgorithms}
* {@link Security#getProperty(String) Security} property to determine
* if the specified keystore type is allowed. Disallowed type will be
* skipped.
*
* @param file the keystore file
* @param password the keystore password, which may be {@code null}
*
Expand Down Expand Up @@ -1730,6 +1776,13 @@ public static final KeyStore getInstance(File file, char[] password)
* <p> Note that the list of registered providers may be retrieved via
* the {@link Security#getProviders() Security.getProviders()} method.
*
* @implNote
* The JDK Reference Implementation additionally uses
* the {@code jdk.crypto.disabledAlgorithms}
* {@link Security#getProperty(String) Security} property to determine
* if the specified keystore type is allowed. Disallowed type will be
* skipped.
*
* @param file the keystore file
* @param param the {@code LoadStoreParameter} that specifies how to load
* the keystore, which may be {@code null}
Expand Down Expand Up @@ -1790,7 +1843,9 @@ private static final KeyStore getInstance(File file, char[] password,
// Detect the keystore type
for (Provider p : Security.getProviders()) {
for (Provider.Service s : p.getServices()) {
if (s.getType().equals("KeyStore")) {
if (s.getType().equals("KeyStore") &&
CryptoAlgorithmConstraints.permits("KEYSTORE",
s.getAlgorithm())) {
try {
KeyStoreSpi impl = (KeyStoreSpi) s.newInstance(null);
if (impl.engineProbe(dataStream)) {
Expand Down
Loading