author | valeriep |
Mon, 21 May 2018 23:40:52 +0000 | |
changeset 50204 | 3195a713e24d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
26 |
* @bug 4853305 4963723 8146293 |
2 | 27 |
* @summary Test signing/verifying using all the signature algorithms |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
28 |
* @library /test/lib |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
29 |
* @build jdk.test.lib.SigTestUtil |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
30 |
* @run main TestSignatures |
2 | 31 |
* @author Andreas Sterbenz |
30046 | 32 |
* @key randomness |
2 | 33 |
*/ |
34 |
||
35 |
import java.io.*; |
|
36 |
import java.util.*; |
|
37 |
||
38 |
import java.security.*; |
|
39 |
import java.security.interfaces.*; |
|
40 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
41 |
import jdk.test.lib.SigTestUtil; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
42 |
import static jdk.test.lib.SigTestUtil.SignatureType; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
43 |
|
2 | 44 |
public class TestSignatures { |
45 |
||
46 |
private final static String BASE = System.getProperty("test.src", "."); |
|
47 |
||
48 |
private static final char[] password = "test12".toCharArray(); |
|
49 |
||
50 |
private static Provider provider; |
|
51 |
||
52 |
private static byte[] data; |
|
53 |
||
54 |
static KeyStore getKeyStore() throws Exception { |
|
55 |
InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks")); |
|
56 |
KeyStore ks = KeyStore.getInstance("JKS"); |
|
57 |
ks.load(in, password); |
|
58 |
in.close(); |
|
59 |
return ks; |
|
60 |
} |
|
61 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
62 |
private static void testSignature(String mdAlg, PrivateKey privateKey, |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
63 |
PublicKey publicKey) throws NoSuchAlgorithmException, |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
64 |
InvalidKeyException, SignatureException { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
65 |
System.out.println("Testing against " + mdAlg + "..."); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
66 |
String sigAlg = SigTestUtil.generateSigAlg(SignatureType.RSA, mdAlg); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
67 |
Signature s = Signature.getInstance(sigAlg, provider); |
2 | 68 |
s.initSign(privateKey); |
69 |
s.update(data); |
|
70 |
byte[] sig = s.sign(); |
|
71 |
s.initVerify(publicKey); |
|
72 |
s.update(data); |
|
73 |
boolean result; |
|
74 |
result = s.verify(sig); |
|
75 |
if (result == false) { |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
76 |
throw new RuntimeException("Verification 1 failed"); |
2 | 77 |
} |
78 |
s.update(data); |
|
79 |
result = s.verify(sig); |
|
80 |
if (result == false) { |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
81 |
throw new RuntimeException("Verification 2 failed"); |
2 | 82 |
} |
83 |
result = s.verify(sig); |
|
84 |
if (result == true) { |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
85 |
throw new RuntimeException("Verification 3 succeeded"); |
2 | 86 |
} |
87 |
} |
|
88 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
89 |
private static void test(PrivateKey privateKey, PublicKey publicKey) |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
90 |
throws Exception { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
91 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
92 |
int testSize = ((RSAPublicKey)publicKey).getModulus().bitLength(); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
93 |
System.out.println("modulus size = " + testSize); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
94 |
// work around a corner case where the key size is one bit short |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
95 |
if ((testSize & 0x07) != 0) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
96 |
testSize += (8 - (testSize & 0x07)); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
97 |
System.out.println("adjusted modulus size = " + testSize); |
2 | 98 |
} |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
99 |
Iterable<String> sign_alg_pkcs15 = |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
100 |
SigTestUtil.getDigestAlgorithms(SignatureType.RSA, testSize); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
101 |
sign_alg_pkcs15.forEach(testAlg -> { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
102 |
try { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
103 |
testSignature(testAlg, privateKey, publicKey); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
104 |
} catch (NoSuchAlgorithmException | InvalidKeyException | |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
105 |
SignatureException ex) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
106 |
throw new RuntimeException(ex); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
107 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
108 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
109 |
); |
2 | 110 |
} |
111 |
||
112 |
public static void main(String[] args) throws Exception { |
|
113 |
long start = System.currentTimeMillis(); |
|
114 |
provider = Security.getProvider("SunRsaSign"); |
|
115 |
data = new byte[2048]; |
|
116 |
new Random().nextBytes(data); |
|
117 |
KeyStore ks = getKeyStore(); |
|
118 |
KeyFactory kf = KeyFactory.getInstance("RSA", provider); |
|
119 |
for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) { |
|
120 |
String alias = (String)e.nextElement(); |
|
121 |
if (ks.isKeyEntry(alias)) { |
|
122 |
System.out.println("* Key " + alias + "..."); |
|
123 |
PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password); |
|
124 |
PublicKey publicKey = ks.getCertificate(alias).getPublicKey(); |
|
125 |
privateKey = (PrivateKey)kf.translateKey(privateKey); |
|
126 |
publicKey = (PublicKey)kf.translateKey(publicKey); |
|
127 |
test(privateKey, publicKey); |
|
128 |
} |
|
129 |
} |
|
130 |
long stop = System.currentTimeMillis(); |
|
131 |
System.out.println("All tests passed (" + (stop - start) + " ms)."); |
|
132 |
} |
|
133 |
} |