2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 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
|
|
26 |
* @bug 4856966
|
|
27 |
* @summary Test signing/verifying using all the signature algorithms
|
|
28 |
* @author Andreas Sterbenz
|
|
29 |
* @library ..
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.util.*;
|
|
34 |
|
|
35 |
import java.security.*;
|
|
36 |
import java.security.interfaces.*;
|
|
37 |
|
|
38 |
public class TestSignatures extends PKCS11Test {
|
|
39 |
|
|
40 |
private final static String BASE = System.getProperty("test.src", ".");
|
|
41 |
|
|
42 |
private static final char[] password = "test12".toCharArray();
|
|
43 |
|
|
44 |
private static Provider provider;
|
|
45 |
|
|
46 |
private static byte[] data;
|
|
47 |
|
|
48 |
static KeyStore getKeyStore() throws Exception {
|
|
49 |
InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));
|
|
50 |
KeyStore ks = KeyStore.getInstance("JKS");
|
|
51 |
ks.load(in, password);
|
|
52 |
in.close();
|
|
53 |
return ks;
|
|
54 |
}
|
|
55 |
|
|
56 |
private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception {
|
|
57 |
System.out.println("Testing " + algorithm + "...");
|
|
58 |
Signature s = Signature.getInstance(algorithm, provider);
|
|
59 |
s.initSign(privateKey);
|
|
60 |
s.update(data);
|
|
61 |
byte[] sig = s.sign();
|
|
62 |
s.initVerify(publicKey);
|
|
63 |
s.update(data);
|
|
64 |
boolean result;
|
|
65 |
result = s.verify(sig);
|
|
66 |
if (result == false) {
|
|
67 |
throw new Exception("Verification 1 failed");
|
|
68 |
}
|
|
69 |
s.update(data);
|
|
70 |
result = s.verify(sig);
|
|
71 |
if (result == false) {
|
|
72 |
throw new Exception("Verification 2 failed");
|
|
73 |
}
|
|
74 |
result = s.verify(sig);
|
|
75 |
if (result == true) {
|
|
76 |
throw new Exception("Verification 3 succeeded");
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {
|
|
81 |
testSignature("MD2withRSA", privateKey, publicKey);
|
|
82 |
testSignature("MD5withRSA", privateKey, publicKey);
|
|
83 |
testSignature("SHA1withRSA", privateKey, publicKey);
|
|
84 |
testSignature("SHA256withRSA", privateKey, publicKey);
|
|
85 |
RSAPublicKey rsaKey = (RSAPublicKey)publicKey;
|
|
86 |
if (rsaKey.getModulus().bitLength() > 512) {
|
|
87 |
// for SHA384 and SHA512 the data is too long for 512 bit keys
|
|
88 |
testSignature("SHA384withRSA", privateKey, publicKey);
|
|
89 |
testSignature("SHA512withRSA", privateKey, publicKey);
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
public static void main(String[] args) throws Exception {
|
|
94 |
main(new TestSignatures());
|
|
95 |
}
|
|
96 |
|
|
97 |
public void main(Provider p) throws Exception {
|
|
98 |
long start = System.currentTimeMillis();
|
|
99 |
provider = p;
|
|
100 |
data = new byte[2048];
|
|
101 |
new Random().nextBytes(data);
|
|
102 |
KeyStore ks = getKeyStore();
|
|
103 |
KeyFactory kf = KeyFactory.getInstance("RSA", provider);
|
|
104 |
for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
|
|
105 |
String alias = (String)e.nextElement();
|
|
106 |
if (ks.isKeyEntry(alias)) {
|
|
107 |
System.out.println("* Key " + alias + "...");
|
|
108 |
PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
|
|
109 |
PublicKey publicKey = ks.getCertificate(alias).getPublicKey();
|
|
110 |
privateKey = (PrivateKey)kf.translateKey(privateKey);
|
|
111 |
publicKey = (PublicKey)kf.translateKey(publicKey);
|
|
112 |
test(privateKey, publicKey);
|
|
113 |
}
|
|
114 |
}
|
|
115 |
long stop = System.currentTimeMillis();
|
|
116 |
System.out.println("All tests passed (" + (stop - start) + " ms).");
|
|
117 |
}
|
|
118 |
}
|