8318340: Improve RSA key implementations

Reviewed-by: rhalade, mschoene, valeriep, mullan
This commit is contained in:
Weijun Wang 2023-10-27 00:29:20 +00:00 committed by Jaikiran Pai
parent 2885469c4b
commit 0203c7e612
6 changed files with 34 additions and 43 deletions

View File

@ -301,14 +301,6 @@ public final class RSAPrivateCrtKeyImpl
return keyParams;
}
// return a string representation of this key for debugging
@Override
public String toString() {
return "SunRsaSign " + type.keyAlgo + " private CRT key, "
+ n.bitLength() + " bits" + "\n params: " + keyParams
+ "\n modulus: " + n + "\n private exponent: " + d;
}
// utility method for parsing DER encoding of RSA private keys in PKCS#1
// format as defined in RFC 8017 Appendix A.1.2, i.e. SEQ of version, n,
// e, d, p, q, pe, qe, and coeff, and return the parsed components.

View File

@ -138,14 +138,6 @@ public final class RSAPrivateKeyImpl extends PKCS8Key implements RSAPrivateKey {
return keyParams;
}
// return a string representation of this key for debugging
@Override
public String toString() {
return "Sun " + type.keyAlgo + " private key, " + n.bitLength()
+ " bits" + "\n params: " + keyParams + "\n modulus: " + n
+ "\n private exponent: " + d;
}
/**
* Restores the state of this object from the stream.
* <p>

View File

@ -76,10 +76,14 @@ abstract class CKey implements Key, Length {
protected final String algorithm;
protected CKey(String algorithm, NativeHandles handles, int keyLength) {
private final boolean isPublic;
protected CKey(String algorithm, NativeHandles handles, int keyLength,
boolean isPublic) {
this.algorithm = algorithm;
this.handles = handles;
this.keyLength = keyLength;
this.isPublic = isPublic;
}
// Native method to cleanup the key handle.
@ -102,6 +106,18 @@ abstract class CKey implements Key, Length {
return algorithm;
}
public String toString() {
String typeStr;
if (handles.hCryptKey != 0) {
typeStr = getKeyType(handles.hCryptKey) + ", container=" +
getContainerName(handles.hCryptProv);
} else {
typeStr = "CNG";
}
return algorithm + " " + (isPublic ? "PublicKey" : "PrivateKey") +
" [size=" + keyLength + " bits, type=" + typeStr + "]";
}
protected static native String getContainerName(long hCryptProv);
protected static native String getKeyType(long hCryptKey);

View File

@ -42,7 +42,7 @@ class CPrivateKey extends CKey implements PrivateKey {
private static final long serialVersionUID = 8113152807912338063L;
private CPrivateKey(String alg, NativeHandles handles, int keyLength) {
super(alg, handles, keyLength);
super(alg, handles, keyLength, false);
}
// Called by native code inside security.cpp
@ -65,16 +65,6 @@ class CPrivateKey extends CKey implements PrivateKey {
return null;
}
public String toString() {
if (handles.hCryptKey != 0) {
return algorithm + "PrivateKey [size=" + keyLength + " bits, type=" +
getKeyType(handles.hCryptKey) + ", container=" +
getContainerName(handles.hCryptProv) + "]";
} else {
return algorithm + "PrivateKey [size=" + keyLength + " bits, type=CNG]";
}
}
// This class is not serializable
@java.io.Serial
private void writeObject(java.io.ObjectOutputStream out)

View File

@ -114,9 +114,8 @@ public abstract class CPublicKey extends CKey implements PublicKey {
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(algorithm).append("PublicKey [size=").append(keyLength)
.append("]\n ECPoint: ").append(getW())
StringBuffer sb = new StringBuffer(super.toString());
sb.append("\n ECPoint: ").append(getW())
.append("\n params: ").append(getParams());
return sb.toString();
}
@ -135,16 +134,8 @@ public abstract class CPublicKey extends CKey implements PublicKey {
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(algorithm).append("PublicKey [size=").append(keyLength)
.append(" bits, type=");
if (handles.hCryptKey != 0) {
sb.append(getKeyType(handles.hCryptKey))
.append(", container=").append(getContainerName(handles.hCryptProv));
} else {
sb.append("CNG");
}
sb.append("]\n modulus: ").append(getModulus())
StringBuffer sb = new StringBuffer(super.toString());
sb.append("\n modulus: ").append(getModulus())
.append("\n public exponent: ").append(getPublicExponent());
return sb.toString();
}
@ -215,7 +206,7 @@ public abstract class CPublicKey extends CKey implements PublicKey {
protected CPublicKey(
String alg, NativeHandles handles, int keyLength) {
super(alg, handles, keyLength);
super(alg, handles, keyLength, true);
}
@Override

View File

@ -233,7 +233,17 @@ public abstract class PKCS11Test {
throw new RuntimeException("Test root directory not found");
}
}
PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
File pkcs11 = new File(cwd, PKCS11_REL_PATH.replace('/', SEP));
if (!new File(pkcs11, "nss/p11-nss.txt").exists()) {
// this test might be in the closed
pkcs11 = new File(new File(cwd, "../../../open/test/jdk"),
PKCS11_REL_PATH.replace('/', SEP));
if (!new File(pkcs11, "nss/p11-nss.txt").exists()) {
throw new RuntimeException("Not a PKCS11 directory"
+ pkcs11.getAbsolutePath());
}
}
PKCS11_BASE = pkcs11.getAbsolutePath();
return PKCS11_BASE;
}