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 @@ -141,6 +141,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 @@ -152,9 +158,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
33 changes: 28 additions & 5 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 @@ -847,7 +848,7 @@ private String getProviderName() {
* The JDK Reference Implementation additionally uses 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 than the order of providers returned by
* {@link Security#getProviders() Security.getProviders()}.
*
Expand All @@ -871,6 +872,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 Down Expand Up @@ -920,8 +926,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 Down Expand Up @@ -965,8 +978,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 @@ -1783,8 +1803,11 @@ private static final KeyStore getInstance(File file, char[] password,
file);
}

keystore = new KeyStore(impl, (Provider)objs[1], type);
break;
if (CryptoAlgorithmConstraints.permits(
"KEYSTORE", type)) {
keystore = new KeyStore(impl, (Provider)objs[1], type);
break;
}
}
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
// ignore
Expand Down
26 changes: 23 additions & 3 deletions src/java.base/share/classes/java/security/MessageDigest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

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

import javax.crypto.SecretKey;

Expand Down Expand Up @@ -173,10 +174,15 @@ protected MessageDigest(String algorithm) {
public static MessageDigest getInstance(String algorithm)
throws NoSuchAlgorithmException {
Objects.requireNonNull(algorithm, "null algorithm name");

if (!CryptoAlgorithmConstraints.permits("MessageDigest", algorithm)) {
throw new NoSuchAlgorithmException(algorithm + " is disabled");
}

try {
MessageDigest md;
Object[] objs = Security.getImpl(algorithm, "MessageDigest",
(String)null);
MessageDigest md;
if (objs[0] instanceof MessageDigest) {
md = (MessageDigest)objs[0];
} else {
Expand Down Expand Up @@ -237,8 +243,15 @@ public static MessageDigest getInstance(String algorithm, String provider)
throws NoSuchAlgorithmException, NoSuchProviderException
{
Objects.requireNonNull(algorithm, "null algorithm name");
if (provider == null || provider.isEmpty())

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

if (!CryptoAlgorithmConstraints.permits("MessageDigest", algorithm)) {
throw new NoSuchAlgorithmException(algorithm + " is disabled");
}

Object[] objs = Security.getImpl(algorithm, "MessageDigest", provider);
if (objs[0] instanceof MessageDigest) {
MessageDigest md = (MessageDigest)objs[0];
Expand Down Expand Up @@ -290,8 +303,15 @@ public static MessageDigest getInstance(String algorithm,
throws NoSuchAlgorithmException
{
Objects.requireNonNull(algorithm, "null algorithm name");
if (provider == null)

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

if (!CryptoAlgorithmConstraints.permits("MessageDigest", algorithm)) {
throw new NoSuchAlgorithmException(algorithm + " is disabled");
}

Object[] objs = Security.getImpl(algorithm, "MessageDigest", provider);
if (objs[0] instanceof MessageDigest) {
MessageDigest md = (MessageDigest)objs[0];
Expand Down
Loading