Skip to content

Commit 54419d4

Browse files
Alexey Bakhtingnu-andrew
Alexey Bakhtin
authored andcommittedApr 7, 2024
8318340: Improve RSA key implementations
Reviewed-by: mbalao, andrew Backport-of: 62d9cec1d6b804a70381bfb8ac902b6bb649f8ae
1 parent 04ccdbf commit 54419d4

File tree

5 files changed

+23
-42
lines changed

5 files changed

+23
-42
lines changed
 

‎jdk/src/share/classes/sun/security/rsa/RSAPrivateCrtKeyImpl.java

-8
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,6 @@ public AlgorithmParameterSpec getParams() {
247247
return keyParams;
248248
}
249249

250-
// return a string representation of this key for debugging
251-
@Override
252-
public String toString() {
253-
return "SunRsaSign " + getAlgorithm() + " private CRT key, " + n.bitLength()
254-
+ " bits" + "\n params: " + keyParams + "\n modulus: " + n
255-
+ "\n private exponent: " + d;
256-
}
257-
258250
/**
259251
* Parse the key. Called by PKCS8Key.
260252
*/

‎jdk/src/share/classes/sun/security/rsa/RSAPrivateKeyImpl.java

-8
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,6 @@ public AlgorithmParameterSpec getParams() {
123123
return keyParams;
124124
}
125125

126-
// return a string representation of this key for debugging
127-
@Override
128-
public String toString() {
129-
return "Sun " + getAlgorithm() + " private key, " + n.bitLength()
130-
+ " bits" + "\n params: " + keyParams + "\n modulus: " + n
131-
+ "\n private exponent: " + d;
132-
}
133-
134126
/**
135127
* Restores the state of this object from the stream.
136128
* <p>

‎jdk/src/windows/classes/sun/security/mscapi/CKey.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ protected void finalize() throws Throwable {
7676

7777
protected final String algorithm;
7878

79-
protected CKey(String algorithm, NativeHandles handles, int keyLength) {
79+
private final boolean isPublic;
80+
81+
protected CKey(String algorithm, NativeHandles handles, int keyLength,
82+
boolean isPublic) {
8083
this.algorithm = algorithm;
8184
this.handles = handles;
8285
this.keyLength = keyLength;
86+
this.isPublic = isPublic;
8387
}
8488

8589
// Native method to cleanup the key handle.
@@ -102,6 +106,18 @@ public String getAlgorithm() {
102106
return algorithm;
103107
}
104108

109+
public String toString() {
110+
String typeStr;
111+
if (handles.hCryptKey != 0) {
112+
typeStr = getKeyType(handles.hCryptKey) + ", container=" +
113+
getContainerName(handles.hCryptProv);
114+
} else {
115+
typeStr = "CNG";
116+
}
117+
return algorithm + " " + (isPublic ? "PublicKey" : "PrivateKey") +
118+
" [size=" + keyLength + " bits, type=" + typeStr + "]";
119+
}
120+
105121
protected native static String getContainerName(long hCryptProv);
106122

107123
protected native static String getKeyType(long hCryptKey);

‎jdk/src/windows/classes/sun/security/mscapi/CPrivateKey.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class CPrivateKey extends CKey implements PrivateKey {
4141
private static final long serialVersionUID = 8113152807912338063L;
4242

4343
private CPrivateKey(String alg, NativeHandles handles, int keyLength) {
44-
super(alg, handles, keyLength);
44+
super(alg, handles, keyLength, false);
4545
}
4646

4747
// Called by native code inside security.cpp
@@ -64,16 +64,6 @@ public byte[] getEncoded() {
6464
return null;
6565
}
6666

67-
public String toString() {
68-
if (handles.hCryptKey != 0) {
69-
return algorithm + "PrivateKey [size=" + keyLength + " bits, type=" +
70-
getKeyType(handles.hCryptKey) + ", container=" +
71-
getContainerName(handles.hCryptProv) + "]";
72-
} else {
73-
return algorithm + "PrivateKey [size=" + keyLength + " bits, type=CNG]";
74-
}
75-
}
76-
7767
// This class is not serializable
7868
private void writeObject(java.io.ObjectOutputStream out)
7969
throws java.io.IOException {

‎jdk/src/windows/classes/sun/security/mscapi/CPublicKey.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ public ECParameterSpec getParams() {
110110
}
111111

112112
public String toString() {
113-
StringBuffer sb = new StringBuffer();
114-
sb.append(algorithm).append("PublicKey [size=").append(keyLength)
115-
.append("]\n ECPoint: ").append(getW())
113+
StringBuffer sb = new StringBuffer(super.toString());
114+
sb.append("\n ECPoint: ").append(getW())
116115
.append("\n params: ").append(getParams());
117116
return sb.toString();
118117
}
@@ -129,16 +128,8 @@ public static class CRSAPublicKey extends CPublicKey implements RSAPublicKey {
129128
}
130129

131130
public String toString() {
132-
StringBuffer sb = new StringBuffer();
133-
sb.append(algorithm).append("PublicKey [size=").append(keyLength)
134-
.append(" bits, type=");
135-
if (handles.hCryptKey != 0) {
136-
sb.append(getKeyType(handles.hCryptKey))
137-
.append(", container=").append(getContainerName(handles.hCryptProv));
138-
} else {
139-
sb.append("CNG");
140-
}
141-
sb.append("]\n modulus: ").append(getModulus())
131+
StringBuffer sb = new StringBuffer(super.toString());
132+
sb.append("\n modulus: ").append(getModulus())
142133
.append("\n public exponent: ").append(getPublicExponent());
143134
return sb.toString();
144135
}
@@ -209,7 +200,7 @@ public static CPublicKey of(
209200

210201
protected CPublicKey(
211202
String alg, NativeHandles handles, int keyLength) {
212-
super(alg, handles, keyLength);
203+
super(alg, handles, keyLength, true);
213204
}
214205

215206
@Override

0 commit comments

Comments
 (0)
Please sign in to comment.