author | valeriep |
Fri, 26 Jun 2015 21:34:34 +0000 | |
changeset 31270 | e6470b24700d |
parent 28403 | 02b91a87c4dd |
child 32138 | 23830562d3d1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31270
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
2 |
* Copyright (c) 2003, 2015, 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 |
// common infrastructure for SunPKCS11 tests |
|
26 |
||
27 |
import java.io.*; |
|
28 |
import java.util.*; |
|
29 |
||
30 |
import java.security.*; |
|
21977
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
31 |
import java.security.spec.ECGenParameterSpec; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
32 |
import java.security.spec.ECParameterSpec; |
2 | 33 |
|
34 |
public abstract class PKCS11Test { |
|
35 |
||
36 |
// directory of the test source |
|
37 |
static final String BASE = System.getProperty("test.src", "."); |
|
38 |
||
39 |
static final char SEP = File.separatorChar; |
|
40 |
||
41 |
private final static String REL_CLOSED = "../../../../closed/sun/security/pkcs11".replace('/', SEP); |
|
42 |
||
43 |
// directory corresponding to BASE in the /closed hierarchy |
|
44 |
static final String CLOSED_BASE; |
|
45 |
||
46 |
static { |
|
47 |
// hack |
|
48 |
String absBase = new File(BASE).getAbsolutePath(); |
|
49 |
int k = absBase.indexOf(SEP + "test" + SEP + "sun" + SEP); |
|
50 |
if (k < 0) k = 0; |
|
51 |
String p1 = absBase.substring(0, k + 6); |
|
52 |
String p2 = absBase.substring(k + 5); |
|
53 |
CLOSED_BASE = p1 + "closed" + p2; |
|
54 |
} |
|
55 |
||
56 |
static String NSPR_PREFIX = ""; |
|
57 |
||
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
58 |
// NSS version info |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
59 |
public static enum ECCState { None, Basic, Extended }; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
60 |
static double nss_version = -1; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
61 |
static ECCState nss_ecc_status = ECCState.Extended; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
62 |
|
19066 | 63 |
// The NSS library we need to search for in getNSSLibDir() |
64 |
// Default is "libsoftokn3.so", listed as "softokn3" |
|
65 |
// The other is "libnss3.so", listed as "nss3". |
|
66 |
static String nss_library = "softokn3"; |
|
67 |
||
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
68 |
// NSS versions of each library. It is simplier to keep nss_version |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
69 |
// for quick checking for generic testing than many if-else statements. |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
70 |
static double softoken3_version = -1; |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
71 |
static double nss3_version = -1; |
31270
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
72 |
static Provider pkcs11; |
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
73 |
|
31270
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
74 |
// Goes through ServiceLoader instead of Provider.getInstance() since it |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
75 |
// works on all platforms |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
76 |
static { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
77 |
ServiceLoader sl = ServiceLoader.load(java.security.Provider.class); |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
78 |
Iterator<Provider> iter = sl.iterator(); |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
79 |
Provider p = null; |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
80 |
boolean found = false; |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
81 |
while (iter.hasNext()) { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
82 |
try { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
83 |
p = iter.next(); |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
84 |
if (p.getName().equals("SunPKCS11")) { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
85 |
found = true; |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
86 |
break; |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
87 |
}; |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
88 |
} catch (Exception e) { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
89 |
// ignore and move on to the next one |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
90 |
} |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
91 |
} |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
92 |
// Nothing found through ServiceLoader; fall back to reflection |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
93 |
if (!found) { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
94 |
try { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
95 |
Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11"); |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
96 |
p = (Provider) clazz.newInstance(); |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
97 |
} catch (Exception ex) { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
98 |
ex.printStackTrace(); |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
99 |
} |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
100 |
} |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
101 |
pkcs11 = p; |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
102 |
} |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
103 |
|
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
104 |
// Return a SunPKCS11 provider configured with the specified config file |
2 | 105 |
static Provider getSunPKCS11(String config) throws Exception { |
31270
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
106 |
if (pkcs11 == null) { |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
107 |
throw new NoSuchProviderException("No PKCS11 provider available"); |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
108 |
} |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
109 |
return pkcs11.configure(config); |
2 | 110 |
} |
111 |
||
112 |
public abstract void main(Provider p) throws Exception; |
|
113 |
||
114 |
private void premain(Provider p) throws Exception { |
|
115 |
long start = System.currentTimeMillis(); |
|
116 |
System.out.println("Running test with provider " + p.getName() + "..."); |
|
117 |
main(p); |
|
118 |
long stop = System.currentTimeMillis(); |
|
31270
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
119 |
System.out.println("Completed test with provider " + p.getName() + |
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
120 |
" (" + (stop - start) + " ms)."); |
2 | 121 |
} |
122 |
||
123 |
public static void main(PKCS11Test test) throws Exception { |
|
10328 | 124 |
Provider[] oldProviders = Security.getProviders(); |
125 |
try { |
|
126 |
System.out.println("Beginning test run " + test.getClass().getName() + "..."); |
|
127 |
testDefault(test); |
|
128 |
testNSS(test); |
|
129 |
testDeimos(test); |
|
130 |
} finally { |
|
19066 | 131 |
// NOTE: Do not place a 'return' in any finally block |
132 |
// as it will suppress exceptions and hide test failures. |
|
10328 | 133 |
Provider[] newProviders = Security.getProviders(); |
19066 | 134 |
boolean found = true; |
10328 | 135 |
// Do not restore providers if nothing changed. This is especailly |
136 |
// useful for ./Provider/Login.sh, where a SecurityManager exists. |
|
137 |
if (oldProviders.length == newProviders.length) { |
|
19066 | 138 |
found = false; |
10328 | 139 |
for (int i = 0; i<oldProviders.length; i++) { |
140 |
if (oldProviders[i] != newProviders[i]) { |
|
141 |
found = true; |
|
142 |
break; |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
19066 | 146 |
if (found) { |
147 |
for (Provider p: newProviders) { |
|
148 |
Security.removeProvider(p.getName()); |
|
149 |
} |
|
150 |
for (Provider p: oldProviders) { |
|
151 |
Security.addProvider(p); |
|
152 |
} |
|
10328 | 153 |
} |
154 |
} |
|
2 | 155 |
} |
156 |
||
157 |
public static void testDeimos(PKCS11Test test) throws Exception { |
|
158 |
if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false || |
|
159 |
"true".equals(System.getProperty("NO_DEIMOS"))) { |
|
160 |
return; |
|
161 |
} |
|
162 |
String base = getBase(); |
|
163 |
String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt"; |
|
164 |
Provider p = getSunPKCS11(p11config); |
|
165 |
test.premain(p); |
|
166 |
} |
|
167 |
||
168 |
public static void testDefault(PKCS11Test test) throws Exception { |
|
169 |
// run test for default configured PKCS11 providers (if any) |
|
170 |
||
171 |
if ("true".equals(System.getProperty("NO_DEFAULT"))) { |
|
172 |
return; |
|
173 |
} |
|
174 |
||
175 |
Provider[] providers = Security.getProviders(); |
|
176 |
for (int i = 0; i < providers.length; i++) { |
|
177 |
Provider p = providers[i]; |
|
178 |
if (p.getName().startsWith("SunPKCS11-")) { |
|
179 |
test.premain(p); |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
private static String PKCS11_BASE; |
|
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
185 |
static { |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
186 |
try { |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
187 |
PKCS11_BASE = getBase(); |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
188 |
} catch (Exception e) { |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
189 |
// ignore |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
190 |
} |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
191 |
} |
2 | 192 |
|
193 |
private final static String PKCS11_REL_PATH = "sun/security/pkcs11"; |
|
194 |
||
195 |
public static String getBase() throws Exception { |
|
196 |
if (PKCS11_BASE != null) { |
|
197 |
return PKCS11_BASE; |
|
198 |
} |
|
199 |
File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile(); |
|
200 |
while (true) { |
|
201 |
File file = new File(cwd, "TEST.ROOT"); |
|
202 |
if (file.isFile()) { |
|
203 |
break; |
|
204 |
} |
|
205 |
cwd = cwd.getParentFile(); |
|
206 |
if (cwd == null) { |
|
207 |
throw new Exception("Test root directory not found"); |
|
208 |
} |
|
209 |
} |
|
210 |
PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath(); |
|
211 |
return PKCS11_BASE; |
|
212 |
} |
|
213 |
||
214 |
public static String getNSSLibDir() throws Exception { |
|
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
215 |
return getNSSLibDir(nss_library); |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
216 |
} |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
217 |
|
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
218 |
static String getNSSLibDir(String library) throws Exception { |
2 | 219 |
Properties props = System.getProperties(); |
220 |
String osName = props.getProperty("os.name"); |
|
221 |
if (osName.startsWith("Win")) { |
|
222 |
osName = "Windows"; |
|
223 |
NSPR_PREFIX = "lib"; |
|
26958 | 224 |
} else if (osName.equals("Mac OS X")) { |
225 |
osName = "MacOSX"; |
|
2 | 226 |
} |
227 |
String osid = osName + "-" |
|
228 |
+ props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model"); |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
229 |
String[] nssLibDirs = osMap.get(osid); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
230 |
if (nssLibDirs == null) { |
2 | 231 |
System.out.println("Unsupported OS, skipping: " + osid); |
232 |
return null; |
|
233 |
} |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
234 |
if (nssLibDirs.length == 0) { |
2 | 235 |
System.out.println("NSS not supported on this platform, skipping test"); |
236 |
return null; |
|
237 |
} |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
238 |
String nssLibDir = null; |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
239 |
for (String dir : nssLibDirs) { |
19066 | 240 |
if (new File(dir).exists() && |
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
241 |
new File(dir + System.mapLibraryName(library)).exists()) { |
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
242 |
nssLibDir = dir; |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
243 |
System.setProperty("pkcs11test.nss.libdir", nssLibDir); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
244 |
break; |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
245 |
} |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
246 |
} |
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
247 |
return nssLibDir; |
2 | 248 |
} |
249 |
||
10328 | 250 |
protected static void safeReload(String lib) throws Exception { |
251 |
try { |
|
252 |
System.load(lib); |
|
253 |
} catch (UnsatisfiedLinkError e) { |
|
254 |
if (e.getMessage().contains("already loaded")) { |
|
255 |
return; |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
||
2 | 260 |
static boolean loadNSPR(String libdir) throws Exception { |
261 |
// load NSS softoken dependencies in advance to avoid resolver issues |
|
10328 | 262 |
safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4")); |
263 |
safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4")); |
|
264 |
safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4")); |
|
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
265 |
safeReload(libdir + System.mapLibraryName("sqlite3")); |
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
266 |
safeReload(libdir + System.mapLibraryName("nssutil3")); |
2 | 267 |
return true; |
268 |
} |
|
269 |
||
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
270 |
// Check the provider being used is NSS |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
271 |
public static boolean isNSS(Provider p) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
272 |
return p.getName().toUpperCase().equals("SUNPKCS11-NSS"); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
273 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
274 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
275 |
static double getNSSVersion() { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
276 |
if (nss_version == -1) |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
277 |
getNSSInfo(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
278 |
return nss_version; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
279 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
280 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
281 |
static ECCState getNSSECC() { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
282 |
if (nss_version == -1) |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
283 |
getNSSInfo(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
284 |
return nss_ecc_status; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
285 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
286 |
|
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
287 |
public static double getLibsoftokn3Version() { |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
288 |
if (softoken3_version == -1) |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
289 |
return getNSSInfo("softokn3"); |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
290 |
return softoken3_version; |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
291 |
} |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
292 |
|
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
293 |
public static double getLibnss3Version() { |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
294 |
if (nss3_version == -1) |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
295 |
return getNSSInfo("nss3"); |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
296 |
return nss3_version; |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
297 |
} |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
298 |
|
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
299 |
/* Read the library to find out the verison */ |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
300 |
static void getNSSInfo() { |
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
301 |
getNSSInfo(nss_library); |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
302 |
} |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
303 |
|
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
304 |
static double getNSSInfo(String library) { |
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
305 |
String nssHeader = "$Header: NSS"; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
306 |
boolean found = false; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
307 |
String s = null; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
308 |
int i = 0; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
309 |
String libfile = ""; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
310 |
|
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
311 |
if (library.compareTo("softokn3") == 0 && softoken3_version > -1) |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
312 |
return softoken3_version; |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
313 |
if (library.compareTo("nss3") == 0 && nss3_version > -1) |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
314 |
return nss3_version; |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
315 |
|
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
316 |
try { |
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
317 |
libfile = getNSSLibDir() + System.mapLibraryName(library); |
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
318 |
FileInputStream is = new FileInputStream(libfile); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
319 |
byte[] data = new byte[1000]; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
320 |
int read = 0; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
321 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
322 |
while (is.available() > 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
323 |
if (read == 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
324 |
read = is.read(data, 0, 1000); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
325 |
} else { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
326 |
// Prepend last 100 bytes in case the header was split |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
327 |
// between the reads. |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
328 |
System.arraycopy(data, 900, data, 0, 100); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
329 |
read = 100 + is.read(data, 100, 900); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
330 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
331 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
332 |
s = new String(data, 0, read); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
333 |
if ((i = s.indexOf(nssHeader)) > 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
334 |
found = true; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
335 |
// If the nssHeader is before 920 we can break, otherwise |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
336 |
// we may not have the whole header so do another read. If |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
337 |
// no bytes are in the stream, that is ok, found is true. |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
338 |
if (i < 920) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
339 |
break; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
340 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
341 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
342 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
343 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
344 |
is.close(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
345 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
346 |
} catch (Exception e) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
347 |
e.printStackTrace(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
348 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
349 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
350 |
if (!found) { |
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
351 |
System.out.println("lib" + library + |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
352 |
" version not found, set to 0.0: " + libfile); |
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
353 |
nss_version = 0.0; |
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
354 |
return nss_version; |
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
355 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
356 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
357 |
// the index after whitespace after nssHeader |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
358 |
int afterheader = s.indexOf("NSS", i) + 4; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
359 |
String version = s.substring(afterheader, s.indexOf(' ', afterheader)); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
360 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
361 |
// If a "dot dot" release, strip the extra dots for double parsing |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
362 |
String[] dot = version.split("\\."); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
363 |
if (dot.length > 2) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
364 |
version = dot[0]+"."+dot[1]; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
365 |
for (int j = 2; dot.length > j; j++) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
366 |
version += dot[j]; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
367 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
368 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
369 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
370 |
// Convert to double for easier version value checking |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
371 |
try { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
372 |
nss_version = Double.parseDouble(version); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
373 |
} catch (NumberFormatException e) { |
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
374 |
System.out.println("Failed to parse lib" + library + |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
375 |
" version. Set to 0.0"); |
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
376 |
e.printStackTrace(); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
377 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
378 |
|
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
379 |
System.out.print("lib" + library + " version = "+version+". "); |
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
380 |
|
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
381 |
// Check for ECC |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
382 |
if (s.indexOf("Basic") > 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
383 |
nss_ecc_status = ECCState.Basic; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
384 |
System.out.println("ECC Basic."); |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
385 |
} else if (s.indexOf("Extended") > 0) { |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
386 |
nss_ecc_status = ECCState.Extended; |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
387 |
System.out.println("ECC Extended."); |
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
388 |
} else { |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
389 |
System.out.println("ECC None."); |
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
390 |
} |
24863
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
391 |
|
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
392 |
if (library.compareTo("softokn3") == 0) { |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
393 |
softoken3_version = nss_version; |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
394 |
} else if (library.compareTo("nss3") == 0) { |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
395 |
nss3_version = nss_version; |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
396 |
} |
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
397 |
|
bf6df3caafe4
8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents:
24629
diff
changeset
|
398 |
return nss_version; |
19067
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
399 |
} |
5271291b7121
8020424: The NSS version should be detected before running crypto tests
ascarpino
parents:
19066
diff
changeset
|
400 |
|
19066 | 401 |
// Used to set the nss_library file to search for libsoftokn3.so |
402 |
public static void useNSS() { |
|
403 |
nss_library = "nss3"; |
|
404 |
} |
|
405 |
||
2 | 406 |
public static void testNSS(PKCS11Test test) throws Exception { |
407 |
String libdir = getNSSLibDir(); |
|
408 |
if (libdir == null) { |
|
409 |
return; |
|
410 |
} |
|
411 |
String base = getBase(); |
|
412 |
||
413 |
if (loadNSPR(libdir) == false) { |
|
414 |
return; |
|
415 |
} |
|
416 |
||
19066 | 417 |
String libfile = libdir + System.mapLibraryName(nss_library); |
2 | 418 |
|
419 |
String customDBdir = System.getProperty("CUSTOM_DB_DIR"); |
|
420 |
String dbdir = (customDBdir != null) ? |
|
421 |
customDBdir : |
|
422 |
base + SEP + "nss" + SEP + "db"; |
|
423 |
// NSS always wants forward slashes for the config path |
|
424 |
dbdir = dbdir.replace('\\', '/'); |
|
425 |
||
426 |
String customConfig = System.getProperty("CUSTOM_P11_CONFIG"); |
|
427 |
String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt"); |
|
428 |
String p11config = (customConfig != null) ? |
|
429 |
customConfig : |
|
430 |
base + SEP + "nss" + SEP + customConfigName; |
|
431 |
||
432 |
System.setProperty("pkcs11test.nss.lib", libfile); |
|
433 |
System.setProperty("pkcs11test.nss.db", dbdir); |
|
434 |
Provider p = getSunPKCS11(p11config); |
|
435 |
test.premain(p); |
|
436 |
} |
|
437 |
||
21977
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
438 |
// Generate a vector of supported elliptic curves of a given provider |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
439 |
static Vector<ECParameterSpec> getKnownCurves(Provider p) throws Exception { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
440 |
int index; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
441 |
int begin; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
442 |
int end; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
443 |
String curve; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
444 |
KeyPair kp = null; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
445 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
446 |
Vector<ECParameterSpec> results = new Vector<ECParameterSpec>(); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
447 |
// Get Curves to test from SunEC. |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
448 |
String kcProp = Security.getProvider("SunEC"). |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
449 |
getProperty("AlgorithmParameters.EC SupportedCurves"); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
450 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
451 |
if (kcProp == null) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
452 |
throw new RuntimeException( |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
453 |
"\"AlgorithmParameters.EC SupportedCurves property\" not found"); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
454 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
455 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
456 |
System.out.println("Finding supported curves using list from SunEC\n"); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
457 |
index = 0; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
458 |
for (;;) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
459 |
// Each set of curve names is enclosed with brackets. |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
460 |
begin = kcProp.indexOf('[', index); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
461 |
end = kcProp.indexOf(']', index); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
462 |
if (begin == -1 || end == -1) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
463 |
break; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
464 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
465 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
466 |
/* |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
467 |
* Each name is separated by a comma. |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
468 |
* Just get the first name in the set. |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
469 |
*/ |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
470 |
index = end + 1; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
471 |
begin++; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
472 |
end = kcProp.indexOf(',', begin); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
473 |
if (end == -1) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
474 |
// Only one name in the set. |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
475 |
end = index -1; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
476 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
477 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
478 |
curve = kcProp.substring(begin, end); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
479 |
ECParameterSpec e = getECParameterSpec(p, curve); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
480 |
System.out.print("\t "+ curve + ": "); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
481 |
try { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
482 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
483 |
kpg.initialize(e); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
484 |
kp = kpg.generateKeyPair(); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
485 |
results.add(e); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
486 |
System.out.println("Supported"); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
487 |
} catch (ProviderException ex) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
488 |
System.out.println("Unsupported: PKCS11: " + |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
489 |
ex.getCause().getMessage()); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
490 |
} catch (InvalidAlgorithmParameterException ex) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
491 |
System.out.println("Unsupported: Key Length: " + |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
492 |
ex.getMessage()); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
493 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
494 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
495 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
496 |
if (results.size() == 0) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
497 |
throw new RuntimeException("No supported EC curves found"); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
498 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
499 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
500 |
return results; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
501 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
502 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
503 |
private static ECParameterSpec getECParameterSpec(Provider p, String name) |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
504 |
throws Exception { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
505 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
506 |
AlgorithmParameters parameters = |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
507 |
AlgorithmParameters.getInstance("EC", p); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
508 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
509 |
parameters.init(new ECGenParameterSpec(name)); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
510 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
511 |
return parameters.getParameterSpec(ECParameterSpec.class); |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
512 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
513 |
|
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
514 |
// Check support for a curve with a provided Vector of EC support |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
515 |
boolean checkSupport(Vector<ECParameterSpec> supportedEC, |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
516 |
ECParameterSpec curve) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
517 |
boolean found = false; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
518 |
for (ECParameterSpec ec: supportedEC) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
519 |
if (ec.equals(curve)) { |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
520 |
return true; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
521 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
522 |
} |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
523 |
return false; |
17f538f05b73
8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents:
19067
diff
changeset
|
524 |
} |
2 | 525 |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
526 |
private static final Map<String,String[]> osMap; |
2 | 527 |
|
12294
f313586fc3cc
7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents:
12042
diff
changeset
|
528 |
// Location of the NSS libraries on each supported platform |
2 | 529 |
static { |
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
530 |
osMap = new HashMap<String,String[]>(); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
531 |
osMap.put("SunOS-sparc-32", new String[]{"/usr/lib/mps/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
532 |
osMap.put("SunOS-sparcv9-64", new String[]{"/usr/lib/mps/64/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
533 |
osMap.put("SunOS-x86-32", new String[]{"/usr/lib/mps/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
534 |
osMap.put("SunOS-amd64-64", new String[]{"/usr/lib/mps/64/"}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
535 |
osMap.put("Linux-i386-32", new String[]{ |
24629
97bddac495b7
8044038: Security tests fail on 32 bit linux platform
vinnie
parents:
21977
diff
changeset
|
536 |
"/usr/lib/i386-linux-gnu/", "/usr/lib32/", "/usr/lib/"}); |
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
537 |
osMap.put("Linux-amd64-64", new String[]{ |
19066 | 538 |
"/usr/lib/x86_64-linux-gnu/", "/usr/lib/x86_64-linux-gnu/nss/", |
539 |
"/usr/lib64/"}); |
|
13565
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
540 |
osMap.put("Windows-x86-32", new String[]{ |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
541 |
PKCS11_BASE + "/nss/lib/windows-i586/".replace('/', SEP)}); |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
542 |
osMap.put("Windows-amd64-64", new String[]{ |
6298d600104f
7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents:
13250
diff
changeset
|
543 |
PKCS11_BASE + "/nss/lib/windows-amd64/".replace('/', SEP)}); |
26958 | 544 |
osMap.put("MacOSX-x86_64-64", new String[]{ |
545 |
PKCS11_BASE + "/nss/lib/macosx-x86_64/"}); |
|
2 | 546 |
} |
547 |
||
548 |
private final static char[] hexDigits = "0123456789abcdef".toCharArray(); |
|
549 |
||
550 |
public static String toString(byte[] b) { |
|
551 |
if (b == null) { |
|
552 |
return "(null)"; |
|
553 |
} |
|
554 |
StringBuffer sb = new StringBuffer(b.length * 3); |
|
555 |
for (int i = 0; i < b.length; i++) { |
|
556 |
int k = b[i] & 0xff; |
|
557 |
if (i != 0) { |
|
558 |
sb.append(':'); |
|
559 |
} |
|
560 |
sb.append(hexDigits[k >>> 4]); |
|
561 |
sb.append(hexDigits[k & 0xf]); |
|
562 |
} |
|
563 |
return sb.toString(); |
|
564 |
} |
|
565 |
||
566 |
public static byte[] parse(String s) { |
|
567 |
if (s.equals("(null)")) { |
|
568 |
return null; |
|
569 |
} |
|
570 |
try { |
|
571 |
int n = s.length(); |
|
572 |
ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3); |
|
573 |
StringReader r = new StringReader(s); |
|
574 |
while (true) { |
|
575 |
int b1 = nextNibble(r); |
|
576 |
if (b1 < 0) { |
|
577 |
break; |
|
578 |
} |
|
579 |
int b2 = nextNibble(r); |
|
580 |
if (b2 < 0) { |
|
581 |
throw new RuntimeException("Invalid string " + s); |
|
582 |
} |
|
583 |
int b = (b1 << 4) | b2; |
|
584 |
out.write(b); |
|
585 |
} |
|
586 |
return out.toByteArray(); |
|
587 |
} catch (IOException e) { |
|
588 |
throw new RuntimeException(e); |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
private static int nextNibble(StringReader r) throws IOException { |
|
593 |
while (true) { |
|
594 |
int ch = r.read(); |
|
595 |
if (ch == -1) { |
|
596 |
return -1; |
|
597 |
} else if ((ch >= '0') && (ch <= '9')) { |
|
598 |
return ch - '0'; |
|
599 |
} else if ((ch >= 'a') && (ch <= 'f')) { |
|
600 |
return ch - 'a' + 10; |
|
601 |
} else if ((ch >= 'A') && (ch <= 'F')) { |
|
602 |
return ch - 'A' + 10; |
|
603 |
} |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
<T> T[] concat(T[] a, T[] b) { |
|
608 |
if ((b == null) || (b.length == 0)) { |
|
609 |
return a; |
|
610 |
} |
|
31270
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
28403
diff
changeset
|
611 |
T[] r = Arrays.copyOf(a, a.length + b.length); |
2 | 612 |
System.arraycopy(b, 0, r, a.length, b.length); |
613 |
return r; |
|
614 |
} |
|
615 |
||
28403 | 616 |
/** |
617 |
* Returns supported algorithms of specified type. |
|
618 |
*/ |
|
619 |
static List<String> getSupportedAlgorithms(String type, String alg, |
|
620 |
Provider p) { |
|
621 |
// prepare a list of supported algorithms |
|
622 |
List<String> algorithms = new ArrayList<>(); |
|
623 |
Set<Provider.Service> services = p.getServices(); |
|
624 |
for (Provider.Service service : services) { |
|
625 |
if (service.getType().equals(type) |
|
626 |
&& service.getAlgorithm().startsWith(alg)) { |
|
627 |
algorithms.add(service.getAlgorithm()); |
|
628 |
} |
|
629 |
} |
|
630 |
return algorithms; |
|
631 |
} |
|
632 |
||
2 | 633 |
} |