Skip to content
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,146 @@
/*
* 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.SignatureException;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;

/**
* NONEwithRSA Signature implementation using the RSA/ECB/PKCS1Padding Cipher
* implementation from SunJCE.
*/
public final class RSACipherAdaptor extends SignatureSpi {

private final RSACipher c;
private ByteArrayOutputStream verifyBuf;

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

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

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

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

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

@Override
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");
}
}
}

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

@Override
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);
}
}

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

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

@Override
@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 @@ -147,6 +147,12 @@ public Void run() {
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 @@ -158,9 +164,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
79 changes: 72 additions & 7 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 @@ -839,12 +840,21 @@ 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. If the
* {@systemProperty jdk.crypto.disabledAlgorithms} is set, it supersedes
* the security property value.
* </li>
* </ul>
*
* @param type the type of keystore.
* See the KeyStore section in the <a href=
Expand All @@ -866,6 +876,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 @@ -885,6 +900,14 @@ 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. If the
* {@systemProperty jdk.crypto.disabledAlgorithms} is set, it supersedes
* the security property value.
*
* @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 @@ -913,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 @@ -931,6 +961,14 @@ 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. If the
* {@systemProperty jdk.crypto.disabledAlgorithms} is set, it supersedes
* the security property value.
*
* @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 @@ -958,8 +996,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 @@ -1675,6 +1720,14 @@ 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. If the
* {@systemProperty jdk.crypto.disabledAlgorithms} is set, it supersedes
* the security property value. Disallowed type will be skipped.
*
* @param file the keystore file
* @param password the keystore password, which may be {@code null}
*
Expand Down Expand Up @@ -1731,6 +1784,14 @@ 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. If the
* {@systemProperty jdk.crypto.disabledAlgorithms} is set, it supersedes
* the security property value. 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 @@ -1802,8 +1863,12 @@ private static final KeyStore getInstance(File file, char[] password,
kdebug.println(s.getAlgorithm()
+ " keystore detected: " + file);
}
keystore = new KeyStore(impl, p, s.getAlgorithm());
break;
String ksAlgo = s.getAlgorithm();
if (CryptoAlgorithmConstraints.permits(
"KEYSTORE", ksAlgo)) {
keystore = new KeyStore(impl, p, ksAlgo);
break;
}
}
} catch (NoSuchAlgorithmException e) {
// ignore
Expand Down
Loading