1 /* |
1 /* |
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
26 * @bug 4856966 |
26 * @bug 4856966 |
27 * @summary Test signing/verifying using all the signature algorithms |
27 * @summary Test signing/verifying using all the signature algorithms |
28 * @author Andreas Sterbenz |
28 * @author Andreas Sterbenz |
29 * @library .. |
29 * @library .. |
30 * @key randomness |
30 * @key randomness |
|
31 * @run main/othervm TestSignatures |
|
32 * @run main/othervm TestSignatures sm rsakeys.ks.policy |
31 */ |
33 */ |
32 |
34 |
33 import java.io.*; |
35 import java.io.File; |
34 import java.util.*; |
36 import java.io.FileInputStream; |
35 |
37 import java.io.InputStream; |
36 import java.security.*; |
38 import java.security.KeyFactory; |
37 import java.security.interfaces.*; |
39 import java.security.KeyStore; |
|
40 import java.security.PrivateKey; |
|
41 import java.security.Provider; |
|
42 import java.security.PublicKey; |
|
43 import java.security.Signature; |
|
44 import java.security.interfaces.RSAPublicKey; |
|
45 import java.util.Enumeration; |
|
46 import java.util.Random; |
38 |
47 |
39 public class TestSignatures extends PKCS11Test { |
48 public class TestSignatures extends PKCS11Test { |
40 |
|
41 private final static String BASE = System.getProperty("test.src", "."); |
|
42 |
49 |
43 private static final char[] password = "test12".toCharArray(); |
50 private static final char[] password = "test12".toCharArray(); |
44 |
51 |
45 private static Provider provider; |
52 private static Provider provider; |
46 |
53 |
47 private static byte[] data; |
54 private static byte[] data; |
48 |
55 |
49 static KeyStore getKeyStore() throws Exception { |
56 static KeyStore getKeyStore() throws Exception { |
50 InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks")); |
57 KeyStore ks; |
51 KeyStore ks = KeyStore.getInstance("JKS"); |
58 try (InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"))) { |
52 ks.load(in, password); |
59 ks = KeyStore.getInstance("JKS"); |
53 in.close(); |
60 ks.load(in, password); |
|
61 } |
54 return ks; |
62 return ks; |
55 } |
63 } |
56 |
64 |
57 private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception { |
65 private static void testSignature(String algorithm, PrivateKey privateKey, |
|
66 PublicKey publicKey) throws Exception { |
58 System.out.println("Testing " + algorithm + "..."); |
67 System.out.println("Testing " + algorithm + "..."); |
59 Signature s = Signature.getInstance(algorithm, provider); |
68 Signature s = Signature.getInstance(algorithm, provider); |
60 s.initSign(privateKey); |
69 s.initSign(privateKey); |
61 s.update(data); |
70 s.update(data); |
62 byte[] sig = s.sign(); |
71 byte[] sig = s.sign(); |
76 if (result == true) { |
85 if (result == true) { |
77 throw new Exception("Verification 3 succeeded"); |
86 throw new Exception("Verification 3 succeeded"); |
78 } |
87 } |
79 } |
88 } |
80 |
89 |
81 private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception { |
90 private static void test(PrivateKey privateKey, PublicKey publicKey) |
|
91 throws Exception { |
82 testSignature("MD2withRSA", privateKey, publicKey); |
92 testSignature("MD2withRSA", privateKey, publicKey); |
83 testSignature("MD5withRSA", privateKey, publicKey); |
93 testSignature("MD5withRSA", privateKey, publicKey); |
84 testSignature("SHA1withRSA", privateKey, publicKey); |
94 testSignature("SHA1withRSA", privateKey, publicKey); |
85 testSignature("SHA224withRSA", privateKey, publicKey); |
95 testSignature("SHA224withRSA", privateKey, publicKey); |
86 testSignature("SHA256withRSA", privateKey, publicKey); |
96 testSignature("SHA256withRSA", privateKey, publicKey); |
91 testSignature("SHA512withRSA", privateKey, publicKey); |
101 testSignature("SHA512withRSA", privateKey, publicKey); |
92 } |
102 } |
93 } |
103 } |
94 |
104 |
95 public static void main(String[] args) throws Exception { |
105 public static void main(String[] args) throws Exception { |
96 main(new TestSignatures()); |
106 main(new TestSignatures(), args); |
97 } |
107 } |
98 |
108 |
|
109 @Override |
99 public void main(Provider p) throws Exception { |
110 public void main(Provider p) throws Exception { |
100 |
111 |
101 /* |
112 /* |
102 * Use Solaris SPARC 11.2 or later to avoid an intermittent failure |
113 * Use Solaris SPARC 11.2 or later to avoid an intermittent failure |
103 * when running SunPKCS11-Solaris (8044554) |
114 * when running SunPKCS11-Solaris (8044554) |
104 */ |
115 */ |
105 if (p.getName().equals("SunPKCS11-Solaris") && |
116 if (p.getName().equals("SunPKCS11-Solaris") && |
106 System.getProperty("os.name").equals("SunOS") && |
117 props.getProperty("os.name").equals("SunOS") && |
107 System.getProperty("os.arch").equals("sparcv9") && |
118 props.getProperty("os.arch").equals("sparcv9") && |
108 System.getProperty("os.version").compareTo("5.11") <= 0 && |
119 props.getProperty("os.version").compareTo("5.11") <= 0 && |
109 getDistro().compareTo("11.2") < 0) { |
120 getDistro().compareTo("11.2") < 0) { |
110 |
121 |
111 System.out.println("SunPKCS11-Solaris provider requires " + |
122 System.out.println("SunPKCS11-Solaris provider requires " + |
112 "Solaris SPARC 11.2 or later, skipping"); |
123 "Solaris SPARC 11.2 or later, skipping"); |
113 return; |
124 return; |