1
1
/*
2
- * Copyright (c) 2015, 2018 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2015, 2021 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
29
29
import static javax .crypto .Cipher .PRIVATE_KEY ;
30
30
import static javax .crypto .Cipher .PUBLIC_KEY ;
31
31
32
+ import jdk .test .lib .Asserts ;
32
33
import jdk .test .lib .SigTestUtil ;
33
34
import static jdk .test .lib .SigTestUtil .SignatureType ;
34
35
35
36
/**
36
37
* @test
37
- * @bug 8044199 8146293
38
- * @summary Create a signature for RSA and get its signed data. re-initiate
38
+ * @bug 8044199 8146293 8163498
39
+ * @summary Ensure keys created from KeyFactory::getKeySpec and from constructors
40
+ * are equal.
41
+ * Create a signature for RSA and get its signed data. re-initiate
39
42
* the signature with the public key. The signature can be verified
40
43
* by acquired signed data.
41
- * @library /test/lib
44
+ * @library /test/lib ../tools/keytool/fakegen
42
45
* @build jdk.test.lib.SigTestUtil
46
+ * @build java.base/sun.security.rsa.RSAKeyPairGenerator
43
47
* @run main SignatureTest 512
44
48
* @run main SignatureTest 768
45
49
* @run main SignatureTest 1024
@@ -133,12 +137,24 @@ private static Key[] manipulateKey(int type, Key key)
133
137
} catch (InvalidKeySpecException expected ) {
134
138
}
135
139
140
+ RSAPublicKeySpec pubKeySpec1 = kf .getKeySpec (key , RSAPublicKeySpec .class );
141
+ RSAPublicKeySpec pubKeySpec2 = new RSAPublicKeySpec (
142
+ ((RSAPublicKey ) key ).getModulus (),
143
+ ((RSAPublicKey ) key ).getPublicExponent ());
144
+
145
+ Asserts .assertTrue (keySpecEquals (pubKeySpec1 , pubKeySpec2 ),
146
+ "Both RSAPublicKeySpec should be equal" );
147
+
148
+ X509EncodedKeySpec x509KeySpec1 = kf .getKeySpec (key , X509EncodedKeySpec .class );
149
+ X509EncodedKeySpec x509KeySpec2 = new X509EncodedKeySpec (key .getEncoded ());
150
+
151
+ Asserts .assertTrue (encodedKeySpecEquals (x509KeySpec1 , x509KeySpec2 ),
152
+ "Both X509EncodedKeySpec should be equal" );
153
+
136
154
return new Key []{
137
- kf .generatePublic (kf .getKeySpec (key , RSAPublicKeySpec .class )),
138
- kf .generatePublic (new X509EncodedKeySpec (key .getEncoded ())),
139
- kf .generatePublic (new RSAPublicKeySpec (
140
- ((RSAPublicKey ) key ).getModulus (),
141
- ((RSAPublicKey ) key ).getPublicExponent ()))
155
+ key ,
156
+ kf .generatePublic (pubKeySpec1 ),
157
+ kf .generatePublic (x509KeySpec1 )
142
158
};
143
159
case PRIVATE_KEY :
144
160
try {
@@ -147,13 +163,24 @@ private static Key[] manipulateKey(int type, Key key)
147
163
+ " not thrown" );
148
164
} catch (InvalidKeySpecException expected ) {
149
165
}
166
+ RSAPrivateKeySpec privKeySpec1 = kf .getKeySpec (key , RSAPrivateKeySpec .class );
167
+ RSAPrivateKeySpec privKeySpec2 = new RSAPrivateKeySpec (
168
+ ((RSAPrivateKey ) key ).getModulus (),
169
+ ((RSAPrivateKey ) key ).getPrivateExponent ());
170
+
171
+ Asserts .assertTrue (keySpecEquals (privKeySpec1 , privKeySpec2 ),
172
+ "Both RSAPrivateKeySpec should be equal" );
173
+
174
+ PKCS8EncodedKeySpec pkcsKeySpec1 = kf .getKeySpec (key , PKCS8EncodedKeySpec .class );
175
+ PKCS8EncodedKeySpec pkcsKeySpec2 = new PKCS8EncodedKeySpec (key .getEncoded ());
176
+
177
+ Asserts .assertTrue (encodedKeySpecEquals (pkcsKeySpec1 , pkcsKeySpec2 ),
178
+ "Both PKCS8EncodedKeySpec should be equal" );
179
+
150
180
return new Key []{
151
- kf .generatePrivate (kf .getKeySpec (key ,
152
- RSAPrivateKeySpec .class )),
153
- kf .generatePrivate (new PKCS8EncodedKeySpec (
154
- key .getEncoded ())),
155
- kf .generatePrivate (new RSAPrivateKeySpec (((RSAPrivateKey ) key ).getModulus (),
156
- ((RSAPrivateKey ) key ).getPrivateExponent ()))
181
+ key ,
182
+ kf .generatePrivate (privKeySpec1 ),
183
+ kf .generatePrivate (pkcsKeySpec1 )
157
184
};
158
185
}
159
186
throw new RuntimeException ("We shouldn't reach here" );
@@ -197,4 +224,22 @@ private static void checkSignature(byte[] data, PublicKey pub,
197
224
+ " signature" );
198
225
}
199
226
}
227
+
228
+ private static boolean keySpecEquals (RSAPublicKeySpec spec1 , RSAPublicKeySpec spec2 ) {
229
+ return spec1 .getModulus ().equals (spec2 .getModulus ())
230
+ && spec1 .getPublicExponent ().equals (spec2 .getPublicExponent ())
231
+ && Objects .equals (spec1 .getParams (), spec2 .getParams ());
232
+ }
233
+
234
+ private static boolean keySpecEquals (RSAPrivateKeySpec spec1 , RSAPrivateKeySpec spec2 ) {
235
+ return spec1 .getModulus ().equals (spec2 .getModulus ())
236
+ && spec1 .getPrivateExponent ().equals (spec2 .getPrivateExponent ())
237
+ && Objects .equals (spec1 .getParams (), spec2 .getParams ());
238
+ }
239
+
240
+ private static boolean encodedKeySpecEquals (EncodedKeySpec spec1 , EncodedKeySpec spec2 ) {
241
+ return Objects .equals (spec1 .getAlgorithm (), spec2 .getAlgorithm ())
242
+ && spec1 .getFormat ().equals (spec2 .getFormat ())
243
+ && Arrays .equals (spec1 .getEncoded (), spec2 .getEncoded ());
244
+ }
200
245
}
0 commit comments