8318340: Improve RSA key implementations
Reviewed-by: rhalade, mschoene, valeriep, mullan
This commit is contained in:
parent
2885469c4b
commit
0203c7e612
@ -301,14 +301,6 @@ public final class RSAPrivateCrtKeyImpl
|
|||||||
return keyParams;
|
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
|
// 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,
|
// 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.
|
// e, d, p, q, pe, qe, and coeff, and return the parsed components.
|
||||||
|
@ -138,14 +138,6 @@ public final class RSAPrivateKeyImpl extends PKCS8Key implements RSAPrivateKey {
|
|||||||
return keyParams;
|
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.
|
* Restores the state of this object from the stream.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -76,10 +76,14 @@ abstract class CKey implements Key, Length {
|
|||||||
|
|
||||||
protected final String algorithm;
|
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.algorithm = algorithm;
|
||||||
this.handles = handles;
|
this.handles = handles;
|
||||||
this.keyLength = keyLength;
|
this.keyLength = keyLength;
|
||||||
|
this.isPublic = isPublic;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Native method to cleanup the key handle.
|
// Native method to cleanup the key handle.
|
||||||
@ -102,6 +106,18 @@ abstract class CKey implements Key, Length {
|
|||||||
return algorithm;
|
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 getContainerName(long hCryptProv);
|
||||||
|
|
||||||
protected static native String getKeyType(long hCryptKey);
|
protected static native String getKeyType(long hCryptKey);
|
||||||
|
@ -42,7 +42,7 @@ class CPrivateKey extends CKey implements PrivateKey {
|
|||||||
private static final long serialVersionUID = 8113152807912338063L;
|
private static final long serialVersionUID = 8113152807912338063L;
|
||||||
|
|
||||||
private CPrivateKey(String alg, NativeHandles handles, int keyLength) {
|
private CPrivateKey(String alg, NativeHandles handles, int keyLength) {
|
||||||
super(alg, handles, keyLength);
|
super(alg, handles, keyLength, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called by native code inside security.cpp
|
// Called by native code inside security.cpp
|
||||||
@ -65,16 +65,6 @@ class CPrivateKey extends CKey implements PrivateKey {
|
|||||||
return null;
|
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
|
// This class is not serializable
|
||||||
@java.io.Serial
|
@java.io.Serial
|
||||||
private void writeObject(java.io.ObjectOutputStream out)
|
private void writeObject(java.io.ObjectOutputStream out)
|
||||||
|
@ -114,9 +114,8 @@ public abstract class CPublicKey extends CKey implements PublicKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer(super.toString());
|
||||||
sb.append(algorithm).append("PublicKey [size=").append(keyLength)
|
sb.append("\n ECPoint: ").append(getW())
|
||||||
.append("]\n ECPoint: ").append(getW())
|
|
||||||
.append("\n params: ").append(getParams());
|
.append("\n params: ").append(getParams());
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
@ -135,16 +134,8 @@ public abstract class CPublicKey extends CKey implements PublicKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer(super.toString());
|
||||||
sb.append(algorithm).append("PublicKey [size=").append(keyLength)
|
sb.append("\n modulus: ").append(getModulus())
|
||||||
.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())
|
|
||||||
.append("\n public exponent: ").append(getPublicExponent());
|
.append("\n public exponent: ").append(getPublicExponent());
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
@ -215,7 +206,7 @@ public abstract class CPublicKey extends CKey implements PublicKey {
|
|||||||
|
|
||||||
protected CPublicKey(
|
protected CPublicKey(
|
||||||
String alg, NativeHandles handles, int keyLength) {
|
String alg, NativeHandles handles, int keyLength) {
|
||||||
super(alg, handles, keyLength);
|
super(alg, handles, keyLength, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -233,7 +233,17 @@ public abstract class PKCS11Test {
|
|||||||
throw new RuntimeException("Test root directory not found");
|
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;
|
return PKCS11_BASE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user