Skip to content

Commit 6f498af

Browse files
committed
Update to address review comments from Weijun
1 parent b48afdd commit 6f498af

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

src/java.base/share/classes/sun/security/rsa/RSAKeyFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@
4343
* between the following:
4444
*
4545
* For public keys:
46-
* . PublicKey with an X.509 encoding
46+
* . RSA PublicKey with an X.509 encoding
4747
* . RSA PublicKey with an PKCS#1 encoding
4848
* . RSAPublicKey
4949
* . RSAPublicKeySpec
5050
* . X509EncodedKeySpec
5151
*
5252
* For private keys:
53-
* . PrivateKey with a PKCS#8 encoding
53+
* . RSA PrivateKey with a PKCS#8 encoding
5454
* . RSA PrivateKey with a PKCS#1 encoding
5555
* . RSAPrivateKey
5656
* . RSAPrivateCrtKey
@@ -381,7 +381,8 @@ protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
381381
try {
382382
// convert key to one of our keys
383383
// this also verifies that the key is a valid RSA key and ensures
384-
// that the encoding is X.509/PKCS#8 for public/private keys
384+
// that the encoding is X.509/PKCS#8 or PKCS#1 for public/private
385+
// keys
385386
key = engineTranslateKey(key);
386387
} catch (InvalidKeyException e) {
387388
throw new InvalidKeySpecException(e);

src/java.base/share/classes/sun/security/rsa/RSAPrivateCrtKeyImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public static RSAPrivateKey newKey(KeyType type, String format,
9898
(key.getCrtCoefficient().signum() == 0)) {
9999
return new RSAPrivateKeyImpl(key.type, key.keyParams,
100100
key.getModulus(), key.getPrivateExponent());
101-
} else return key;
101+
} else {
102+
return key;
103+
}
102104
case "PKCS#1":
103105
try {
104106
BigInteger[] comps = parseASN1(encoded);

test/jdk/sun/security/pkcs11/rsa/TestKeyFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,9 @@ private static void testKey(Key key1, Key key2) throws Exception {
153153
}
154154
}
155155
// only compare encodings if keys are of the same format
156-
if (key1.getFormat().equals(key2.getFormat())) {
157-
if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
158-
throw new Exception("Encodings not equal");
159-
}
156+
if (key1.getFormat().equals(key2.getFormat()) &&
157+
!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
158+
throw new Exception("Encodings not equal");
160159
}
161160
}
162161

test/jdk/sun/security/rsa/TestKeyFactory.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ public class TestKeyFactory {
8181
"QKBW1TS0/2iB9zNkFMj5/+h7l2oqTT7sSQIDAQAB";
8282

8383

84-
private static final PrivateKey CUSTOM_PRIV;
85-
private static final PublicKey CUSTOM_PUB;
84+
private static final PrivateKey P1_PRIV;
85+
private static final PublicKey P1_PUB;
8686

8787
static {
8888
byte[] encodedPriv = Base64.getDecoder().decode(PKCS1_PRIV_STR);
89-
CUSTOM_PRIV = new PrivateKey() {
89+
P1_PRIV = new PrivateKey() {
9090
@Override
9191
public String getAlgorithm() {
9292
return "RSA";
@@ -102,7 +102,7 @@ public byte[] getEncoded() {
102102
}
103103
};
104104
byte[] encodedPub = Base64.getDecoder().decode(PKCS1_PUB_STR);
105-
CUSTOM_PUB = new PublicKey() {
105+
P1_PUB = new PublicKey() {
106106
@Override
107107
public String getAlgorithm() {
108108
return "RSA";
@@ -145,31 +145,30 @@ private static void testKey(Key key1, Key key2) throws Exception {
145145
}
146146
}
147147
// skip equals check when key1 is custom key
148-
if (key1 != CUSTOM_PRIV && key1 != CUSTOM_PUB) {
148+
if (key1 != P1_PRIV && key1 != P1_PUB) {
149149
if (!key1.equals(key2)) {
150150
throw new Exception("Keys not equal");
151151
}
152152
}
153153

154154
// only compare encodings if keys are of the same format
155-
if (key1.getFormat().equals(key2.getFormat())) {
156-
if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
157-
throw new Exception("Encodings not equal");
158-
}
155+
if (key1.getFormat().equals(key2.getFormat()) &&
156+
!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
157+
throw new Exception("Encodings not equal");
159158
}
160159
}
161160

162161
private static void testPublic(KeyFactory kf, PublicKey key)
163162
throws Exception {
164-
System.out.println("Testing " + (key == CUSTOM_PUB? "PKCS#1" : "") +
163+
System.out.println("Testing " + (key == P1_PUB? "PKCS#1" : "") +
165164
" public key...");
166165

167166
PublicKey key2 = (PublicKey)kf.translateKey(key);
168167
KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);
169168
PublicKey key3 = kf.generatePublic(rsaSpec);
170169
KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
171170
PublicKey key4 = kf.generatePublic(x509Spec);
172-
if (key != CUSTOM_PUB) {
171+
if (key != P1_PUB) {
173172
testKey(key, key);
174173
}
175174
testKey(key, key2);
@@ -185,14 +184,14 @@ private static void testPublic(KeyFactory kf, PublicKey key)
185184

186185
private static void testPrivate(KeyFactory kf, PrivateKey key)
187186
throws Exception {
188-
System.out.println("Testing " + (key == CUSTOM_PRIV? "PKCS#1" : "") +
187+
System.out.println("Testing " + (key == P1_PRIV? "PKCS#1" : "") +
189188
" private key...");
190189
PrivateKey key2 = (PrivateKey)kf.translateKey(key);
191190
KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);
192191
PrivateKey key3 = kf.generatePrivate(rsaSpec);
193192
KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
194193
PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
195-
if (key != CUSTOM_PRIV) {
194+
if (key != P1_PRIV) {
196195
testKey(key, key);
197196
}
198197
testKey(key, key2);
@@ -240,8 +239,8 @@ public static void main(String[] args) throws Exception {
240239
}
241240
}
242241
// repeat the test w/ PKCS#1 RSA Private Key
243-
test(kf, CUSTOM_PRIV);
244-
test(kf, CUSTOM_PUB);
242+
test(kf, P1_PRIV);
243+
test(kf, P1_PUB);
245244

246245
long stop = System.currentTimeMillis();
247246
System.out.println("All tests passed (" + (stop - start) + " ms).");

0 commit comments

Comments
 (0)